-
Notifications
You must be signed in to change notification settings - Fork 4
196 lines (149 loc) · 7.16 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
name: Publish to pip
on:
push:
branches:
- main
paths-ignore:
- 'pyproject.toml' # Ignore changes to pyproject.toml
- 'src/version.py' # Ignore changes to src/version.py
jobs:
publish:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v2
- name: Echo Push Event
run: |
echo trigger_branch=${{ github.ref }} >> $GITHUB_ENV
echo "The workflow was triggered by a push to branch: $trigger_branch"
echo git_event=${{ github.sha }} >> $GITHUB_ENV
echo "Push event SHA: $git_event"
echo "Pushed by: ${{ github.actor }}"
shell: bash
- name: Check if version number is in pyproject.toml
run: |
# Check if the version number is in pyproject.toml
if grep -q "version\s*=\s*['\"]\?[0-9.-]\+['\"]\?" pyproject.toml; then
echo "✔ Version is found in pyproject.toml."
else
echo "✘ ERROR - Version not found in pyproject.toml."
fi
shell: bash
- name: Extract version number from pyproject.toml
run: |
# Use grep to find the line containing the version number
version_line=$(grep -E "version\s*=" pyproject.toml)
# Use awk to extract the version number
version=$(echo "$version_line" | awk -F"['\"]" '{print $2}')
# Echo the extracted version number
echo "✔ Version number is: $version"
echo "version=$version" >> $GITHUB_ENV
shell: bash
- name: Increment the rightmost integer in the version
run: |
# Increment the rightmost integer by 1
IFS='.' read -a version_parts <<< "$version"
last_part="${version_parts[-1]}"
new_last_part=$((last_part + 1))
version_parts[-1]=$new_last_part
new_version="${version_parts[0]}.${version_parts[1]}.${version_parts[2]}"
# Update the version in pyproject.toml
sed -i "s/\(version\s*=\s*['\"]\?\)[0-9.-]\+\(['\"]\?\)/\1$new_version\2/" pyproject.toml
# Echo the updated version
echo "✔ New version number is: $new_version"
echo "new_version=$new_version" >> $GITHUB_ENV
shell: bash
- name: Create a new branch
run: |
branch="feature/update-version-$new_version"
echo "creating branch: $branch"
echo "branch=$branch" >> $GITHUB_ENV
git checkout -b $branch
git push origin $branch
env:
GITHUB_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
- name: Commit and Push Changes
run: |
git config --global user.email "[email protected]"
git config --global user.name "SuleyNL"
# Switch to the update_version branch
git fetch
git checkout $branch
git add pyproject.toml
echo "Branch to push to is: $branch"
# Commit the changes in pyproject.toml
git commit -am "Increment version from $version to $new_version"
# Push the changes from current local branch to remote branch
git push origin HEAD:$branch
env:
GITHUB_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
# The version can also be updated using a version in the github repo, this must first be analyzed for pros and cons
#- name: Update version in src/version.py and pyproject.toml
# run: |
# # Increment the version in src/version.py
# python -c "import re; f=open('src/version.py', 'r+'); v=re.sub(r'(\d+)', lambda x: str(int(x.group(0))+1), f.read()); f.seek(0); f.write(v); f.truncate(); f.close()"
# # Update the version in pyproject.toml
# sed -i 's/\(version = "\)[0-9.]*\(".*\)/\1NEW_VERSION\2/' pyproject.toml
# env:
# NEW_VERSION: ${{ github.run_number }}
- name: Create Pull Request
run: |
echo "Push event SHA: $git_event"
echo mergedBranch="${{ github.event.pull_request.base.ref }}" >> $GITHUB_ENV
echo "✔ Merged from branch: $mergedBranch"
pr_title="Update version from $version to $new_version"
pr_body="This is an automated version-update on pyproject.toml.
The workflow was triggered by a succesfull pull request from branch: $mergedBranch
With push event SHA of: $git_event
This action is performed by the Github Action: publish.yml after every accepted pull request. When finished,
this will have published the latest patch of the Extractable codebase to Pypi so it can be pip installed.
# DO NOT DO ANYTHING WITH THIS PULL REQUEST
This pull request should resolve itself within 1 minute.
If its older than 5 minutes and still not resolved you may check the github workflow actions to see where it went wrong.
And in that case it is safe to delete this pull request"
gh pr create --base main --head $branch --title "$pr_title" --body "$pr_body"
env:
GITHUB_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
- name: Merge Pull Request
run: |
# Use the gh pr review command to request changes
gh pr comment $branch --body "updated version in pyproject.toml from $version to $new_version"
# Merge the pull request
gh pr merge $branch --merge --admin --delete-branch
env:
GITHUB_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
- name: Build package
run: |
python -m pip install --upgrade pdm
pdm build
working-directory: ./ # Specify the directory where pyproject.toml is located
- name: Upload package to PyPI
run: |
python -m pip install twine
twine upload dist/extractable-* --verbose
echo "### :rocket: Succesfully shipped [extractable-$new_version](https://pypi.org/project/extractable/$new_version/) to pip!" > $GITHUB_STEP_SUMMARY
env:
TWINE_USERNAME: __token__
TWINE_PASSWORD: ${{ secrets.PYPI_API }}
# TODO: would be nice if the pushed code was first tested by publishing on test pypi and then rerunning the e2e and unit and component tests
#- name: Add main_version to github repo variable
#TODO: would be nice if this worked: keeping the version number in the github main repo
# run: |
# echo "mversion is $m_version"
# echo "$m_version=$new_version" >> $GITHUB_ENV
#
# echo "main_version=$new_version" >> $GITHUB_ENV
# echo "github env: $new_version"
#
# env:
# GITHUB_TOKEN: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
# m_version: ${{ vars.main_version }}
#- name: Add main_version to github repo variable try 2
# uses: peter-evans/repository-dispatch@v2
# with:
# token: ${{ secrets.GH_PERSONAL_ACCESS_TOKEN }}
# event-type: 'edit-variable'
# client-payload: {
# secret_name: 'MAIN_VERSION',
# secret_value: "${{ env.new_version }}",
# }