Skip to content

Commit d4a1e10

Browse files
committed
Add publish and release workflows
1 parent 8df956a commit d4a1e10

File tree

2 files changed

+144
-0
lines changed

2 files changed

+144
-0
lines changed

.github/workflows/publish.yaml

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
name: Publish PyPi package
2+
3+
on:
4+
workflow_run:
5+
workflows: ["Release Workflow"]
6+
types:
7+
- completed
8+
workflow_dispatch:
9+
10+
jobs:
11+
build-and-publish:
12+
name: Build and publish PyPi package
13+
runs-on: ubuntu-latest
14+
if: ${{ github.event.workflow_run.conclusion == 'success' }}
15+
permissions:
16+
packages: write
17+
contents: read
18+
steps:
19+
- name: Check out the repo
20+
uses: actions/checkout@v4
21+
22+
- name: Fetch tags
23+
run: git fetch --depth=1 --tags
24+
25+
- name: Get latest tag or set default
26+
id: latest_tag
27+
run: |
28+
TAG=$(git tag -l --sort=-v:refname | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)
29+
if [ -z "$TAG" ]; then
30+
TAG="latest"
31+
fi
32+
echo "::set-output name=tag::$TAG"
33+
34+
- name: Set up Python
35+
uses: actions/setup-python@v2
36+
with:
37+
python-version: '3.9'
38+
39+
- name: Install dependencies
40+
run: |
41+
python -m pip install --upgrade pip
42+
pip install setuptools wheel twine toml build
43+
44+
- name: Update pyproject.toml
45+
run: |
46+
python -c "\
47+
import toml; \
48+
pyproject = toml.load('pyproject.toml'); \
49+
pyproject['project']['version'] = '${{ steps.latest_tag.outputs.tag }}'; \
50+
toml.dump(pyproject, open('pyproject.toml', 'w'))"
51+
52+
- name: Build and publish
53+
env:
54+
TWINE_USERNAME: __token__
55+
TWINE_PASSWORD: ${{ secrets.PYPI_API_TOKEN }}
56+
run: |
57+
python -m build
58+
twine upload dist/*

.github/workflows/release.yaml

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
name: Release Workflow
2+
3+
on:
4+
workflow_dispatch:
5+
inputs:
6+
releaseType:
7+
description: 'Release Type'
8+
required: true
9+
default: 'patch'
10+
type: choice
11+
options:
12+
- major
13+
- minor
14+
- patch
15+
draft:
16+
description: 'Create a draft release'
17+
required: false
18+
default: false
19+
type: boolean
20+
prerelease:
21+
description: 'Mark release as a prerelease'
22+
required: false
23+
default: false
24+
type: boolean
25+
releaseMessage:
26+
description: 'Release Message'
27+
required: false
28+
default: ''
29+
type: string
30+
31+
jobs:
32+
release:
33+
runs-on: ubuntu-latest
34+
steps:
35+
- name: Checkout repository
36+
uses: actions/checkout@v3
37+
38+
- name: Set up Node.js
39+
uses: actions/setup-node@v3
40+
with:
41+
node-version: '14'
42+
43+
- name: Fetch tags
44+
run: git fetch --depth=1 --tags
45+
46+
- name: Get latest tag or set default
47+
id: latest_tag
48+
run: |
49+
TAG=$(git tag -l --sort=-v:refname | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | head -n 1)
50+
if [ -z "$TAG" ]; then
51+
TAG="0.0.0"
52+
fi
53+
echo "::set-output name=tag::$TAG"
54+
55+
- name: Create dummy package.json
56+
run: |
57+
echo "{\"version\": \"${{ steps.latest_tag.outputs.tag }}\"}" > package.json
58+
cat package.json
59+
60+
- name: Calculate next version
61+
id: next_version
62+
run: |
63+
npm --no-git-tag-version version ${{ github.event.inputs.releaseType }}
64+
NEXT_VERSION=$(node -p "require('./package.json').version")
65+
echo "Next version: $NEXT_VERSION"
66+
echo "::set-output name=version::$NEXT_VERSION"
67+
68+
- name: Create Git tag
69+
run: git tag ${{ steps.next_version.outputs.version }}
70+
71+
- name: Push Git tag
72+
env:
73+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
74+
run: |
75+
git push origin ${{ steps.next_version.outputs.version }}
76+
77+
- name: Create Release
78+
uses: actions/create-release@v1
79+
env:
80+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
81+
with:
82+
tag_name: ${{ steps.next_version.outputs.version }}
83+
release_name: Release v${{ steps.next_version.outputs.version }}
84+
body: ${{ github.event.inputs.releaseMessage }}
85+
draft: ${{ github.event.inputs.draft }}
86+
prerelease: ${{ github.event.inputs.prerelease }}

0 commit comments

Comments
 (0)