-
Notifications
You must be signed in to change notification settings - Fork 1
236 lines (204 loc) · 8.26 KB
/
test_publish_and_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
name: Run Tests
on:
# PR merges count as pushes
push:
branches:
- main
pull_request:
workflow_dispatch:
env:
REPO_NAME: sat-utils
SLACK_UPDATES: true
SLACK_CHANNEL_ID: C05Q5KLUW6Q
RELEASE_REPO: https://github.com/ncstate-sat/sat-utils/
FLIT_USERNAME: __token__
FLIT_PASSWORD: ${{ secrets.PYPI_TOKEN }}
jobs:
run_tests:
name: Run Tests
runs-on: ubuntu-latest
steps:
- name: Get Code
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: Get SAT Actions
uses: actions/checkout@v3
with:
repository: ncstate-sat/actions
path: actions
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Cache Packages
uses: actions/cache@v3
with:
path: ~/.local
key: requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ hashFiles('**/requirements/base/base.txt') }}-${{ hashFiles('**/requirements/dev/dev.txt') }}-${{ hashFiles('.github/workflows/*.yml') }}-1
restore-keys: |
requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-
- name: Setup Virtual Environment
run: |
python -m venv /tmp/venv
source /tmp/venv/bin/activate
make setup
- name: Run Pre-commit
run: |
source /tmp/venv/bin/activate
pre-commit install
pre-commit run --all
- name: Run Tests
run: |
source /tmp/venv/bin/activate
pytest
- name: Send Failure Message on Slack if Tests Fail
if: ${{ failure() }}
uses: ./actions/slack-updates
with:
channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }}
message: "The tests for `${{ env.REPO_NAME }}` have failed."
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }}
- name: Send Success Message on Slack
uses: ./actions/slack-updates
with:
channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }}
message: "The tests for `${{ env.REPO_NAME }}` have passed."
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }}
build_and_publish:
name: Build and Publish
# only proceed if test job ran successfully
needs: run_tests
if: ${{ github.event_name == 'push' }}
runs-on: ubuntu-latest
environment: release
permissions:
id-token: write
steps:
- name: Get Code
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
- name: Retrieve current version
run: echo "CURRENT_VERSION=v$(grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)" >> $GITHUB_ENV
- name: Retrieve previous version
run: echo "PREV_VERSION=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV
- name: Check if version has been updated
if: ${{ env.PREV_VERSION >= env.CURRENT_VERSION }}
run: |
echo "Version in pyproject.toml has not been updated"
exit 1
- name: Get SAT Actions
uses: actions/checkout@v3
with:
repository: ncstate-sat/actions
path: actions
- name: Setup Python
uses: actions/setup-python@v4
with:
python-version: "3.10"
- name: Cache Packages
uses: actions/cache@v3
with:
path: ~/.local
key: requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-${{ hashFiles('**/requirements/base/base.txt') }}-${{ hashFiles('**/requirements/dev/dev.txt') }}-${{ hashFiles('.github/workflows/*.yml') }}-1
restore-keys: |
requirements-local-${{ runner.os }}-${{ steps.setup_python.outputs.python-version }}-
- name: Setup Virtual Environment
run: python -m venv /tmp/venv
- name: Configure pip
run: |
source /tmp/venv/bin/activate
make setup
- name: Build and Publish
run: |
source /tmp/venv/bin/activate
ls -la
flit publish
- name: Send Failure Message on Slack if Build/Publish Fails
if: ${{ failure() }}
uses: ./actions/slack-updates
with:
channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }}
message: "The tests for `${{ env.REPO_NAME }}` have passed, but publishing to PyPI failed."
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }}
- name: Send Success Message on Slack
uses: ./actions/slack-updates
with:
channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }}
message: "A new version of `${{ env.REPO_NAME }}` has been uploaded to PyPI."
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }}
release:
name: Release
# only proceed if publish workflow ran successfully
needs: build_and_publish
if: ${{ github.event_name == 'push' }}
runs-on: ubuntu-latest
env:
PR_BODY: ${{ github.event.pull_request.body }}
LATEST_COMMIT_MESSAGE: ${{ github.event.head_commit.message }}
steps:
- name: Get Code
uses: actions/checkout@v3
with:
ref: ${{ github.head_ref }}
fetch-tags: true
- name: Get SAT Actions
uses: actions/checkout@v3
with:
repository: ncstate-sat/actions
path: actions
- name: Retrieve current version
run: echo "CURRENT_VERSION=v$(grep -m 1 version pyproject.toml | tr -s ' ' | tr -d '"' | tr -d "'" | cut -d' ' -f3)" >> $GITHUB_ENV
- name: Retrieve previous version
run: echo "PREV_VERSION=$(git describe --tags `git rev-list --tags --max-count=1`)" >> $GITHUB_ENV
- name: Determine if event is a PR merge
if: contains(fromJSON('["pull_request"]'), github.event_name)
run: |
echo "BODY<<EOF" >> $GITHUB_ENV
echo "${PR_BODY}" >> ${GITHUB_ENV}
echo "EOF" >> $GITHUB_ENV
# TODO still have to check whether non-PR push to main works correctly
- name: Determine if event is just a push
if: ${{ ! contains(fromJSON('["pull_request"]'), github.event_name) }}
run: |
echo "BODY<<EOF" >> $GITHUB_ENV
echo "${LATEST_COMMIT_MESSAGE}" >> ${GITHUB_ENV}
echo "EOF" >> $GITHUB_ENV
- name: Create Release
uses: actions/create-release@v1
# This token is provided by Actions, you do not need to create your own token
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: "${{ env.CURRENT_VERSION }}"
release_name: "${{ env.CURRENT_VERSION }}"
body: |
## What's Changed
${{ env.BODY }}
**Full Changelog**: ${{ env.RELEASE_REPO }}/compare/${{ env.PREV_VERSION }}...${{ env.CURRENT_VERSION }}
- name: Send Failure Message on Slack if Release Fails
if: ${{ failure() }}
uses: ./actions/slack-updates
with:
channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }}
message: "The new version of `${{ env.REPO_NAME }}` has been uploaded to PyPI, but the GitHub release failed."
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }}
- name: Send Success Message on Slack
uses: ./actions/slack-updates
with:
channel-id: ${{ github.event.inputs.slack-channel-id || env.SLACK_CHANNEL_ID }}
message: "A new Github release for `${{ env.REPO_NAME }}` with version `${{ env.CURRENT_VERSION }}` has been created."
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_UPDATES: ${{ github.event.inputs.slack-updates || env.SLACK_UPDATES }}