-
Notifications
You must be signed in to change notification settings - Fork 1
110 lines (103 loc) · 3.89 KB
/
test-and-deploy.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
name: Test and Deploy
# This workflow will run with these triggers
on:
# Trigger on pushes to main
push:
branches: [ main ]
# Trigger on pull requests to main
pull_request:
branches: [ main ]
# Manual trigger
workflow_dispatch:
# A workflow run is made up of one or more jobs.
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup .NET
uses: actions/setup-dotnet@v1
with:
dotnet-version: 5.0.x
- name: Restore dependencies
run: dotnet restore
working-directory: ./minitwit
- name: Build dotnet project
run: dotnet build --no-restore
working-directory: ./minitwit
- name: Run dotnet tests
working-directory: ./minitwit/Models.Test
run: dotnet test --no-build --verbosity normal
# Jobs run in parallel by default,
# the publish-docker-image job is configured only to run if any tests fail, by using:
# needs: test
publish-docker-image:
runs-on: ubuntu-latest
needs: test
if: github.ref == 'refs/heads/main'
steps:
- uses: actions/checkout@v2
- name: Dotnet Publish
# Create a publish folder with a binary build
run: dotnet publish -c Release -o ./publish
working-directory: ./minitwit/Api
- name: Docker login
# Login to github packages registry
# github.actor and secrets.GITHUB_TOKEN are generated automatically
run: |
docker login https://docker.pkg.github.com -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }}
- name: Docker build, tag and push
# Build a docker image from /minitwit/Api/publish
# Tag the docker image (default tag latest)
# Push the docker image to github packages registry
run: |
cd ./minitwit/Api
docker build -t twooter .
docker tag twooter docker.pkg.github.com/themagicstrings/twooter/twooter
docker push docker.pkg.github.com/themagicstrings/twooter/twooter
deploy:
needs: publish-docker-image
runs-on: ubuntu-latest
if: github.ref == 'refs/heads/main'
steps:
- name: Configure SSH
# Retrieve private SSH key from github secrets
# Create key file with owner read/write permissions
# Put github secrets host and user into an ssh config file
run: |
mkdir -p ~/.ssh/
echo "$SSH_KEY" > ~/.ssh/staging.key
chmod 600 ~/.ssh/staging.key
cat >>~/.ssh/config <<END
Host staging
HostName $SSH_HOST
User $SSH_USER
IdentityFile ~/.ssh/staging.key
StrictHostKeyChecking no
END
env:
SSH_USER: ${{ secrets.STAGING_SSH_USER }}
SSH_KEY: ${{ secrets.STAGING_SSH_KEY }}
SSH_HOST: ${{ secrets.STAGING_SSH_HOST }}
- name: Pull and run docker image with SSH
# Run ssh with config file and script
# Script will install docker, login to github packages and pull new image, stop and clean up old deployment, run the new image in a new container
run: |
ssh staging "
apt update
apt install -y docker.io
docker login https://docker.pkg.github.com -u ${{ github.actor }} -p ${{ secrets.GITHUB_TOKEN }}
docker pull docker.pkg.github.com/themagicstrings/twooter/twooter:latest
docker service rm twooter-instance
docker service create \
--with-registry-auth \
-p 443:443 \
-p 80:80 \
--mount 'type=volume,src=twooter-logs,dst=/publish/logs,volume-driver=local' \
--name twooter-instance \
-e \"DB_IP=${{ secrets.DB_IP }}\" \
-e \"DB_PASSWORD=${{ secrets.DB_PASSWORD }}\" \
-e \"DB_CONNECTION_STRING=${{ secrets.DB_CONNECTION_STRING }}\" \
docker.pkg.github.com/themagicstrings/twooter/twooter:latest
docker service scale twooter-instance=3
"