Skip to content

Commit 3fddb08

Browse files
kratsghenryiiipre-commit-ci[bot]
authored
feat: Support GitLab CI (#113)
* add example CI yml * forgot missing stage * gotta use rules * fix: conditional CI Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> * fix: use __compiled Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> * fix: update python range * docs: mention gitlab support Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> * add pre-commit auto-update ​ * chore: rename ci/ to .ci/ Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> * fix: move .github if gitlab Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> * fix: filename too long on Windows * Apply suggestions from code review * style: pre-commit fixes --------- Signed-off-by: Henry Schreiner <henryschreineriii@gmail.com> Co-authored-by: Henry Schreiner <henryschreineriii@gmail.com> Co-authored-by: Henry Schreiner <henry.fredrick.schreiner@cern.ch> Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 62d29d7 commit 3fddb08

5 files changed

Lines changed: 201 additions & 1 deletion

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ templates for Python packages?
1818
- Twelve different backends to choose from for building packages.
1919
- Template generation tested in GitHub Actions using nox.
2020
- Supports generation with [copier][], [cookiecutter][], and [cruft][].
21+
- Supports GitHub Actions if targeting a `github.com` url (the default), and
22+
adds experimental GitLab CI support otherwise.
2123
- Includes several compiled backends using [pybind11][], with wheels produced
2224
for all platforms using [cibuildwheel][].
2325
- Provides [`sp-repo-review`][pypi-link] to evaluate existing repos against the

cookiecutter.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,5 +24,6 @@
2424
"__year": "{% now 'utc', '%Y' %}",
2525
"__project_slug": "{{ cookiecutter.project_name | lower | replace('-', '_') | replace('.', '_') }}",
2626
"__type": "{{ 'compiled' if cookiecutter.backend in ['pybind11', 'skbuild', 'mesonpy', 'maturin'] else 'pure' }}",
27-
"__answers": ""
27+
"__answers": "",
28+
"__ci": "{{ 'github' if 'github.com' in cookiecutter.url else 'gitlab' }}"
2829
}

helpers/extensions.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,5 @@ def hook(self, context):
1515
else "pure"
1616
)
1717
context["__answers"] = context["_copier_conf"]["answers_file"]
18+
context["__ci"] = "github" if "github.com" in context["url"] else "gitlab"
1819
return {"cookiecutter": context}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
#!/bin/bash
2+
set -ex
3+
pre-commit autoupdate
4+
git status
5+
git diff --quiet -- .pre-commit-config.yaml && git diff --staged --quiet -- .pre-commit-config.yaml || exit_code=$?
6+
7+
if [[ ${exit_code} -ne 0 ]]; then
8+
PRE_COMMIT_CI_BRANCH="pre-commit-ci-update-config-$(date +'%Y%m%d')"
9+
10+
git config --global user.email "git@gitlab.com"
11+
git config --global user.name "GitLab CI"
12+
git remote set-url origin "https://__pre-commit_ci_token:${PRE_COMMIT_CI_JOB_TOKEN}@${CI_SERVER_HOST}:${CI_SERVER_PORT}/${CI_PROJECT_PATH}.git"
13+
git remote -v
14+
git add .pre-commit-config.yaml
15+
git checkout -b "${PRE_COMMIT_CI_BRANCH}"
16+
git commit -m "chore: [pre-commit.ci] pre-commit autoupdate"
17+
git push -u origin "${PRE_COMMIT_CI_BRANCH}":"${PRE_COMMIT_CI_BRANCH}" \
18+
-o merge_request.create \
19+
-o merge_request.target="$CI_DEFAULT_BRANCH" \
20+
-o merge_request.remove_source_branch \
21+
-o merge_request.title="chore: [pre-commit.ci] pre-commit autoupdate"
22+
fi
Lines changed: 174 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,174 @@
1+
stages:
2+
- autoupdate
3+
- check
4+
- test
5+
- build
6+
- deploy
7+
8+
variables:
9+
# see https://docs.gitlab.com/ee/ci/caching/#cache-python-dependencies
10+
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
11+
12+
cache:
13+
paths:
14+
- .cache/pip
15+
- .venv/
16+
17+
image: python:3.8-buster
18+
before_script:
19+
# want to set up a virtualenv to cache
20+
- apt-get install -y --no-install-recommends git
21+
- python -V
22+
- python -m venv .venv
23+
- source .venv/bin/activate
24+
- python -m pip install -U pip pipx
25+
- python -m pipx ensurepath
26+
- python -m pip freeze
27+
28+
pre-commit:
29+
stage: check
30+
variables:
31+
PRE_COMMIT_HOME: "$CI_PROJECT_DIR/.cache/pre-commit"
32+
rules:
33+
- if: $CI_PIPELINE_SOURCE == "push"
34+
script:
35+
- python -m pip install pre-commit
36+
- pre-commit run --hook-stage manual --all-files
37+
cache:
38+
key:
39+
files:
40+
- .pre-commit-config.yaml
41+
paths:
42+
- .cache/pre-commit
43+
44+
pre-commit-autoupdate:
45+
stage: autoupdate
46+
rules:
47+
- if: $CI_PIPELINE_SOURCE == "schedule"
48+
when: always
49+
- if:
50+
$CI_PIPELINE_SOURCE == "push" && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
51+
when: manual
52+
# manual jobs need allow_failure: https://gitlab.com/gitlab-org/gitlab/-/issues/233876
53+
allow_failure: true
54+
script:
55+
- python -m pip install pre-commit
56+
- .github/pre-commit-update.sh
57+
cache:
58+
key:
59+
files:
60+
- .pre-commit-config.yaml
61+
paths:
62+
- .cache/pre-commit
63+
64+
lint:
65+
stage: check
66+
rules:
67+
- if: $CI_PIPELINE_SOURCE == "push"
68+
script:
69+
- pipx run nox -s pylint
70+
71+
tests:
72+
stage: test
73+
image: $IMAGE
74+
rules:
75+
- if: $CI_PIPELINE_SOURCE == "push"
76+
script:
77+
- python -V
78+
- python -m pip install .[test]
79+
- python -m pytest -ra --cov={{ cookiecutter.project_name }}
80+
parallel:
81+
matrix:
82+
- IMAGE: ['python:3.8-buster', 'python:3.11-buster']
83+
84+
{%- if not cookiecutter.__compiled %}
85+
package:
86+
stage: build
87+
rules:
88+
- if: $CI_PIPELINE_SOURCE == "push"
89+
script:
90+
- pipx run build
91+
{%- if cookiecutter.project_type != "trampolim" %}
92+
- pipx run twine check dist/*
93+
{%- endif %}
94+
artifacts:
95+
paths:
96+
- dist/
97+
expire_in: 1 day
98+
99+
{%- else %}
100+
make_sdist:
101+
stage: build
102+
rules:
103+
- if: $CI_PIPELINE_SOURCE == "push"
104+
script:
105+
- pipx run build --sdist
106+
{%- if cookiecutter.project_type != "trampolim" %}
107+
- pipx run twine check dist/*
108+
{%- endif %}
109+
artifacts:
110+
paths:
111+
- dist/*.tar.gz
112+
expire_in: 1 day
113+
114+
make_wheels:
115+
stage: build
116+
rules:
117+
- if: $CI_PIPELINE_SOURCE == "push"
118+
# make a docker daemon available for cibuildwheel to use
119+
tags:
120+
- docker-privileged
121+
services:
122+
- name: docker:dind
123+
entrypoint: ["env", "-u", "DOCKER_HOST"]
124+
command: ["dockerd-entrypoint.sh"]
125+
matrix:
126+
- CIBW_BUILD: ['cp38-*', 'cp39-*', 'cp310-*', 'cp311-*', 'cp312-*']
127+
CIBW_PLATFORM: ['linux', 'windows'] # 'macos' not supported by CIBW on GitLab CI
128+
variables:
129+
DOCKER_HOST: tcp://docker:2375/
130+
DOCKER_DRIVER: overlay2
131+
# See https://github.com/docker-library/docker/pull/166
132+
DOCKER_TLS_CERTDIR: ""
133+
CIBW_ARCHS_LINUX: x86_64
134+
CIBW_ARCHS_MACOS: x86_64
135+
CIBW_ALLOW_PRERELEASES: "1"
136+
script:
137+
- curl -sSL https://get.docker.com/ | sh
138+
- python -m pip install cibuildwheel
139+
- cibuildwheel --output-dir dist
140+
artifacts:
141+
paths:
142+
- dist/*.whl
143+
expire_in: 1 day
144+
145+
.deploy:
146+
stage: deploy
147+
dependencies:
148+
{%- if not cookiecutter.__compiled %}
149+
- package
150+
{%- else %}
151+
- make_sdist
152+
- make_wheels
153+
{%- endif %}
154+
script:
155+
- pipx run twine upload --verbose dist/*whl dist/*gz
156+
157+
deploy_staging:
158+
extends: .deploy
159+
rules:
160+
- if:
161+
$CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"
162+
variables:
163+
TWINE_REPOSITORY: testpypi
164+
TWINE_USERNAME: __token__
165+
TWINE_PASSWORD: $TESTPYPI_TOKEN
166+
167+
deploy_production:
168+
extends: .deploy
169+
only:
170+
- tags
171+
variables:
172+
TWINE_REPOSITORY: pypi
173+
TWINE_USERNAME: __token__
174+
TWINE_PASSWORD: $PYPI_TOKEN

0 commit comments

Comments
 (0)