-
Notifications
You must be signed in to change notification settings - Fork 20
211 lines (182 loc) · 6.93 KB
/
vcs-scraper-ci.yaml
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
name: VCS Scraper CI
on:
push:
branches: [ "main" ]
paths:
- .github/workflows/vcs-scraper-ci.yaml
- components/resc-vcs-scraper/**
- '!**.md'
- '!**.png'
- '!**.gif'
- .gitignore
pull_request:
branches: [ "main" ]
paths:
- .github/workflows/vcs-scraper-ci.yaml
- components/resc-vcs-scraper/**
- '!**.md'
- '!**.png'
- '!**.gif'
- .gitignore
workflow_dispatch:
env:
RESC_VCS_SCRAPER_DIR: components/resc-vcs-scraper
CONTAINER_REGISTRY: rescabnamro
IMAGE_NAME: resc-vcs-scraper
permissions: read-all
jobs:
python-basic-validation:
name: Python Basic Validation
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9"]
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Install test dependencies
run: |
cd ${{ env.RESC_VCS_SCRAPER_DIR }}
pip install -r test-requirements.txt
- name: Test with pytest
run: |
cd ${{ env.RESC_VCS_SCRAPER_DIR }}
tox -e pytest
- name: Import sorting with isort
run: |
cd ${{ env.RESC_VCS_SCRAPER_DIR }}
tox -e sort
- name: Lint with flake8
run: |
cd ${{ env.RESC_VCS_SCRAPER_DIR }}
tox -e lint
# This step will only execute if PR is created internally.
- name: SonarCloud Scan
if: ${{ (github.event.pull_request.head.repo.full_name == 'abnamro/repository-scanner') || (github.ref == 'refs/heads/main') }}
uses: SonarSource/sonarcloud-github-action@master
with:
projectBaseDir: ${{ env.RESC_VCS_SCRAPER_DIR }}
args: >
-Dsonar.organization=abnamro-resc
-Dsonar.projectKey=abnamro-resc_resc-vcs-scraper
-Dsonar.python.coverage.reportPaths=coverage.xml
-Dsonar.sourceEncoding=UTF-8
-Dsonar.projectName=resc-vcs-scraper
-Dsonar.groupid=resc
-Dsonar.sources=src/
-Dsonar.inclusions=**/*.py
-Dsonar.exclusions=**/*yml,**/*.xml,**/*.txt,**/*.html,**/*.js
-Dsonar.tests=tests/
-Dsonar.cpd.exclusions=**/*yml,**/*.xml,**/*.txt,**/*.html,**/*.js
-Dsonar.coverage.exclusions=**/*yml,**/*.xml,**/*.txt,**/*.html,**/*.js
-Dsonar.pdf.skip=true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Needed to get PR information, if any
SONAR_TOKEN: ${{ secrets.__SONAR_TOKEN_BACKEND__ }}
- name: Get Branch Name
id: extract_branch
run: |
if [[ ${GITHUB_EVENT_NAME} == 'pull_request' ]]; then
export BRANCH_NAME="${GITHUB_HEAD_REF}"
echo "::set-output name=branch_name::${BRANCH_NAME}"
elif [[ ${GITHUB_EVENT_NAME} == 'push' ]]; then
export BRANCH_NAME="${GITHUB_REF_NAME}"
echo "::set-output name=branch_name::${BRANCH_NAME}"
else
echo "Event is neither pull_request nor push"
fi
outputs:
branch_name: ${{ steps.extract_branch.outputs.branch_name }}
python-build-and-publish:
name: Python Build and Publish
needs: python-basic-validation
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.9"]
outputs:
scraper_version: ${{ steps.getversion.outputs.scraper_version }}
steps:
- name: Checkout repository
uses: actions/checkout@v3
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v4
with:
python-version: ${{ matrix.python-version }}
- name: Building python package
run: |
cd ${{ env.RESC_VCS_SCRAPER_DIR }}
python -m pip install --upgrade pip build
python -m build
- name: Publish python package to PyPI
if: ${{ needs.python-basic-validation.outputs.branch_name == 'main' }}
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.__PYPI_TOKEN__ }}
packages_dir: ${{ env.RESC_VCS_SCRAPER_DIR }}/dist
skip_existing: true
- id: getversion
name: Get package version
run: |
cd ${{ env.RESC_VCS_SCRAPER_DIR }}
scraper_version=$(python ./setup.py --version)
echo "scraper_version=$scraper_version" >> $GITHUB_OUTPUT
dockerize:
name: Build and Push Docker image
needs: [python-basic-validation, python-build-and-publish]
runs-on: ubuntu-latest
steps:
- name: Checkout Repository
uses: actions/checkout@v3
- name: Docker Lint
uses: hadolint/hadolint-action@v2.0.0
with:
dockerfile: ${{ env.RESC_VCS_SCRAPER_DIR }}/Dockerfile
failure-threshold: error
- name: Build an image from Dockerfile
run: |
cd ${{ env.RESC_VCS_SCRAPER_DIR }}
docker build -t ${{ env.CONTAINER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{needs.python-build-and-publish.outputs.scraper_version}} .
- name: Run Trivy vulnerability scanner
uses: aquasecurity/trivy-action@master
with:
image-ref: ${{ env.CONTAINER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{needs.python-build-and-publish.outputs.scraper_version}}
format: 'table'
exit-code: '0'
ignore-unfixed: true
vuln-type: 'os,library'
severity: 'CRITICAL,HIGH'
- name: Get Branch Name
run: |
if [[ ${GITHUB_EVENT_NAME} == 'pull_request' ]]; then
echo "BRANCH_NAME=$(echo ${GITHUB_HEAD_REF})" >> "$GITHUB_ENV"
elif [[ ${GITHUB_EVENT_NAME} == 'push' ]]; then
echo "BRANCH_NAME=$(echo ${GITHUB_REF_NAME})" >> "$GITHUB_ENV"
else
echo "Event is neither pull_request nor push"
fi
- name: Determine if image needs to be published
run: |
if [[ ${{ needs.python-basic-validation.outputs.branch_name }} == 'main' ]]; then
echo "PUBLISH_IMAGE=true" >> "$GITHUB_ENV"
echo "Going to publish image to registry"
else
echo "PUBLISH_IMAGE=false" >> "$GITHUB_ENV"
echo "Skipping publishing of image to registry"
fi
- name: Log in to Container Registry
if: ${{ env.PUBLISH_IMAGE == 'true' }}
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
username: ${{ secrets.__DOCKER_HUB_USER__ }}
password: ${{ secrets.__DOCKER_HUB_PASS__ }}
- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: ${{ env.RESC_VCS_SCRAPER_DIR }}/
push: ${{ env.PUBLISH_IMAGE }}
tags: ${{ env.CONTAINER_REGISTRY }}/${{ env.IMAGE_NAME }}:latest, ${{ env.CONTAINER_REGISTRY }}/${{ env.IMAGE_NAME }}:${{needs.python-build-and-publish.outputs.scraper_version}}