-
Notifications
You must be signed in to change notification settings - Fork 0
/
.gitlab-ci.yml
240 lines (206 loc) · 5.76 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
# Official language image. Look for the different tagged releases at:
# https://hub.docker.com/r/library/python/tags/
image: python:3.11
# Define the stages for the pipeline
stages:
- setup
- lint
- bump
- build
- release
- publish
# Pipeline rules
workflow:
rules:
# Prevent running a push pipeline for a release commit that's not a tag
- if: $CI_COMMIT_MESSAGE =~ /^chore\(release\):.*/ && $CI_COMMIT_TAG == null
when: never
# If a push to branch with open merge request then ignore
- if: $CI_PIPELINE_SOURCE == "merge_request_event"
when: never
# If source is push and no open merge request then run
- if: $CI_COMMIT_BRANCH
# Run tag pipelines
- if: $CI_COMMIT_TAG
# Change pip's cache directory to be inside the project directory since we can
# only cache local items.
variables:
PIP_CACHE_DIR: "$CI_PROJECT_DIR/.cache/pip"
VERSION_FILE: ".version"
# Pip's cache doesn't store the python packages
# https://pip.pypa.io/en/stable/topics/caching/
#
# Default cache values
default:
cache: &global_cache
key: $CI_COMMIT_REF_SLUG
paths:
- .cache/pypoetry
- .cache/pip
- .venv/
- ./task
policy: pull-push
# Anchor to use pull only cache
.use_cached_venv: &use_cached_venv
before_script:
- source .venv/bin/activate
cache:
<<: *global_cache
policy: pull
.only_tag: &only_tag
rules:
- if: $CI_COMMIT_TAG
when: on_success
- when: never
.no_tag: &no_tag
rules:
- if: $CI_COMMIT_TAG
when: never
- when: on_success
.only_protected: &only_protected
rules:
- if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_COMMIT_TAG == null
when: on_success
- when: never
# Anchor for docker jobs
.docker-job:
image: docker:latest
services:
- docker:dind
# Deactivate cache for docker jobs
cache: []
# Setup job to install dependencies
build-env:
stage: .pre
script:
- python -m venv .venv/
- source .venv/bin/activate
- pip install -U pip
- pip install poetry
- poetry install
# Install go-task
- sh -c "$(curl --location https://taskfile.dev/install.sh)" -- -d -b .
rules:
# If project dependencies were changed then run
- changes:
- flowctl/
- pyproject.toml
- poetry.lock
when: always
# If .venv exists then skip
- exists:
- .venv/
when: never
# Default to pip install pre-commitmanual, but continue
# pipeline if not run
- when: manual
allow_failure: true
lint:
<<: *use_cached_venv
<<: *no_tag
stage: lint
script:
- ./task lint
lint-commit:
<<: *use_cached_venv
<<: *no_tag
stage: lint
script:
- |
# Get the commit message of the last commit
commit_message=$(git log -1 --pretty=format:%s)
# If the commit message starts with "Merge branch", it is a merge commit, so skip the linting.
if [[ $commit_message == 'Merge branch'* ]]; then
echo "Merge commit detected, skipping lint."
exit 0
fi
pip install gitlint
# Ensure the commit message is valid
# We should always pass this as long as the pusher has the pre-commit hooks installed
# but just as a sanity check we'll run it here
git log -1 --pretty=%B | gitlint
bump-version:
<<: *use_cached_venv
<<: *only_protected
variables:
REPO_URL: "https://release-token:${RELEASE_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git"
stage: bump
before_script:
- pip install -U commitizen
# Set git info
- git config --global user.email "$GIT_COMMIT_EMAIL"
- git config --global user.name "$GIT_COMMIT_USER"
# NOTE: Must be set to CLONE strategy in gitlab CI otherwise there will be
# excess commits and tags from a dirty cache
- git checkout $CI_COMMIT_BRANCH
- git pull $REPO_URL $CI_COMMIT_BRANCH
# Explicitly checkout branch since gitlab CI detaches
script:
# Bump the version
- export CZ_BUMP_OUTPUT=$(cz bump --yes 2>&1)
- export CZ_BUMP_EXIT_CODE=$?
# Push new tag to trigger release workflow if bumped
- echo $CZ_BUMP_OUTPUT
- |
if [[ $CZ_BUMP_EXIT_CODE -eq 21 ]]; then
echo "Version did not change, no release will be made."
else
echo "Version changed."
git push $REPO_URL $CI_COMMIT_BRANCH --tags
fi
get-version:
<<: *use_cached_venv
stage: setup
script:
- export PROJECT_VERSION=$(poetry version --short)
- echo $PROJECT_VERSION
- echo "PROJECT_VERSION=$PROJECT_VERSION" > $VERSION_FILE
artifacts:
reports:
dotenv: $VERSION_FILE
build-wheel:
<<: *use_cached_venv
<<: *only_tag
stage: build
script:
- poetry build --format wheel
artifacts:
name: dist
paths:
- dist/
build-docker:
<<: *use_cached_venv
extends: .docker-job
stage: build
rules:
- if: $CI_COMMIT_TAG
when: on_success
- when: manual
allow_failure: true
script:
- export COMMIT_TAG=${CI_COMMIT_TAG:-$CI_COMMIT_SHORT_SHA}
- echo $CI_GHCR_PAT | docker login $CI_GHCR_REGISTRY -u $CI_GHCR_USER --password-stdin
- ./task build-docker IMAGE_NAME=$CI_GHCR_IMAGE IMAGE_TAG=$COMMIT_TAG PYTHON_VERSION=3.11 DOCKER_BUILD_ARGS="--pull"
- if [ -n "$CI_COMMIT_TAG" ]; then
docker tag "$CI_GHCR_IMAGE:$COMMIT_TAG" "$CI_GHCR_IMAGE:latest";
fi
- docker push "$CI_GHCR_IMAGE" --all-tags
make-release:
<<: *only_tag
stage: release
image: registry.gitlab.com/gitlab-org/release-cli:latest
script:
- echo "Building release $CI_COMMIT_TAG"
release:
name: Release $CI_COMMIT_TAG
tag_name: $CI_COMMIT_TAG
description: $CHANGELOG
publish-package:
<<: *use_cached_venv
<<: *only_tag
stage: publish
variables:
PACKAGE_NAME: flowctl
script: |
poetry config pypi-token.pypi $PYPI_TOKEN
poetry publish