generated from V3lop5/python-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
282 lines (237 loc) · 7.65 KB
/
main.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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
name: CI/CD main
on:
push:
branches:
- main
jobs:
# Inspect the information that is accessible in each context
# https://docs.github.com/en/actions/learn-github-actions/contexts#example-printing-context-information-to-the-log-file
# You can delete this section
jobinfo:
runs-on: ubuntu-latest
steps:
- name: Dump GitHub context
env:
GITHUB_CONTEXT: ${{ toJSON(github) }}
run: echo "$GITHUB_CONTEXT"
- name: Dump job context
env:
JOB_CONTEXT: ${{ toJSON(job) }}
run: echo "$JOB_CONTEXT"
- name: Dump steps context
env:
STEPS_CONTEXT: ${{ toJSON(steps) }}
run: echo "$STEPS_CONTEXT"
- name: Dump runner context
env:
RUNNER_CONTEXT: ${{ toJSON(runner) }}
run: echo "$RUNNER_CONTEXT"
- name: Dump strategy context
env:
STRATEGY_CONTEXT: ${{ toJSON(strategy) }}
run: echo "$STRATEGY_CONTEXT"
- name: Dump matrix context
env:
MATRIX_CONTEXT: ${{ toJSON(matrix) }}
run: echo "$MATRIX_CONTEXT"
# Check linting
lint:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
- name: Cache pip
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: lint-${{ hashFiles('pyproject.toml') }}-${{ hashFiles('requirements**.txt') }}
- name: Install requirements
run: |
pip install -e .[test]
- name: flake8 Lint
uses: reviewdog/action-flake8@v3
with:
github_token: ${{ github.token }}
# Checks if all tests pass
test:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: 'x64'
- name: Cache pip
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: test-${{ hashFiles('pyproject.toml') }}-${{ hashFiles('requirements**.txt') }}
- name: Install requirements
run: |
pip install --upgrade --upgrade-strategy eager -r requirements-dev.txt -e .
- name: Run tests
run: |
pytest --cov=./ensysmod --cov-report=xml --junitxml="result.xml" ./tests
- name: Upload tests results
uses: actions/upload-artifact@v3
if: always()
with:
name: test-results
path: |
coverage.xml
result.xml
uplaod_coverage_results:
needs: test
runs-on: ubuntu-latest
name: "Upload code coverage"
if: always()
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Download tests results
uses: actions/download-artifact@v2
with:
name: test-results
- name: Check files
run: |
cat ./coverage.xml
- name: Upload coverage to Codecov
uses: codecov/codecov-action@v3
env:
CODECOV_TOKEN: ${{ secrets.CODECOV_TOKEN }}
with:
flags: pytest
fail_ci_if_error: true
publish_test_results:
needs: test
runs-on: ubuntu-latest
name: "Publish unit test results"
if: always()
steps:
- name: Download tests results
uses: actions/download-artifact@v2
with:
name: test-results
- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v2
with:
files: result.xml
report_individual_runs: true
# Test if project still runs on every os
test_build:
needs: [test, lint]
runs-on: ${{ matrix.os }}
name: Build Python ${{ matrix.py }} @ ${{ matrix.os }}
strategy:
fail-fast: true
matrix:
os: ["ubuntu-latest", "windows-latest", "macos-latest"]
py: ["3.9", "3.10"]
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: '3.10'
architecture: 'x64'
- name: Cache pip
uses: actions/cache@v3
with:
path: ~/.cache/pip
key: ${{matrix.os}}-${{ matrix.py }}-${{ hashFiles('pyproject.toml') }}-${{ hashFiles('requirements**.txt') }}
- name: Install requirements
run: |
pip install --upgrade --upgrade-strategy eager -r requirements-dev.txt -e .
- name: Run tests
run: |
pytest ./tests
build_dev_image:
needs: [ test, lint, test_build ]
runs-on: ubuntu-latest
name: "Build dev image"
if: ${{ success() && github.actor != 'dependabot[bot]' }}
outputs:
image_tag: ${{ steps.get_tag.outputs.DOCKER_TAG }}
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ github.token }}
- name: Get tag for docker image
id: get_tag
run: |
DOCKER_TAG=ghcr.io/${{ github.repository }}:main
DOCKER_TAG=${DOCKER_TAG,,}
echo ::set-output name=DOCKER_TAG::"${DOCKER_TAG}"
- name: Build the Docker image
run: |
docker build . --tag ${{ steps.get_tag.outputs.DOCKER_TAG }}
- name: Push the Docker image to GitHub Container Registry
run: |
docker push ${{ steps.get_tag.outputs.DOCKER_TAG }}
deploy_dev:
needs: [ build_dev_image ]
runs-on: ubuntu-latest
concurrency: ssh-connection # only one ssh connection at a time
name: "Deploy dev image"
if: ${{ success() && github.actor != 'dependabot[bot]' }}
steps:
- name: Update deployment status - start
uses: bobheadxi/deployments@v1.4.0
id: deployment
with:
step: start
token: ${{ github.token }}
env: Development
no_override: false
desc: "Development deployment for main branch"
ref: "main" # dev deployment of main branch
transient: true
- name: Install VPN
run: |
sudo /sbin/modprobe tun
sudo apt install openconnect
- name: Connect VPN
run: |
echo "${{ secrets.VPN_PASS }}" | sudo openconnect ${{ secrets.VPN_URL }} --background --user=${{ secrets.VPN_USER }} --passwd-on-stdin
- name: Deploy docker container on private server
uses: appleboy/ssh-action@v0.1.4
with:
host: ${{ secrets.SSH_URL }}
username: ${{ secrets.SSH_USER }}
password: ${{ secrets.SSH_PASS }}
script: |
docker system prune -af
docker pull ${{ needs.build_dev_image.outputs.image_tag }}
docker ps --filter publish=9000
docker rm -f $(docker ps --filter publish=9000 -aq)
docker run -d -p 9000:8080 --name "dev" ${{ needs.build_dev_image.outputs.image_tag }}
- name: Disconnect VPN
if: ${{ always() }}
run: |
sudo pkill openconnect
- name: Get env url
id: get_env_url
run: |
ENV_URL="http://${{ secrets.SSH_URL }}:9000"
echo ::set-output name=ENV_URL::"${ENV_URL}"
- name: Update deployment status - finish
uses: bobheadxi/deployments@v1.4.0
if: always()
with:
step: finish
token: ${{ github.token }}
status: ${{ job.status }}
deployment_id: ${{ steps.deployment.outputs.deployment_id }}
env_url: ${{ steps.get_env_url.outputs.env_url }}