-
Notifications
You must be signed in to change notification settings - Fork 13
215 lines (189 loc) · 6.88 KB
/
build-modules.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
name: Build Modules
on:
pull_request:
types: [ opened, synchronize, reopened ]
push:
branches:
- main
workflow_dispatch:
env:
REGISTRY: ghcr.io
IMAGE_PREFIX_NAME: SEKOIA-IO
jobs:
find-modules:
name: Find modules in repo
runs-on: ubuntu-latest
outputs:
matrix: ${{ steps.list-modules.outputs.matrix }}
steps:
- name: Check-out the repo under $GITHUB_WORKSPACE
uses: actions/checkout@v3
with:
fetch-depth: ${{ github.event_name == 'pull_request' && 2 || 0 }}
- id: list-modules
name: List modules having changed files
run: |
if ${{ github.event_name == 'workflow_dispatch' }}; then
echo "matrix=$(cut -d "/" -f1 <<< "$(ls -a */manifest.json)" | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT
exit 0
fi
if ${{ github.event_name == 'pull_request' }}; then
start="HEAD^1"
end="HEAD"
else
start=${{ github.event.before }}
end=${{ github.event.after }}
fi
echo "matrix=$(comm -12 <(cut -d "/" -f1 <<< "$(git diff --name-only -r $start $end)" | sort | uniq) <(cut -d "/" -f1 <<< "$(ls -a */manifest.json)") | jq -R -s -c 'split("\n")[:-1]')" >> $GITHUB_OUTPUT
test:
name: Test module ${{ matrix.module }}
runs-on: ubuntu-latest
needs: find-modules
if: needs.find-modules.outputs.matrix != '[]'
strategy:
matrix:
module: ${{fromJSON(needs.find-modules.outputs.matrix)}}
fail-fast: false
steps:
- name: Check-out the repo under $GITHUB_WORKSPACE
uses: actions/checkout@v3
- name: Set up Python 3.11
uses: actions/setup-python@v4
id: setup-python
with:
python-version: '3.11'
- name: Install Poetry
run: |
pip install poetry
poetry config virtualenvs.in-project true
poetry config installer.modern-installation false
- name: Install Dependencies
id: install-dependencies
run: |
poetry install
working-directory: ${{ matrix.module }}
- name: Execute Black
uses: psf/black@stable
with:
options: "--check --verbose"
src: ./"${{ matrix.module }}"
- name: Execute Mypy
run: |
poetry run pip install mypy
mkdir -p .mypy_cache
poetry run mypy --install-types --non-interactive --ignore-missing-imports --show-column-numbers --hide-error-context .
working-directory: "${{ matrix.module }}"
- name: Execute Python tests
id: execute-tests
run: |
poetry run python -m pytest --junit-xml=junit.xml --cov-report term --cov-report xml:coverage.xml --cov . --cov-config pyproject.toml
working-directory: "${{ matrix.module }}"
- name: Upload Event
uses: actions/upload-artifact@v3
with:
name: Event File
path: ${{ github.event_path }}
- name: Upload Test Results
if: always()
uses: actions/upload-artifact@v3
with:
name: Unit Test Results (${{ matrix.module }})
path: "${{ matrix.module }}/junit.xml"
- name: Code Coverage Flag
run: |
FLAG="${{ matrix.module }}"
FLAG=${FLAG// } # replace spaces
echo FLAG=${FLAG} >> $GITHUB_ENV # update GitHub ENV vars
- name: Code Coverage
uses: codecov/codecov-action@v3
with:
token: ${{ secrets.CODECOV_TOKEN }}
files: ${{ matrix.module }}/coverage.xml
flags: ${{ env.FLAG }}
fail_ci_if_error: true
build-docker:
name: Build ${{ matrix.module }} docker image
runs-on: ubuntu-latest
needs: find-modules
if: needs.find-modules.outputs.matrix != '[]'
strategy:
matrix:
module: ${{fromJSON(needs.find-modules.outputs.matrix)}}
fail-fast: false
steps:
- name: Check-out the repo under $GITHUB_WORKSPACE
uses: actions/checkout@v3
- name: Read Module Manifest
id: read-module-manifest
run: |
content=`cat "${{ matrix.module }}/manifest.json"`
# the following lines are only required for multi line json
content="${content//$'\n'/''}"
content="${content//$'\r'/''}"
# end of optional handling for multi line json
echo $content
echo "$content"
echo "manifest=$content" >> $GITHUB_OUTPUT
- name: Log in to the Container registry
if: ${{ github.event_name == 'push' }}
uses: docker/login-action@65b78e6e13532edd9afa3aa52ac7964289d1a9c1
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata (tags, labels) for Docker
if: ${{ github.event_name == 'push' }}
id: meta
uses: docker/metadata-action@v4
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX_NAME }}/automation-module-${{fromJson(steps.read-module-manifest.outputs.manifest).slug}}
flavor: |
latest=auto
prefix=
suffix=
tags: |
type=pep440,pattern={{version}},value=${{fromJson(steps.read-module-manifest.outputs.manifest).version}}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Build and push Docker image
uses: docker/build-push-action@v4
with:
push: ${{ github.event_name == 'push' }}
context: ${{ matrix.Module }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
test-gate:
name: Build & Test Gate
runs-on: ubuntu-latest
needs: [find-modules, test, build-docker]
if: always()
steps:
- name: Success
if: ${{ !(contains(needs.*.result, 'failure')) || needs.find-modules.outputs.matrix == '[]' }}
run: exit 0
- name: Failure
if: ${{ contains(needs.*.result, 'failure') && needs.find-modules.outputs.matrix != '[]' }}
run: |
exit 1
deploy-module:
name: Deploy modules
runs-on: ubuntu-latest
needs: [find-modules, build-docker]
if: ${{ (github.event_name == 'push' || github.event_name == 'workflow_dispatch') && needs.find-modules.outputs.matrix != '[]' }}
strategy:
matrix:
module: ${{fromJSON(needs.find-modules.outputs.matrix)}}
steps:
- name: my-app-install token
id: my-app
uses: getsentry/action-github-app-token@v1
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.APP_PRIVATE_KEY }}
- name: Repository Dispatch
uses: peter-evans/repository-dispatch@v1
with:
token: ${{ steps.my-app.outputs.token }}
event-type: publish-automation-modules
repository: SekoiaLab/platform
client-payload: '{"module": "${{ matrix.module }}"}'