-
Notifications
You must be signed in to change notification settings - Fork 72
Expand file tree
/
Copy path{% if cookiecutter.__ci=='gitlab' %}.gitlab-ci.yml{% endif %}
More file actions
185 lines (168 loc) · 4.23 KB
/
{% if cookiecutter.__ci=='gitlab' %}.gitlab-ci.yml{% endif %}
File metadata and controls
185 lines (168 loc) · 4.23 KB
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
stages:
- autoupdate
- check
- test
- build
- deploy
variables:
# see https://docs.gitlab.com/ee/ci/caching/#cache-python-dependencies
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
cache:
paths:
- .cache/pip
- .venv/
image: python:3.8-buster
before_script:
# want to set up a virtualenv to cache
- apt-get install -y --no-install-recommends git jq
- python -V
- python -m venv .venv
- source .venv/bin/activate
- python -m pip install -U pip pipx
- python -m pipx ensurepath
- python -m pip freeze
pre-commit:
stage: check
variables:
PRE_COMMIT_HOME: "$CI_PROJECT_DIR/.cache/pre-commit"
rules:
- if: $CI_PIPELINE_SOURCE == "push"
script:
- python -m pip install pre-commit
- pre-commit run --hook-stage manual --all-files
cache:
key:
files:
- .pre-commit-config.yaml
paths:
- .cache/pre-commit
pre-commit-autoupdate:
stage: autoupdate
rules:
- if: $CI_PIPELINE_SOURCE == "schedule"
when: always
- if:
$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
when: manual
# manual jobs need allow_failure: https://gitlab.com/gitlab-org/gitlab/-/issues/233876
allow_failure: true
script:
- python -m pip install pre-commit
- .github/pre-commit-update.sh
cache:
key:
files:
- .pre-commit-config.yaml
paths:
- .cache/pre-commit
lint:
stage: check
rules:
- if: $CI_PIPELINE_SOURCE == "push"
script:
- pipx run nox -s pylint
tests:
stage: test
image: $IMAGE
rules:
- if: $CI_PIPELINE_SOURCE == "push"
script:
- python -V
- python -m pip install .[test]
- python -m pytest -ra --cov={{ cookiecutter.project_name }}
parallel:
matrix:
- IMAGE: ['python:3.8-buster', 'python:3.11-buster']
{%- if cookiecutter.__type == "pure" %}
package:
stage: build
rules:
- if: $CI_PIPELINE_SOURCE == "push"
script:
- pipx run build
- pipx run twine check dist/*
artifacts:
paths:
- dist/
expire_in: 1 day
{%- else %}
make_sdist:
stage: build
rules:
- if: $CI_PIPELINE_SOURCE == "push"
script:
- pipx run build --sdist
- pipx run twine check dist/*
artifacts:
paths:
- dist/*.tar.gz
expire_in: 1 day
make_wheels:
stage: build
rules:
- if: $CI_PIPELINE_SOURCE == "push"
# make a docker daemon available for cibuildwheel to use
tags:
- docker-privileged
services:
- name: docker:dind
entrypoint: ["env", "-u", "DOCKER_HOST"]
command: ["dockerd-entrypoint.sh"]
matrix:
- CIBW_BUILD: ['cp38-*', 'cp39-*', 'cp310-*', 'cp311-*', 'cp312-*']
CIBW_PLATFORM: ['linux', 'windows'] # 'macos' not supported by CIBW on GitLab CI
variables:
DOCKER_HOST: tcp://docker:2375/
DOCKER_DRIVER: overlay2
# See https://github.com/docker-library/docker/pull/166
DOCKER_TLS_CERTDIR: ""
CIBW_ARCHS_LINUX: x86_64
CIBW_ARCHS_MACOS: x86_64
CIBW_ALLOW_PRERELEASES: "1"
script:
- curl -sSL https://get.docker.com/ | sh
- python -m pip install cibuildwheel
- cibuildwheel --output-dir dist
artifacts:
paths:
- dist/*.whl
expire_in: 1 day
{%- endif %}
.deploy:
stage: deploy
dependencies:
{%- if cookiecutter.__type == "pure" %}
- package
{%- else %}
- make_sdist
- make_wheels
{%- endif %}
script:
# Retrieve the OIDC token from GitLab CI/CD and exchange it for a PyPI API token
- oidc_token=$(pipx run id PYPI)
- response=$(curl -X POST "${OIDC_MINT_TOKEN_URL}" -d "{\"token\":\"${oidc_token}\"}")
- api_token=$(jq --raw-output '.token' <<< "${response}")
- pipx run twine upload --password "${api_token}" --verbose dist/*whl dist/*gz
deploy_staging:
extends: .deploy
rules:
- if:
$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"
id_tokens:
PYPI_ID_TOKEN:
aud: testpypi
variables:
TWINE_REPOSITORY: testpypi
TWINE_USERNAME: __token__
OIDC_MINT_TOKEN_URL: "https://test.pypi.org/_/oidc/mint-token"
deploy_production:
extends: .deploy
only:
- tags
id_tokens:
PYPI_ID_TOKEN:
aud: pypi
variables:
TWINE_REPOSITORY: pypi
TWINE_USERNAME: __token__
OIDC_MINT_TOKEN_URL: "https://pypi.org/_/oidc/mint-token"