-
Notifications
You must be signed in to change notification settings - Fork 0
197 lines (180 loc) · 7.86 KB
/
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
name: release
on:
push:
branches:
- main
jobs:
build:
strategy:
matrix:
python-version: [3.11]
os: ['ubuntu-latest']
runs-on: ${{ matrix.os }}
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Set up Python ${{ matrix.python-version }}
uses: actions/setup-python@v2
with:
python-version: ${{ matrix.python-version }}
- name: Setup
shell: bash
run: |
python -m pip install --upgrade pip
python -m pip install build
python -m build
- name: Get versions
id: get-versions
shell: bash
run: |
python -c "
import subprocess
import tomllib
vparse = lambda x: tuple(map(int, x.split('.')))
with open('pyproject.toml', 'rb') as f:
data = tomllib.load(f)
name = data['project']['name']
checkout_version = data['project']['version']
pypi_version = subprocess.run(f'python -m pip index versions {name}',
shell=True, capture_output=True).stdout
pypi_version = pypi_version.split(b'\n', 1)[0].split(b' ')[1][1:-1].decode('utf-8')
new_version = str(vparse(checkout_version) > vparse(pypi_version)).lower()
subprocess.run(f'echo name={name} >> $GITHUB_OUTPUT', shell=True)
subprocess.run(f'echo tag=v{checkout_version} >> $GITHUB_OUTPUT', shell=True)
subprocess.run(f'echo new-version={new_version} >> $GITHUB_OUTPUT', shell=True)
print(f'Got checkout_version={vparse(checkout_version)!r}')
print(f'Got pypi_version={vparse(pypi_version)!r}')
print(f'Setting name={name}')
print(f'Setting tag=v{checkout_version}')
print(f'Setting new-version={new_version}')
"
- name: Test sdist
id: test-sdist
if: steps.get-versions.outputs.new-version == 'true'
shell: bash
run: |
python -m pip install dist/*.tar.gz
cd $(mktemp -d)
set +e
bash -c "
cp -r ${{ github.workspace }}/test ./test
python -m pip install -r ./test/requirements.txt
pytest --disable-warnings
"
if [ "$?" -eq 0 ]
then
echo result=true >> $GITHUB_OUTPUT
else
echo result=false >> $GITHUB_OUTPUT
fi
set -e
python -m pip uninstall -y -r <(pip freeze)
cd ${{ github.workspace }}
continue-on-error: true
- name: Test bdist_wheel
id: test-bdist-wheel
if: steps.get-versions.outputs.new-version == 'true'
shell: bash
run: |
python -m pip install dist/*.whl
cd $(mktemp -d)
set +e
bash -c "
cp -r ${{ github.workspace }}/test ./test
python -m pip install -r test/requirements.txt
pytest --disable-warnings
"
if [ "$?" -eq 0 ]
then
echo result=true >> $GITHUB_OUTPUT
else
echo result=false >> $GITHUB_OUTPUT
fi
set -e
python -m pip uninstall -y -r <(pip freeze)
cd ${{ github.workspace }}
continue-on-error: true
- name: Logging
shell: bash
run: |
echo new-version=${{ steps.get-versions.outputs.new-version }}
echo sdist-result=${{ steps.test-sdist.outputs.result }}
echo bdist-result=${{ steps.test-bdist-wheel.outputs.result }}
- name: Tag
env:
github_user: KyloRen1
github_token: ${{ github.token }}
#if: (steps.get-versions.outputs.new-version == 'true') && (steps.test-sdist.outputs.result == 'true') && (steps.test-bdist-wheel.outputs.result == 'true')
if: (steps.get-versions.outputs.new-version == 'true')
shell: bash
run: |
git config --global user.email "[email protected]"
git config --global user.name "Action: Update Python project"
git tag "${{ steps.get-versions.outputs.tag }}" -m ""
git push https://$github_user:[email protected]/${{ github.repository }} "${{ steps.get-versions.outputs.tag }}"
- name: GitHub release
#if: (steps.get-versions.outputs.new-version == 'true') && (steps.test-sdist.outputs.result == 'true') && (steps.test-bdist-wheel.outputs.result == 'true')
if: (steps.get-versions.outputs.new-version == 'true')
uses: softprops/action-gh-release@v1
with:
name: "${{ steps.get-versions.outputs.name }} ${{ steps.get-versions.outputs.tag }}"
body: "Autogenerated release notes as follows:"
tag_name: "${{ steps.get-versions.outputs.tag }}"
token: ${{ github.token }}
generate_release_notes: true
- name: Push to PyPI
#if: (steps.get-versions.outputs.new-version == 'true') && (steps.test-sdist.outputs.result == 'true') && (steps.test-bdist-wheel.outputs.result == 'true')
if: (steps.get-versions.outputs.new-version == 'true')
uses: pypa/gh-action-pypi-publish@release/v1
with:
password: ${{ secrets.pypi_token }}
- name: Email on success
env:
email_user: ${{ secrets.email_user }}
email_target: ${{ secrets.email_target }}
email_server: ${{ secrets.email_server }}
email_token: ${{ secrets.email_token }}
if: (steps.get-versions.outputs.new-version == 'true') && (steps.test-sdist.outputs.result == 'true') && (steps.test-bdist-wheel.outputs.result == 'true')
shell: bash
run: |
python -c "
from smtplib import SMTP
from email.mime.text import MIMEText
from_ = '$email_user'
to = '$email_target'
msg = MIMEText('')
msg['From'] = from_
msg['To'] = to
msg['Subject'] = 'Deploy success | ${{ github.repository }} ${{ steps.get-versions.outputs.tag }}'
with SMTP('$email_server') as s:
s.starttls()
s.login('$email_user', '$email_token')
s.sendmail(from_, [to], msg.as_string())
"
- name: Email on failure
env:
email_user: ${{ secrets.email_user }}
email_target: ${{ secrets.email_target }}
email_server: ${{ secrets.email_server }}
email_token: ${{ secrets.email_token }}
if: (steps.get-versions.outputs.new-version == 'true') && ((steps.test-sdist.outputs.result != 'true') || (steps.test-bdist-wheel.outputs.result != 'true'))
shell: bash
run: |
python -c "
from smtplib import SMTP
from email.mime.text import MIMEText
from_ = '$email_user'
to = '$email_target'
msg = MIMEText('')
msg['From'] = from_
msg['To'] = to
msg['Subject'] = 'Deploy ERROR | ${{ github.repository }} ${{ steps.get-versions.outputs.tag }}'
with SMTP('$email_server') as s:
s.starttls()
s.login('$email_user', '$email_token')
s.sendmail(from_, [to], msg.as_string())
"
- name: Fail
if: (steps.get-versions.outputs.new-version == 'true') && ((steps.test-sdist.outputs.result != 'true') || (steps.test-bdist-wheel.outputs.result != 'true'))
shell: bash
run: exit 1