-
Notifications
You must be signed in to change notification settings - Fork 10
212 lines (204 loc) · 8.04 KB
/
publish.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
# This GitHub workflow will publish the package to Pypi and create a new stable branch when releasing the master branch.
# For more information see:
# https://help.github.com/actions/language-and-framework-guides/using-python-with-github-actions
# https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/
name: publish
on:
push: # When pushing a tag
tags:
- "*"
- "!*a0" # Exclude initial tag for a new version
jobs:
publish:
name: Build and publish to PyPI
if: startsWith(github.ref, 'refs/tags')
runs-on: ubuntu-latest
permissions:
id-token: write
steps:
#-------- Info gathering and checks
- name: Set pushed tag
id: set-tag
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const result = "${{ github.ref }}".match("refs/tags/(.*)$")[1]
console.log(result)
return result
- name: Check validity of pushed tag
run: |
if [[ ${{ steps.set-tag.outputs.result }} =~ ^[0-9]+\.[0-9]+\.[0-9]+((a|b|rc)[0-9]+)?$ ]]; then
echo "Pushed tag '${{ steps.set-tag.outputs.result }}' is valid";
else
echo "Pushed tag '${{ steps.set-tag.outputs.result }}' is invalid (must be 'M.N.U' or a pre-release 'M.N.Ub/c/rcP'";
false;
fi
- name: Determine whether releasing the master branch
id: set-is-master-branch
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const resp = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "heads/master",
})
const result = (resp.data.object.sha == "${{ github.sha }}")
console.log(result)
return result
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Determine name of stable branch for pushed tag
id: set-stable-branch
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const result = "stable_"+"${{ steps.set-tag.outputs.result }}".match("([0-9]+\.[0-9]+)\.")[1]
console.log(result)
return result
- name: Determine whether releasing stable branch for pushed tag
id: set-is-stable-branch
uses: actions/github-script@v7
with:
result-encoding: string
script: |
var resp
try {
resp = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "heads/${{ steps.set-stable-branch.outputs.result }}",
})
}
catch(err) {
console.log("false (stable branch does not exist: "+err+")")
return false
}
const result = (resp.data.object.sha == "${{ github.sha }}")
console.log(result)
return result
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Determine name of beta branch for pushed tag
id: set-beta-branch
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const result = "beta_"+"${{ steps.set-tag.outputs.result }}".match("([0-9]+\.[0-9]+)\.")[1]
console.log(result)
return result
- name: Determine whether releasing beta branch for pushed tag
id: set-is-beta-branch
uses: actions/github-script@v7
with:
result-encoding: string
script: |
var resp
try {
resp = await github.rest.git.getRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "heads/${{ steps.set-beta-branch.outputs.result }}",
})
}
catch(err) {
console.log("false (beta branch does not exist: "+err+")")
return false
}
const result = (resp.data.object.sha == "${{ github.sha }}")
console.log(result)
return result
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Check released commit to be master or stable or beta branch for pushed tag
run: |
if [[ ${{ steps.set-is-master-branch.outputs.result }} == 'false' && ${{ steps.set-is-stable-branch.outputs.result }} == 'false' && ${{ steps.set-is-beta-branch.outputs.result }} == 'false' ]]; then
echo "Released commit is not 'master' or '${{ steps.set-stable-branch.outputs.result }}' or '${{ steps.set-beta-branch.outputs.result }}' branch";
false;
fi
- name: Set update version
id: set-update-version
uses: actions/github-script@v7
with:
result-encoding: string
script: |
const result = "${{ steps.set-tag.outputs.result }}".match("[0-9]+\.[0-9]+\.([0-9]+)")[1]
console.log(result)
return result
- name: Check update version to be 0 when releasing master branch
if: ${{ steps.set-is-master-branch.outputs.result == 'true' }}
run: |
if [[ ${{ steps.set-update-version.outputs.result }} != '0' ]]; then
echo "Update version '${{ steps.set-update-version.outputs.result }}' in tag '${{ steps.set-tag.outputs.result }}' is invalid (must be 0 when releasing master branch)";
false;
fi
- name: Check update version to be non-0 when releasing stable branch for pushed tag
if: ${{ steps.set-is-stable-branch.outputs.result == 'true' }}
run: |
if [[ ${{ steps.set-update-version.outputs.result }} == '0' ]]; then
echo "Update version '${{ steps.set-update-version.outputs.result }}' in tag '${{ steps.set-tag.outputs.result }}' is invalid (must be non-0 when releasing stable branch for pushed tag)";
false;
fi
#-------- Setup of work environment
- name: Checkout repo
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Set up Python 3.10
uses: actions/setup-python@v5
with:
python-version: "3.10"
- name: Development setup
run: |
make develop
#-------- Publishing of package
- name: Build the distribution
run: |
make build
- name: Display the distribution directory
run: |
ls -l dist
- name: Publish distribution to PyPI
if: startsWith(github.ref, 'refs/tags')
uses: pypa/gh-action-pypi-publish@release/v1
with:
packages-dir: dist
# Pypi has a trusted publisher defined, so we do not need a password:
# https://pypi.org/manage/project/zhmc_prometheus_exporter/settings/publishing/
#-------- Creation of Github release
- name: Determine whether release on Github exists for the pushed tag
id: set-release-exists
uses: octokit/[email protected]
with:
route: GET /repos/${{ github.repository }}/releases/tags/${{ steps.set-tag.outputs.result }}
continue-on-error: true
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create release on Github for the pushed tag if it does not exist
if: ${{ steps.set-release-exists.outputs.status == 404 }}
uses: octokit/[email protected]
with:
route: POST /repos/${{ github.repository }}/releases
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
INPUT_TAG_NAME: ${{ steps.set-tag.outputs.result }}
INPUT_NAME: "Release ${{ steps.set-tag.outputs.result }}"
INPUT_BODY: "Change log https://zhmc-prometheus-exporter.readthedocs.io/en/${{ steps.set-tag.outputs.result }}/changes.html"
#-------- Creation of stable branch
- name: Create new stable branch when releasing master branch
if: steps.set-is-master-branch.outputs.result == 'true'
uses: actions/github-script@v7
with:
script: |
github.rest.git.createRef({
owner: context.repo.owner,
repo: context.repo.repo,
ref: "refs/heads/${{ steps.set-stable-branch.outputs.result }}",
sha: "${{ github.sha }}",
})
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}