Skip to content

Commit 8137025

Browse files
authored
Merge pull request #96 from OWASP/feature/automated-release-workflow
feat: automate release workflow with github environments
2 parents b93e1bb + 772f89f commit 8137025

1 file changed

Lines changed: 79 additions & 40 deletions

File tree

.github/workflows/publish-to-pypi.yml

Lines changed: 79 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,63 @@
1-
name: Publish to PyPI
1+
name: Release DockSec
22

33
on:
4-
push:
5-
tags:
6-
- 'v*.*.*' # Triggers on version tags like v0.0.21, v1.0.0, etc.
4+
workflow_dispatch:
5+
inputs:
6+
version:
7+
description: 'Version to release (e.g., 2026.5.22.4)'
8+
required: true
9+
type: string
710

811
jobs:
9-
publish:
12+
update-version:
1013
runs-on: ubuntu-latest
1114
permissions:
1215
contents: write
13-
packages: write
16+
outputs:
17+
version: ${{ steps.set_version.outputs.VERSION }}
18+
steps:
19+
- name: Checkout code
20+
uses: actions/checkout@v4
21+
with:
22+
token: ${{ secrets.GITHUB_TOKEN }}
23+
24+
- name: Set version output
25+
id: set_version
26+
run: echo "VERSION=${{ github.event.inputs.version }}" >> $GITHUB_OUTPUT
27+
28+
- name: Update setup.py version
29+
run: |
30+
sed -i 's/version=".*"/version="${{ github.event.inputs.version }}"/' setup.py
31+
32+
- name: Commit and push version update
33+
run: |
34+
git config --global user.name "github-actions[bot]"
35+
git config --global user.email "github-actions[bot]@users.noreply.github.com"
36+
git add setup.py
37+
# Only commit if there are changes
38+
git diff --quiet && git diff --staged --quiet || (git commit -m "chore: bump version to ${{ github.event.inputs.version }}" && git push)
39+
40+
- name: Create and push tag
41+
run: |
42+
git tag v${{ github.event.inputs.version }}
43+
git push origin v${{ github.event.inputs.version }}
44+
45+
publish-pypi:
46+
needs: update-version
47+
runs-on: ubuntu-latest
48+
environment: pypi-release
49+
permissions:
50+
contents: read
1451
id-token: write
1552

1653
steps:
1754
- name: Checkout code
18-
uses: actions/checkout@v6
55+
uses: actions/checkout@v4
56+
with:
57+
ref: v${{ needs.update-version.outputs.version }}
1958

2059
- name: Set up Python
21-
uses: actions/setup-python@v6
60+
uses: actions/setup-python@v5
2261
with:
2362
python-version: '3.12'
2463

@@ -27,45 +66,45 @@ jobs:
2766
python -m pip install --upgrade pip
2867
pip install build twine
2968
30-
- name: Extract version from tag
31-
id: get_version
32-
run: |
33-
# Get the tag name (e.g., v0.0.21)
34-
TAG=${GITHUB_REF#refs/tags/v}
35-
echo "VERSION=$TAG" >> $GITHUB_OUTPUT
36-
echo "Publishing version: $TAG"
37-
38-
- name: Verify version matches setup.py
39-
run: |
40-
SETUP_VERSION=$(python -c "import re; content=open('setup.py').read(); print(re.search(r'version=\"([^\"]+)\"', content).group(1))")
41-
TAG_VERSION="${{ steps.get_version.outputs.VERSION }}"
42-
echo "setup.py version: $SETUP_VERSION"
43-
echo "Git tag version: $TAG_VERSION"
44-
if [ "$SETUP_VERSION" != "$TAG_VERSION" ]; then
45-
echo "ERROR: Version mismatch!"
46-
echo "setup.py has version $SETUP_VERSION but git tag is v$TAG_VERSION"
47-
exit 1
48-
fi
49-
echo "✅ Versions match!"
50-
5169
- name: Build package
5270
run: python -m build
5371

54-
- name: Check distribution files
55-
run: |
56-
ls -lh dist/
57-
twine check dist/*
58-
5972
- name: Publish to PyPI
60-
env:
61-
TWINE_USERNAME: __token__
62-
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
63-
run: |
64-
twine upload dist/*
73+
uses: pypa/gh-action-pypi-publish@release/v1
74+
with:
75+
password: ${{ secrets.PYPI_API_TOKEN }}
76+
77+
publish-github-release:
78+
needs: [update-version, publish-pypi]
79+
runs-on: ubuntu-latest
80+
environment: marketplace-release
81+
permissions:
82+
contents: write
6583

84+
steps:
85+
- name: Checkout code
86+
uses: actions/checkout@v4
87+
with:
88+
ref: v${{ needs.update-version.outputs.version }}
89+
90+
- name: Set up Python
91+
uses: actions/setup-python@v5
92+
with:
93+
python-version: '3.12'
94+
95+
- name: Install build dependencies
96+
run: |
97+
python -m pip install --upgrade pip
98+
pip install build
99+
100+
- name: Build package
101+
run: python -m build
102+
66103
- name: Create GitHub Release
67-
uses: softprops/action-gh-release@v3
104+
uses: softprops/action-gh-release@v2
68105
with:
106+
tag_name: v${{ needs.update-version.outputs.version }}
107+
name: Release v${{ needs.update-version.outputs.version }}
69108
generate_release_notes: true
70109
files: |
71110
dist/*.tar.gz

0 commit comments

Comments
 (0)