-
Notifications
You must be signed in to change notification settings - Fork 2
178 lines (170 loc) · 5.57 KB
/
github-ci-cd.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
name: CI/CD pipeline
run-name: CI/CD
on:
push:
branches: [main]
pull_request:
branches: [main]
workflow_dispatch:
branches: [main]
env:
PROJECT_NAME: ${{ github.event.repository.name }}
PROJECT_SRC_DIR: src/
PROJECT_TESTS_DIR: tests/
DOCKER_SERVICE: dev
DOCKER_TARGET: testing
jobs:
build:
runs-on: ubuntu-latest
outputs:
DOCKER_IMAGE: ${{ steps.set_outputs.outputs.DOCKER_IMAGE }}
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Docker Buildx
uses: docker/setup-buildx-action@v2
-
name: Docker meta
id: meta
uses: docker/metadata-action@v4
with:
images: |
${{ secrets.DOCKER_USERNAME }}/${{env.PROJECT_NAME}}-${{env.DOCKER_SERVICE}}
tags: |
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
-
name: Docker login
uses: docker/login-action@v2
with:
registry: ${{ secrets.DOCKER_REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
-
name: Docker build and push
uses: docker/build-push-action@v4
with:
context: .
push: ${{ github.event_name != 'pull_request' }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
file: dockers/${{env.DOCKER_SERVICE}}/dockerfile
target: ${{env.DOCKER_TARGET}}
cache-from: type=gha
cache-to: type=gha,mode=max
-
name: Set Docker Image Name
id: set_outputs
run: |
DOCKER_IMAGE_ENCRYPTED=$(gpg --symmetric --batch --passphrase pass --output - <(echo ${{ steps.meta.outputs.tags }}) | base64 -w0)
echo "DOCKER_IMAGE=$DOCKER_IMAGE_ENCRYPTED" >> $GITHUB_OUTPUT
static:
runs-on: ubuntu-latest
needs: [build]
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Retrieve Docker image name
run: |
DOCKER_IMAGE_DECRYPTED=$(gpg --decrypt --quiet --batch --passphrase pass --output - <(echo ${{ needs.build.outputs.DOCKER_IMAGE}} | base64 --decode))
echo "DOCKER_IMAGE=$DOCKER_IMAGE_DECRYPTED" >> $GITHUB_ENV
-
name: Docker login
uses: docker/login-action@v2
with:
registry: ${{ secrets.DOCKER_REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
-
name: Docker pull
run: docker pull $DOCKER_IMAGE
-
name: Black
run: >
docker run --rm --name=${{ github.run_id }} $DOCKER_IMAGE
"poetry run black --check ${{env.PROJECT_SRC_DIR}}"
-
name: Pylint
run: >
docker run --rm --name=${{ github.run_id }} $DOCKER_IMAGE
"poetry run pylint --rcfile=pyproject.toml ${{env.PROJECT_SRC_DIR}}"
-
name: Flake8
run: >
docker run --rm --name=${{ github.run_id }} $DOCKER_IMAGE
"poetry run flake8 --toml-config=pyproject.toml ${{env.PROJECT_SRC_DIR}}"
-
name: Isort
run: >
docker run --rm --name=${{ github.run_id }} $DOCKER_IMAGE
"poetry run isort --check-only --settings-path=pyproject.toml ${{env.PROJECT_SRC_DIR}}"
-
name: Mypy
run: >
docker run --rm --name=${{ github.run_id }} $DOCKER_IMAGE
"poetry run mypy --config=pyproject.toml ${{env.PROJECT_SRC_DIR}}"
testing:
runs-on: ubuntu-latest
needs: [build, static]
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Retrieve Docker image name
run: |
DOCKER_IMAGE_DECRYPTED=$(gpg --decrypt --quiet --batch --passphrase pass --output - <(echo ${{ needs.build.outputs.DOCKER_IMAGE}} | base64 --decode))
echo "DOCKER_IMAGE=$DOCKER_IMAGE_DECRYPTED" >> $GITHUB_ENV
-
name: Docker login
uses: docker/login-action@v2
with:
registry: ${{ secrets.DOCKER_REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
-
name: Docker pull
run: docker pull $DOCKER_IMAGE
-
name: Unit testing
run: >
docker run --rm --name=${{ github.run_id }} $DOCKER_IMAGE
"poetry run pytest --cov-report=term-missing --cov=${{env.PROJECT_SRC_DIR}} ${{env.PROJECT_TESTS_DIR}}"
security:
runs-on: ubuntu-latest
needs: [build, testing]
steps:
-
name: Checkout
uses: actions/checkout@v3
-
name: Retrieve Docker image name
run: |
DOCKER_IMAGE_DECRYPTED=$(gpg --decrypt --quiet --batch --passphrase pass --output - <(echo ${{ needs.build.outputs.DOCKER_IMAGE}} | base64 --decode))
echo "DOCKER_IMAGE=$DOCKER_IMAGE_DECRYPTED" >> $GITHUB_ENV
-
name: Docker login
uses: docker/login-action@v2
with:
registry: ${{ secrets.DOCKER_REGISTRY }}
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
-
name: Docker pull
run: docker pull $DOCKER_IMAGE
-
name: Bandit
run: >
docker run --rm --name=${{ github.run_id }} $DOCKER_IMAGE
"poetry run bandit ${{env.PROJECT_SRC_DIR}}"
-
name: Safety
run: >
docker run --rm --name=${{ github.run_id }} $DOCKER_IMAGE
"poetry run safety check"