-
Notifications
You must be signed in to change notification settings - Fork 1
292 lines (256 loc) · 11.4 KB
/
make-release.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
name: Make full release
on:
workflow_dispatch:
inputs:
publish-to-test-pypi:
description: 'Publish to test pypi instead of pypi'
required: false
default: false
type: boolean
bump-rule:
description: 'Rule for computing next release version'
required: false
default: 'prerelease'
type: choice
options:
- patch
- minor
- major
- prepatch
- preminor
- premajor
- prerelease
release-version:
description: 'Version number to use(instead of computing). Should be of the format x.y.z[rcn]. Do not use hyphens.'
required: false
default: ''
type: string
merge-strategy:
description: 'Merge strategy and strategy options. Used only in case of merge conflicts'
required: false
default: ''
type: string
defaults:
run:
shell: bash
env:
LANG: en_US.utf-8
LC_ALL: en_US.utf-8
PYTHON_VERSION: '3.10'
PGPASSWORD: 'postgres'
jobs:
deploy-release:
runs-on: ubuntu-22.04
permissions:
contents: write # To push a branch
pull-requests: write # To create a PR from that branch
services:
# Label used to access the service container
postgres:
# Docker Hub image
image: postgres
# Provide the password for postgres
env:
POSTGRES_PASSWORD: postgres
ports:
# Maps tcp port 5432 on service container to the host
- 5432:5432
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ github.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by GitHub!"
- run: echo "🔎 The name of your branch is ${{ github.ref }} and your repository is ${{ github.repository }}."
#----------------------------------------------
# check-out repo and set-up python
#----------------------------------------------
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: 0
ref: develop
token: ${{ secrets.AUTO_PROJECT_PAT }}
- name: Do a git merge dry run
id: merge-dry-run
run: |
git config --global user.email "[email protected]"
git config --global user.name "Deploy Release Github Action"
git checkout main
git merge --no-commit --no-ff develop
continue-on-error: true
- name: Abort merge dry-run
run: |
git merge --abort
- name: Check if merge had conflicts.
# if there is conflict and there is no merge strategy set then abort merge and exit
if: steps.merge-dry-run.outcome != 'success' && github.event.inputs.merge-strategy == ''
run: |
echo "merge strategy is ${{ inputs.merge-strategy }}"
echo "Merge to main has conflicts. Either do a manual merge and release or set input merge-strategy and re-run action"
exit 1
- name: Recheckout develop
run: |
git checkout develop
#------------------
# setup database
#----------------
- run: |
psql -h localhost -U postgres -c "CREATE DATABASE test_historian;"
psql -h localhost -U postgres -c "CREATE USER historian with encrypted password 'historian';"
psql -h localhost -U postgres -c "GRANT ALL PRIVILEGES on database test_historian to historian;"
- name: Set up Python ${{ env.PYTHON_VERSION }}
id: setup-python
uses: actions/setup-python@v4
with:
python-version: ${{ env.PYTHON_VERSION }}
#----------------------------------------------
# ----- install & configure poetry -----
#----------------------------------------------
- name: Install Poetry
uses: snok/[email protected]
with:
virtualenvs-create: true
virtualenvs-in-project: true
installer-parallel: true
#----------------------------------------------
# install your root project, if required
#----------------------------------------------
- name: Install library
run: |
poetry lock --no-update
poetry install --no-interaction
- name: Use given release-version number
if: inputs.release-version != ''
run: |
echo "Using given release version is ${{ inputs.release-version }}"
poetry version ${{ inputs.release-version }}
NEW_TAG=v$(poetry version --short)
# we want to be able to use the variable in later
# steps we set a NEW_TAG environmental variable
echo "NEW_TAG=$(echo ${NEW_TAG})" >> $GITHUB_ENV
# we don't want to update pyproject.toml yet. don't want this change to create merge conflict.
# we don't really persist right version in pyproject.toml to figure out the next version. we use git tags.
git restore pyproject.toml
#----------------------------------------------
# bump version number for patch
#----------------------------------------------
- name: Bump Version
if: inputs.release-version == ''
run: |
# current_tag is the last tagged release in the repository. From there
# we need to remove the v from the beginning of the tag.
echo "Bump rule is ${{ inputs.bump-rule }}"
echo "Given release version is ${{ inputs.release-version }}"
if ! $(git tag -l "v*" = ''); then
# uses -V which is version sort to keep it monotonically increasing.
current_tag=$(git tag -l "v*" | grep --invert-match '-' | sort --reverse -V | sed -n 1p)
echo "current git tag is ${current_tag}"
current_tag=${current_tag#?}
# current_tag is now the version we want to set our poetry version so
# that we can bump the version
poetry version ${current_tag}
poetry version ${{ inputs.bump-rule }} --no-interaction
else
# very first release. start with inputs.release-version
echo "First release. Setting tag as 0.1.0rc0"
current_tag='0.1.0rc0'
poetry version ${current_tag}
fi
NEW_TAG=v$(poetry version --short)
# Finally because we want to be able to use the variable in later
# steps we set a NEW_TAG environmental variable
echo "NEW_TAG=$(echo ${NEW_TAG})" >> $GITHUB_ENV
# we don't want to update pyproject.toml yet. don't want this change to create merge conflict.
# we don't really persist right version in pyproject.toml to figure out the next version. we use git tags.
git restore pyproject.toml
#--------------------------------------------------------------
# Create a new releases/new_tag
#--------------------------------------------------------------
- name: Create a new releases branch
run: |
git checkout -b releases/${NEW_TAG}
git push --set-upstream origin releases/${NEW_TAG}
#--------------------------------------------------------------
# merge changes back to main
#--------------------------------------------------------------
- name: Merge changes back to main
run: |
git checkout main
git merge ${{ inputs.merge-strategy }} releases/${NEW_TAG}
git push
- name: Run tests on main branch
id: run-tests-on-main
run: |
if [[ -d tests ]]; then
poetry add pytest-timeout --group dev
poetry install --no-interaction
poetry run pytest --timeout=600 tests
fi
continue-on-error: true
- name: Do something with a failing build
if: steps.run-tests-on-main.outcome != 'success'
run: |
echo "tests on main did not succeed. Outcome is ${{ steps.run-tests-on-main.outcome }}"
git reset --hard HEAD~1
git push origin HEAD --force
git branch -d releases/${NEW_TAG}
git push origin --delete releases/${NEW_TAG}
echo "reverted changes to main and removed release branch"
exit 1
- name: Create build artifacts
run: |
# set the right version in pyproject.toml before build and publish
poetry version ${NEW_TAG#?}
poetry build -vvv
- uses: ncipollo/release-action@v1
with:
artifacts: "dist/*.gz,dist/*.whl"
artifactErrorsFailBuild: true
generateReleaseNotes: true
commit: ${{ github.ref }}
# check bump-rule and set accordingly
prerelease: ${{ inputs.bump-rule == 'prerelease' }}
tag: ${{ env.NEW_TAG }}
token: ${{ secrets.AUTO_PROJECT_PAT }}
- name: Publish to pypi
id: publish-to-pypi
if: ${{ ! inputs.publish-to-test-pypi }}
run: |
echo "POETRY_PUBLISH_OPTIONS=''" >> $GITHUB_ENV
poetry config pypi-token.pypi ${{ secrets.PYPI_TOKEN }}
poetry publish
continue-on-error: true
- name: Publish to test-pypi
id: publish-to-test-pypi
if: ${{ inputs.publish-to-test-pypi }}
run: |
poetry config repositories.test-pypi https://test.pypi.org/legacy/
poetry config pypi-token.test-pypi ${{ secrets.TEST_PYPI_TOKEN }}
poetry publish -r test-pypi
continue-on-error: true
- name: if publish to pypi/test-pypi failed revert main and delete release branch
if: ${{ steps.publish-to-pypi.outcome != 'success' && steps.publish-to-test-pypi.outcome != 'success' }}
run: |
echo "publish to pypi/test-pypi did not succeed. Outcome for pypi = ${{ steps.publish-to-pypi.outcome }} outcome for test-pypi= ${{ steps.publish-to-test-pypi.outcome }}"
git reset --hard HEAD~1
git push origin HEAD --force
git branch -d releases/${NEW_TAG}
git push origin --delete releases/${NEW_TAG}
echo "reverted changes to main and removed release branch"
- name: if publish to pypi/test-pypi failed delete release and tag on github
if: ${{ ! (steps.publish-to-pypi.outcome == 'success' || steps.publish-to-test-pypi.outcome == 'success') }}
uses: dev-drprasad/[email protected]
env:
GITHUB_TOKEN: ${{ secrets.AUTO_PROJECT_PAT }}
with:
tag_name: ${{ env.NEW_TAG }}
- name: if publish to pypi/test-pypi failed exit with exit code 1
if: ${{ steps.publish-to-pypi.outcome != 'success' && steps.publish-to-test-pypi.outcome != 'success' }}
run: |
exit 1
#--------------------------------------------------------------
# merge changes back to develop
#--------------------------------------------------------------
- name: Merge changes back to develop
run: |
git checkout develop
git merge develop main
git push