1- name : lint and test
1+ ---
2+
3+ name : build, test and deploy
24
35on :
46 push :
57 tags :
68 - ' *'
7- pull_request :
89 branches :
910 - master
1011 - develop
1112
13+ env :
14+ DOCTL_VERSION : 1.84.1
15+
1216jobs :
13- linter :
14- runs-on : ubuntu-20.04
17+ flake8 :
18+ runs-on : ubuntu-latest
1519 name : Check python linting
1620 steps :
1721 - name : Checkout
18- uses : actions/checkout@v2
22+ uses : actions/checkout@v3
1923 - name : Start linter
2024 run : |
21- docker run --rm -w="/code/backend" -v $(pwd):/code alpine/flake8:3.9.2 .
25+ docker run --rm -w="/code/backend" -v $(pwd):/code alpine/flake8:5.0.4 .
2226
2327 black :
24- runs-on : ubuntu-20.04
28+ runs-on : ubuntu-latest
2529 name : Black code formatting
2630 steps :
2731 - name : Checkout
28- uses : actions/checkout@v2
32+ uses : actions/checkout@v3
2933 - name : Set up Python
30- uses : actions/setup-python@v1
34+ uses : actions/setup-python@v4
3135 with :
3236 python-version : ' 3.9'
3337 - name : run black
3438 working-directory : ./backend
3539 run : |
36- pip install black==22.6.0
40+ pip install black==21.5b1
3741 black --check .
3842
3943 isort :
40- runs-on : ubuntu-20.04
44+ runs-on : ubuntu-latest
4145 name : isort imports
4246 steps :
4347 - name : Checkout
@@ -52,12 +56,72 @@ jobs:
5256 pip install isort==5.10.1
5357 isort src
5458
59+ build_backend :
60+ if : ${{ secrets.ENABLE_BUILD }} == 'true'
61+ runs-on : ubuntu-latest
62+ needs : [flake8,black,isort]
63+ name : Build backend
64+ steps :
65+ - name : Checkout
66+ uses : actions/checkout@v3
67+ - name : Install doctl
68+ uses : digitalocean/action-doctl@v2
69+ with :
70+ token : ${{ secrets.DIGITALOCEAN_TOKEN }}
71+ - name : Build backend
72+ run : |
73+ docker build --build-arg env=staging -t ${{ secrets.REGISTRY }}/backend:${{ github.sha }} ./backend
74+ - name : Log in to DigitalOcean Container Registry with short-lived credentials
75+ run : doctl registry login --expiry-seconds 600
76+ - name : Push image to DigitalOcean Container Registry
77+ run : docker push ${{ secrets.REGISTRY }}/backend:${{ github.sha }}
78+
79+ build_proxy :
80+ if : ${{ secrets.ENABLE_BUILD }} == 'true'
81+ runs-on : ubuntu-latest
82+ needs : [flake8,black]
83+ name : Build Nginx proxy
84+ steps :
85+ - name : Checkout
86+ uses : actions/checkout@v3
87+ - name : Install doctl
88+ uses : digitalocean/action-doctl@v2
89+ with :
90+ token : ${{ secrets.DIGITALOCEAN_TOKEN }}
91+ - name : Build proxy
92+ run : |
93+ docker build --build-arg env=staging -t ${{ secrets.REGISTRY }}/proxy:${{ github.sha }} ./proxy
94+ - name : Log in to DigitalOcean Container Registry with short-lived credentials
95+ run : doctl registry login --expiry-seconds 600
96+ - name : Push image to DigitalOcean Container Registry
97+ run : docker push ${{ secrets.REGISTRY }}/proxy:${{ github.sha }}
98+
99+ unit_tests :
100+ runs-on : ubuntu-latest
101+ needs : [build_backend, build_proxy]
102+ name : Run unit tests
103+ steps :
104+ - name : Checkout
105+ uses : actions/checkout@v3
106+ - name : Run unit tests
107+ run : |
108+ docker-compose -f ops/docker-compose.test.yml up --exit-code-from backend
109+ - name : Codecov
110+ uses : codecov/codecov-action@v1
111+ with :
112+ file : /data/coverage.xml
113+ flags : unittests
114+ - name : Clean-up
115+ if : always()
116+ run : |
117+ docker-compose -f ops/docker-compose.test.yml down -v
118+
55119 create-release :
56- needs : [linter, black, isort]
57- runs-on : ubuntu-20.04
120+ needs : [flake8, black,isort]
121+ runs-on : ubuntu-latest
58122 steps :
59123 - name : checkout
60- uses : actions/checkout@v2
124+ uses : actions/checkout@v3
61125 with :
62126 fetch-depth : 0 # need this for all history for all branches and tags
63127 - name : Create Release
@@ -69,3 +133,55 @@ jobs:
69133
70134 outputs :
71135 ReleaseTag : ${{ steps.create_release.outputs.release_tag }}
136+
137+ deploy :
138+ runs-on : ubuntu-latest
139+ name : Deploy
140+ needs : [unit_tests]
141+ steps :
142+ - name : Checkout
143+ uses : actions/checkout@v3
144+
145+ - name : Deploy staging
146+ uses : ironhalik/docker-over-ssh-action@v6
147+ if : github.ref == 'refs/heads/develop'
148+ env :
149+ COMPOSE_FILE : ops/docker-compose.staging.yml
150+ STACK_NAME : tagyoureitbot-staging
151+ DIGITALOCEAN_ACCESS_TOKEN : ${{ secrets.DIGITALOCEAN_TOKEN }}
152+ REGISTRY : ${{ secrets.REGISTRY }}
153+ POSTGRES_PASSWORD : ${{ secrets.POSTGRES_PASSWORD }}
154+ PASSWORD : ${{ secrets.PASSWORD }}
155+ CLIENT_SECRET : ${{ secrets.CLIENT_SECRET }}
156+ with :
157+ user : ubuntu
158+ host : ${{ secrets.STAGING_HOST_IP }}
159+ key : ${{ secrets.SSH_KEY }}
160+ script : |
161+ wget https://github.com/digitalocean/doctl/releases/download/v${{ env.DOCTL_VERSION }}/doctl-${{ env.DOCTL_VERSION }}-linux-amd64.tar.gz
162+ tar xf ./doctl-${{ env.DOCTL_VERSION }}-linux-amd64.tar.gz
163+ mv ./doctl /usr/local/bin
164+ doctl registry login
165+ docker stack deploy --compose-file ${COMPOSE_FILE} --with-registry-auth --prune ${STACK_NAME}
166+
167+ - name : Deploy prod
168+ uses : ironhalik/docker-over-ssh-action@v6
169+ if : github.ref == 'refs/heads/master'
170+ env :
171+ COMPOSE_FILE : ops/docker-compose.prod.yml
172+ STACK_NAME : tagyoureit-prod
173+ DIGITALOCEAN_ACCESS_TOKEN : ${{ secrets.DIGITALOCEAN_TOKEN }}
174+ REGISTRY : ${{ secrets.REGISTRY }}
175+ POSTGRES_PASSWORD : ${{ secrets.POSTGRES_PASSWORD }}
176+ PASSWORD : ${{ secrets.PASSWORD }}
177+ CLIENT_SECRET : ${{ secrets.CLIENT_SECRET }}
178+ with :
179+ user : ubuntu
180+ host : ${{ secrets.PROD_HOST_IP }}
181+ key : ${{ secrets.SSH_KEY }}
182+ script : |
183+ wget https://github.com/digitalocean/doctl/releases/download/v${{ env.DOCTL_VERSION }}/doctl-${{ env.DOCTL_VERSION }}-linux-amd64.tar.gz
184+ tar xf ./doctl-${{ env.DOCTL_VERSION }}-linux-amd64.tar.gz
185+ mv ./doctl /usr/local/bin
186+ doctl registry login
187+ docker stack deploy --compose-file ${COMPOSE_FILE} --with-registry-auth --prune ${STACK_NAME}
0 commit comments