Skip to content

Commit aecba3d

Browse files
authored
Merge pull request #50 from spm/ci-pip-publish
[CI] Update publish-release.yml
2 parents e5762c9 + 59e206b commit aecba3d

File tree

1 file changed

+135
-220
lines changed

1 file changed

+135
-220
lines changed

.github/workflows/publish-release.yml

Lines changed: 135 additions & 220 deletions
Original file line numberDiff line numberDiff line change
@@ -1,239 +1,154 @@
1-
name: Upload Python Package
1+
name: Upload and Publish Python Package
22

33
on:
4-
workflow_dispatch:
5-
push:
6-
tags:
7-
- "[0-9][0-9].[0-9]*"
8-
9-
env:
10-
MLM_LICENSE_TOKEN: ${{ secrets.MATLAB_BATCH_TOKEN }}
11-
12-
permissions:
13-
contents: write
4+
workflow_run:
5+
workflows: ["run_unit_tests"]
6+
types:
7+
- completed
148

159
jobs:
16-
compile-and-deploy:
17-
strategy:
18-
fail-fast: false
19-
matrix:
20-
include:
21-
- matlab: "R2020b"
22-
python: "2.7"
23-
os: "ubuntu-20.04"
24-
25-
- matlab: "R2020b"
26-
python: "3.8"
27-
py3from: "3.6"
28-
py3to: "3.9"
29-
os: "ubuntu-latest"
30-
31-
- matlab: "R2021a"
32-
python: "2.7"
33-
os: "ubuntu-20.04"
34-
35-
- matlab: "R2021a"
36-
python: "3.8"
37-
py3from: "3.7"
38-
py3to: "3.9"
39-
os: "ubuntu-latest"
40-
41-
- matlab: "R2021b"
42-
python: "2.7"
43-
os: "ubuntu-20.04"
44-
45-
- matlab: "R2021b"
46-
python: "3.9"
47-
py3from: "3.7"
48-
py3to: "3.10"
49-
os: "ubuntu-latest"
50-
51-
- matlab: "R2022a"
52-
python: "2.7"
53-
os: "ubuntu-20.04"
54-
55-
- matlab: "R2022a"
56-
python: "3.9"
57-
py3from: "3.8"
58-
py3to: "3.10"
59-
os: "ubuntu-latest"
60-
61-
- matlab: "R2022b"
62-
python: "2.7"
63-
os: "ubuntu-20.04"
64-
65-
- matlab: "R2022b"
66-
python: "3.10"
67-
py3from: "3.8"
68-
py3to: "3.11"
69-
os: "ubuntu-latest"
70-
71-
- matlab: "R2023a"
72-
python: "3.10"
73-
py3from: "3.8"
74-
py3to: "3.11"
75-
os: "ubuntu-latest"
76-
77-
- matlab: "R2023b"
78-
python: "3.11"
79-
py3from: "3.9"
80-
py3to: "3.12"
81-
os: "ubuntu-latest"
82-
83-
- matlab: "R2024a"
84-
python: "3.11"
85-
py3from: "3.9"
86-
py3to: "3.12"
87-
os: "ubuntu-latest"
88-
89-
- matlab: "R2024b"
90-
python: "3.12"
91-
py3from: "3.9"
92-
py3to: "3.13"
93-
os: "ubuntu-latest"
94-
95-
- matlab: "R2024b"
96-
main: "true"
97-
python: "3.12"
98-
py3from: "3.9"
99-
py3to: "3.13"
100-
os: "ubuntu-latest"
101-
102-
- matlab: "R2025a"
103-
main: "false"
104-
python: "3.12"
105-
py3from: "3.9"
106-
py3to: "3.13"
107-
os: "ubuntu-latest"
108-
109-
runs-on: ${{ matrix.os }}
110-
111-
112-
steps:
113-
- name: Update MPM
114-
run: |
115-
rm -f /home/runner/work/_temp/mpm
116-
wget https://www.mathworks.com/mpm/glnxa64/mpm -O /home/runner/work/_temp/mpm
117-
/usr/bin/chmod +x /home/runner/work/_temp/mpm
118-
119-
- name: Set up MATLAB
120-
uses: matlab-actions/setup-matlab@v2
121-
with:
122-
cache: True
123-
release: ${{ matrix.matlab }}
124-
products: |
125-
MATLAB_Compiler
126-
MATLAB_Compiler_SDK
127-
128-
- name: Check out repository
10+
check-tag:
11+
name: Check if commit is a tag
12+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
13+
runs-on: ubuntu-latest
14+
outputs:
15+
is_tagged: ${{ steps.check-tag.outputs.is_tagged }}
16+
tag_name: ${{ steps.check-tag.outputs.tag_name }}
17+
is_prerelease: ${{ steps.check-tag.outputs.is_prerelease }}
18+
steps:
19+
- name: Fetch repo
12920
uses: actions/checkout@v4
130-
131-
- name: Run MPython
132-
uses: matlab-actions/run-command@v2
13321
with:
134-
command: |
135-
cd('.mpython');
136-
addpath(genpath('.'));
137-
spm_make_python('.', true, false);
138-
139-
- name: Set up Python
22+
fetch-depth: 0
23+
24+
- name: Check if commit was a tag and detect prerelease
25+
id: check-tag
26+
run: |
27+
TAG=$(git tag --points-at ${{ github.event.workflow_run.head_sha }} | head -n 1)
28+
echo "Found tag: $TAG"
29+
if [[ "$TAG" =~ ^[0-9]+\.[0-9]+\.[0-9]+.*$ ]]; then
30+
echo "is_tagged=true" >> $GITHUB_OUTPUT
31+
echo "tag_name=$TAG" >> $GITHUB_OUTPUT
32+
33+
if [[ "$TAG" =~ ^[0-9]+\.[0-9]+\.[0-9]+[a-zA-Z].*$ ]]; then
34+
echo "is_prerelease=true" >> $GITHUB_OUTPUT
35+
else
36+
echo "is_prerelease=false" >> $GITHUB_OUTPUT
37+
fi
38+
else
39+
echo "is_tagged=false" >> $GITHUB_OUTPUT
40+
fi
41+
42+
build:
43+
name: Build Package
44+
needs: check-tag
45+
if: needs.check-tag.outputs.is_tagged == 'true'
46+
runs-on: ubuntu-latest
47+
steps:
48+
- uses: actions/checkout@v4
49+
50+
- name: Set up Python
14051
uses: actions/setup-python@v5
14152
with:
142-
python-version: ${{ matrix.python }}
143-
53+
python-version: "3.12"
54+
14455
- name: Install dependencies
14556
run: |
14657
python -m pip install --upgrade pip
14758
pip install build
148-
pip install numpy
14959
150-
- name: Setup python version
151-
if: ${{ matrix.main }}
152-
run: |
153-
sed -i 's/\(version = "[^"]*\)"/version = "${{ github.ref_name }}"/' pyproject.toml
60+
- name: Build sdist and wheel
61+
run: python -m build
15462

155-
- name: Add Matlab version specifier
156-
if: ${{ !matrix.main }}
157-
run: |
158-
sed -i 's/\(version = "[^"]*\)"/\1+${{ matrix.matlab }}"/' pyproject.toml
63+
- name: Upload built packages
64+
uses: actions/upload-artifact@v4
65+
with:
66+
name: dist
67+
path: dist/
15968

160-
- name: Setup Python version (2.7)
161-
if: ${{ !matrix.py3from }}
162-
run: |
163-
sed -i 's/^python-requires = .*/python-requires = ">=2.7,<3.1"/' pyproject.toml
164-
echo "[tool.distutils.bdist_wheel]" >> pyproject.toml
165-
echo "python-tag = \"2.7\"" >> pyproject.toml
166-
cat pyproject.toml
167-
168-
- name: Setup Python version (3+)
169-
if: ${{ matrix.py3from }}
170-
run: |
171-
FROM=${{ matrix.py3from }}
172-
TO=${{ matrix.py3to }}
173-
sed -i 's/^python-requires = .*/python-requires = "$FROM>=,<$TO"/' pyproject.toml
174-
tags=$(for i in $(seq ${FROM//3./} $(( ${TO//3./} - 1)) ); do echo -n "py3$i."; done | sed 's/\.$//')
175-
echo "[tool.distutils.bdist_wheel]" >> pyproject.toml
176-
echo "python-tag = \"$tags\"" >> pyproject.toml
177-
cat pyproject.toml
178-
179-
- name: Build package
180-
run: python -m build --sdist --wheel
181-
182-
- name: Extract MATLAB path to file
183-
uses: matlab-actions/run-command@v2
69+
publish-to-testpypi:
70+
name: Publish to TestPyPI
71+
needs: build
72+
runs-on: ubuntu-latest
73+
permissions:
74+
id-token: write
75+
steps:
76+
- uses: actions/download-artifact@v4
18477
with:
185-
command: |
186-
fileID = fopen('matlab_path.txt', 'w');
187-
matlabpath = matlabroot;
188-
matlabpath = strrep(matlabpath, filesep, '/');
189-
matlabpath = strrep(matlabpath, '\', '\\');
190-
matlabpath = strrep(matlabpath, 'C:', '/c/');
191-
matlabpath = strrep(matlabpath, 'D:', '/d/');
192-
fprintf(fileID, matlabpath);
193-
fclose(fileID);
194-
# sometimes this step hangs when closing matlab, automatically terminating after 2 minutes solves the issue
195-
timeout-minutes: 2
196-
continue-on-error: true
197-
198-
- name: Set environment variable with MATLAB path
199-
shell: bash # Works on Windows as well because of shell: bash
200-
run: |
201-
matlab_path=$(cat matlab_path.txt)
202-
echo "MATLAB_PATH=$matlab_path" >> $GITHUB_ENV
203-
204-
- name: Install package
205-
run: |
206-
python -m pip install $(find ./dist -name "*.whl")
207-
208-
- name: Run unit tests
209-
run: |
210-
export LD_LIBRARY_PATH="${LD_LIBRARY_PATH:+${LD_LIBRARY_PATH}:}\
211-
${{ env.MATLAB_PATH }}/runtime/glnxa64:\
212-
${{ env.MATLAB_PATH }}/bin/glnxa64:\
213-
${{ env.MATLAB_PATH }}/sys/os/glnxa64:\
214-
${{ env.MATLAB_PATH }}/extern/bin/glnxa64"
215-
python -m unittest discover tests -v
216-
217-
- name: Detect pre-release
218-
shell: bash
78+
name: dist
79+
path: dist/
80+
81+
- name: Publish to TestPyPI
82+
uses: pypa/gh-action-pypi-publish@release/v1
83+
with:
84+
repository-url: https://test.pypi.org/legacy/
85+
86+
test-install:
87+
name: Test Install from TestPyPI
88+
needs: publish-to-testpypi
89+
runs-on: ubuntu-latest
90+
steps:
91+
- name: Set up Python
92+
uses: actions/setup-python@v5
93+
with:
94+
python-version: "3.12"
95+
96+
- name: Test install from TestPyPI
21997
run: |
220-
if echo "${{ github.ref_name }}" | grep -q '^[0-9][0-9]\.[0-9][0-9]\.[A-Za-z]'; then
221-
echo "PRERELEASE=true" >> $GITHUB_ENV
222-
else
223-
echo "PRERELEASE=false" >> $GITHUB_ENV
224-
fi
225-
echo PRERELEASE=${{ env.PRERELEASE }}
226-
227-
- name: Release package
228-
uses: softprops/action-gh-release@v2
98+
python -m pip install --index-url https://test.pypi.org/simple/ --extra-index-url https://pypi.org/simple spm-python
99+
python -c "import spm"
100+
101+
publish-to-pypi:
102+
name: Publish to PyPI
103+
needs: test-install
104+
runs-on: ubuntu-latest
105+
environment:
106+
name: pypi
107+
url: https://pypi.org/project/spm-python/
108+
permissions:
109+
id-token: write
110+
steps:
111+
- uses: actions/download-artifact@v4
229112
with:
230-
prerelease: ${{ env.PRERELEASE }}
231-
files: ./dist/spm_python*.whl
232-
continue-on-error: true
233-
234-
- name: Publish package
235-
uses: pypa/gh-action-pypi-publish@27b31702a0e7fc50959f5ad993c78deac1bdfc29
113+
name: dist
114+
path: dist/
115+
116+
- name: Publish to PyPI
117+
uses: pypa/gh-action-pypi-publish@release/v1
118+
119+
sign-and-release:
120+
name: Sign and Create GitHub Release
121+
needs: [check-tag, publish-to-pypi]
122+
if: needs.check-tag.outputs.is_tagged == 'true'
123+
runs-on: ubuntu-latest
124+
permissions:
125+
contents: write
126+
id-token: write
127+
steps:
128+
- uses: actions/download-artifact@v4
129+
with:
130+
name: dist
131+
path: dist/
132+
133+
- name: Sign with Sigstore
134+
uses: sigstore/gh-action-sigstore-python@v3.0.0
236135
with:
237-
user: __token__
238-
password: ${{ secrets.PYPI_API_TOKEN }}
239-
136+
inputs: >-
137+
dist/*.tar.gz
138+
dist/*.whl
139+
140+
- name: Create GitHub Release
141+
env:
142+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
143+
run: |
144+
gh release create "${{ needs.check-tag.outputs.tag_name }}" \
145+
--repo "$GITHUB_REPOSITORY" \
146+
--notes "" \
147+
$([[ "${{ needs.check-tag.outputs.is_prerelease }}" == "true" ]] && echo "--prerelease") \
148+
dist/*
149+
150+
- name: Upload artifact signatures
151+
env:
152+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
153+
run: |
154+
gh release upload "${{ needs.check-tag.outputs.tag_name }}" dist/** --repo "$GITHUB_REPOSITORY"

0 commit comments

Comments
 (0)