-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path.gitlab-ci.yml
323 lines (305 loc) · 8.75 KB
/
.gitlab-ci.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
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
image: python:3.12-bullseye
stages:
- pretest
- build
- test
- pre-release
- post-mr
- deploy
- release
# Global variables for release jobs
variables:
VERSION: minor
BRANCH: main
# Create an anchor to setup node
.setup_node:
before_script:
- pip install -U pip
# Install nodejs
- wget https://nodejs.org/dist/v18.12.1/node-v18.12.1-linux-x64.tar.xz
- tar -xvf node-v18.12.1-linux-x64.tar.xz
- mv node-v18.12.1-linux-x64 /root/nodejs
- rm -rf node-v18.12.1-linux-x64.tar.xz
- export PATH=/root/nodejs/bin:$PATH
- npm --version
# Anchor for setting up prepare_release job
.setup_hatch:
before_script:
- echo "Setting up hatch..."
# Install hatch and install package
- pip install git+https://github.com/pypa/hatch
- pip install -e '.[dev]'
# Anchor with reference for prepare_release job.
# We will test it in CI as well without actually pushing
# the commits. So we reference the anchor to avoid duplication
.prepare_release: &prepare_release
# Check out correct branch
# By default a shallow clone is made in CI. So first fetch from origin
# and then checkout if branch is other than main
- |
if [[ ${BRANCH} != "main" ]]; then
git fetch origin ${BRANCH}
git checkout ${BRANCH}
fi
# Bump version
- hatch version ${VERSION}
# Get new version
- export NEXT_VERSION_SPECIFIER=$(hatch version)
# Generate changelog
- gitlab-activity
- cat CHANGELOG.md
- git add CHANGELOG.md gitlab_activity/_version.py
# Run pre-commit to auto format files
- pre-commit install
- pre-commit run --all-files --show-diff-on-failure || true
# Add potentially changed files
- git add CHANGELOG.md || true
# Configure mail and name of the user who should be visible in the commit history
- git config --global user.email 'release-bot@gitlab.com'
- git config --global user.name 'Release Bot'
# Dont run pre-commit here
- git commit -m "Bump version to ${NEXT_VERSION_SPECIFIER} & update CHANGELOG.md" --no-verify
# Create new tag
- git tag ${NEXT_VERSION_SPECIFIER} -m "Release ${NEXT_VERSION_SPECIFIER}"
# Anchor with reference for add_changelog_entry job.
.add_changelog_entry: &add_changelog_entry # Generate changelog
- gitlab-activity
- cat CHANGELOG.md
- git add CHANGELOG.md
# Run pre-commit to auto format files
- pre-commit install
- pre-commit run --all-files --show-diff-on-failure || true
# Add potentially changed files
- git add CHANGELOG.md || true
# Configure mail and name of the user who should be visible in the commit history
- git config --global user.email 'release-bot@gitlab.com'
- git config --global user.name 'Release Bot'
# Dont run pre-commit here
- git commit -m "Update CHANGELOG.md" --no-verify
# Pre commit tests
pre-commit:
stage: pretest
script:
- mkdir -p build/reports
- pip install pre-commit
- pre-commit install
- pre-commit run --all-files --show-diff-on-failure
artifacts:
paths:
- build/reports/
expire_in: 1 day
except:
- tags
# Test to build package
build-test:
stage: build
script:
- pip install .
artifacts:
paths:
- build/reports/
expire_in: 1 day
except:
- tags
# Test to build docs
build-doc:
stage: build
extends: .setup_node
script:
- pip install git+https://github.com/pypa/hatch
- hatch run docs:build
artifacts:
paths:
- build/reports/
expire_in: 1 day
except:
- tags
# Unit and functional tests
tests:
stage: test
parallel:
matrix:
- PYVER: ["3.8", "3.9", "3.10", "3.11"]
script:
# Install hatch first. Install it from Git until
# a release with [PR#1002](https://github.com/pypa/hatch/pull/1002)
# is made
- pip install git+https://github.com/pypa/hatch
# Checks the current python version
- hatch run +py=$PYVER test:check
# Runs tests
- hatch run +py=$PYVER test:test
# Codecov specific stuff
- curl -Os https://uploader.codecov.io/latest/linux/codecov
- chmod +x codecov
# Seems like we need to add HTTP Proxy explicitly
# Add coverage report only for one version
- ./codecov -t $CODECOV_TOKEN -U $HTTPS_PROXY -f coverage.xml -F py$PYVER
after_script:
- cp coverage.xml build/reports/
coverage: '/TOTAL\s+\d+\s+\d+\s+\d+\s+\d+\s+(\d+)%/'
artifacts:
paths:
- build/reports/
expire_in: 1 day
except:
- tags
# Test to check updating changelog for each MR
test-update-changelog:
stage: pre-release
allow_failure: true
extends: .setup_hatch
# We do clone for this test as cached repos might have existing
# tags that can fail the job.
variables:
GIT_STRATEGY: clone
script:
- *add_changelog_entry
after_script:
- cp CHANGELOG.md build/reports/
artifacts:
paths:
- build/reports/
expire_in: 1 day
except:
- tags
# Test to check release steps
test-release:
stage: pre-release
extends: .setup_hatch
# We do clone for this test as cached repos might have existing
# tags that can fail the job.
variables:
GIT_STRATEGY: clone
script:
- *prepare_release
after_script:
- cp CHANGELOG.md build/reports/
artifacts:
paths:
- build/reports/
expire_in: 1 day
except:
- tags
# Test to check publish steps with local PyPi server
test-publish:
stage: pre-release
needs:
- test-release
before_script:
# Install prerequisites
- pip install git+https://github.com/pypa/hatch
script:
# Build package
- hatch run publish:build-package
# Check dist files
- hatch run publish:check-package
# Start a test pypi server
- hatch run publish:start-localpypi &
# Sleep for a while for server to start
- sleep 30
# Upload package to test
- hatch run publish:upload-to-localpypi
artifacts:
paths:
- build/reports/
expire_in: 1 day
except:
- tags
# Update changelog after MR has been succesfully merged
update_changelog:
stage: post-mr
extends: .setup_hatch
# We do clone for this test as cached repos might have existing
# tags that can fail the job
variables:
GIT_STRATEGY: clone
script:
- *add_changelog_entry
# Set remote push URL and push to originating branch
- git remote set-url --push origin "https://${GITLAB_CI_USER}:${GITLAB_CI_TOKEN}@${CI_REPOSITORY_URL#*@}"
- git push origin HEAD:${CI_COMMIT_REF_NAME}
# Only run on main branch on merge commit
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_MESSAGE =~ /Merge.*/
# Generate and deploy docs
pages:
stage: deploy
extends: .setup_node
script:
- pip install git+https://github.com/pypa/hatch
- hatch run docs:build
- mv docs/website/build/ public/
artifacts:
paths:
- public
only:
- main
except:
- tags
# Create a new release on GitLab UI
release:
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
script:
- echo "Running release job"
release:
name: "Release File $CI_COMMIT_TAG"
description: "Created using the GitLab's release-cli"
tag_name: $CI_COMMIT_TAG
ref: $CI_COMMIT_TAG
only:
- tags
# Prepare release by tagging and adding entry to changelog
prepare_release:
stage: release
when: manual
# We do clone for this test as cached repos might have existing
# tags that can fail the job
variables:
GIT_STRATEGY: clone
extends: .setup_hatch
script:
- *prepare_release
# Set remote push URL and push to originating branch
- git remote set-url --push origin "https://${GITLAB_CI_USER}:${GITLAB_CI_TOKEN}@${CI_REPOSITORY_URL#*@}"
- git push origin HEAD:${CI_COMMIT_REF_NAME} -o ci.skip # Pushes to the same branch as the trigger
- git push origin ${NEXT_VERSION_SPECIFIER} # Pushes the tag BUT triggers the CI to run tagged jobs
# Only run on main branch and do not run on tags
# Because we create tag in this job
only:
- main
except:
- tags
# Publish the release to PyPI
# NOTE: Before we were attempting to run this on tags job. This
# WONT WORK as protected variables are ONLY available on
# PROTECTED TAGS. So we need to run this job on default protected
# branch
publish:
stage: release
needs:
- prepare_release
# Use clone strategy to be able to checkout "latest" main
variables:
GIT_STRATEGY: clone
before_script:
- echo "Preparing publish job..."
# This is crucial. If not the commit we made from previous job
# will not be available here as CI will always checkout the commit
# that triggered the job
- git fetch origin main
- git checkout main
# Install prerequisites
- pip install git+https://github.com/pypa/hatch
script:
# Build package
- hatch run publish:build-package
# Check dist files
- hatch run publish:check-package
# Upload package to test
- hatch run publish:upload-to-pypi
only:
- main
except:
- tags