Skip to content

Commit 4c58c91

Browse files
authored
INTPYTHON-594 Add automated release workflow (#196)
1 parent c34f094 commit 4c58c91

File tree

2 files changed

+151
-68
lines changed

2 files changed

+151
-68
lines changed

.github/workflows/dist-python.yml

+75
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
name: Python Dist
2+
3+
on:
4+
push:
5+
tags:
6+
- "[0-9]+.[0-9]+.[0-9]+"
7+
- "[0-9]+.[0-9]+.[0-9]+.post[0-9]+"
8+
- "[0-9]+.[0-9]+.[0-9]+[a-b][0-9]+"
9+
- "[0-9]+.[0-9]+.[0-9]+rc[0-9]+"
10+
workflow_dispatch:
11+
pull_request:
12+
workflow_call:
13+
inputs:
14+
ref:
15+
required: true
16+
type: string
17+
18+
concurrency:
19+
group: dist-${{ github.ref }}
20+
cancel-in-progress: true
21+
22+
defaults:
23+
run:
24+
shell: bash -eux {0}
25+
26+
jobs:
27+
make_dist:
28+
name: Make Dist
29+
runs-on: macos-latest
30+
steps:
31+
- uses: actions/checkout@v4
32+
with:
33+
persist-credentials: false
34+
35+
- uses: actions/setup-python@v5
36+
with:
37+
# Build sdist on lowest supported Python
38+
python-version: '3.9'
39+
40+
- name: Install python requirements
41+
run: |
42+
python -m pip install uv rust-just build twine
43+
44+
- name: Build Dist
45+
run: |
46+
python -m build .
47+
48+
- name: Test SDist
49+
run: |
50+
python -m twine check --strict dist/*.*
51+
python -m pip install dist/*.gz
52+
cd ..
53+
python -c "from flask_pymongo import PyMongo"
54+
55+
- uses: actions/upload-artifact@v4
56+
with:
57+
name: "dist"
58+
path: ./dist/*.*
59+
60+
collect_dist:
61+
runs-on: ubuntu-latest
62+
needs: [make_dist]
63+
name: Download Dist
64+
steps:
65+
- name: Download all workflow run artifacts
66+
uses: actions/download-artifact@v4
67+
- name: Flatten directory
68+
working-directory: .
69+
run: |
70+
find . -mindepth 2 -type f -exec mv {} . \;
71+
find . -type d -empty -delete
72+
- uses: actions/upload-artifact@v4
73+
with:
74+
name: all-dist-${{ github.run_id }}
75+
path: "./*"

.github/workflows/release-python.yml

+76-68
Original file line numberDiff line numberDiff line change
@@ -1,99 +1,107 @@
1-
name: Python Wheels
1+
name: Release
22

33
on:
4-
push:
5-
branches: ["main"]
6-
tags:
7-
- "**"
8-
pull_request:
94
workflow_dispatch:
5+
inputs:
6+
following_version:
7+
description: "The post (dev) version to set"
8+
dry_run:
9+
description: "Dry Run?"
10+
default: false
11+
type: boolean
12+
schedule:
13+
- cron: '30 5 * * *'
14+
15+
env:
16+
# Changes per repo
17+
PRODUCT_NAME: Flask-PyMongo
18+
# Constant
19+
# inputs will be empty on a scheduled run. so, we only set dry_run
20+
# to 'false' when the input is set to 'false'.
21+
DRY_RUN: ${{ ! contains(inputs.dry_run, 'false') }}
22+
FOLLOWING_VERSION: ${{ inputs.following_version || '' }}
1023

1124
concurrency:
12-
group: wheels-${{ github.ref }}
25+
group: release-${{ github.ref }}
1326
cancel-in-progress: true
1427

1528
defaults:
1629
run:
1730
shell: bash -eux {0}
1831

1932
jobs:
20-
21-
build_dist:
22-
name: Build Distribution Files
23-
runs-on: ubuntu-latest
24-
steps:
25-
- uses: actions/checkout@v4
26-
with:
27-
fetch-depth: 0
28-
persist-credentials: false
29-
30-
- uses: actions/setup-python@v5
31-
with:
32-
# Build sdist on lowest supported Python
33-
python-version: '3.9'
34-
35-
- name: Install build
36-
run: |
37-
python -m pip install build
38-
39-
- name: build the dist files
40-
run: |
41-
python -m build .
42-
43-
- name: Upload the dist files
44-
uses: actions/upload-artifact@v4
45-
with:
46-
name: dist-${{ github.run_id }}
47-
path: ./dist/*.*
48-
49-
test_dist:
50-
needs: [build_dist]
51-
name: Test Distribution Files
33+
pre-publish:
34+
environment: release
5235
runs-on: ubuntu-latest
36+
if: github.repository_owner == 'mongodb-labs' || github.event_name == 'workflow_dispatch'
37+
permissions:
38+
id-token: write
39+
contents: write
40+
outputs:
41+
version: ${{ steps.pre-publish.outputs.version }}
5342
steps:
54-
- uses: actions/checkout@v4
43+
- uses: mongodb-labs/drivers-github-tools/secure-checkout@v2
5544
with:
56-
fetch-depth: 0
57-
persist-credentials: false
58-
59-
- uses: actions/setup-python@v5
45+
app_id: ${{ vars.APP_ID }}
46+
private_key: ${{ secrets.APP_PRIVATE_KEY }}
47+
- uses: mongodb-labs/drivers-github-tools/setup@v2
6048
with:
61-
# Build sdist on lowest supported Python
62-
python-version: '3.9'
63-
64-
- name: Download the dists
65-
uses: actions/download-artifact@v4
49+
aws_role_arn: ${{ secrets.AWS_ROLE_ARN }}
50+
aws_region_name: ${{ vars.AWS_REGION_NAME }}
51+
aws_secret_id: ${{ secrets.AWS_SECRET_ID }}
52+
artifactory_username: ${{ vars.ARTIFACTORY_USERNAME }}
53+
- uses: mongodb-labs/drivers-github-tools/python-labs/pre-publish@v2
54+
id: pre-publish
6655
with:
67-
name: dist-${{ github.run_id }}
68-
path: dist/
69-
70-
- name: Test the sdist
71-
run: |
72-
cd dist
73-
pip install *.tar.gz
74-
python -c "import flask_pymongo"
75-
pip uninstall -y flask_pymongo
56+
dry_run: ${{ env.DRY_RUN }}
7657

77-
- name: Test the wheel
78-
run: |
79-
cd dist
80-
pip install *.whl
81-
python -c "import flask_pymongo"
82-
pip uninstall -y flask_pymongo
58+
build-dist:
59+
needs: [pre-publish]
60+
uses: ./.github/workflows/dist-python.yml
61+
with:
62+
ref: ${{ needs.pre-publish.outputs.version }}
8363

8464
publish:
8565
# https://packaging.python.org/en/latest/guides/publishing-package-distribution-releases-using-github-actions-ci-cd-workflows/#publishing-the-distribution-to-pypi
86-
needs: [test_dist]
87-
if: startsWith(github.ref, 'refs/tags/')
66+
needs: [build-dist]
67+
if: (github.repository_owner == 'mongodb-labs' && github.event_name != 'pull_request') || github.event_name == 'workflow_dispatch'
8868
runs-on: ubuntu-latest
8969
environment: release
9070
permissions:
9171
id-token: write
9272
steps:
93-
- name: Download the dists
73+
- name: Download all the dists
9474
uses: actions/download-artifact@v4
9575
with:
96-
name: dist-${{ github.run_id }}
76+
name: all-dist-${{ github.run_id }}
9777
path: dist/
9878
- name: Publish distribution 📦 to PyPI
79+
if: startsWith(github.ref, 'refs/tags/')
9980
uses: pypa/gh-action-pypi-publish@release/v1
81+
82+
post-publish:
83+
needs: [publish]
84+
runs-on: ubuntu-latest
85+
environment: release
86+
permissions:
87+
id-token: write
88+
contents: write
89+
attestations: write
90+
security-events: write
91+
steps:
92+
- uses: mongodb-labs/drivers-github-tools/secure-checkout@v2
93+
with:
94+
app_id: ${{ vars.APP_ID }}
95+
private_key: ${{ secrets.APP_PRIVATE_KEY }}
96+
- uses: mongodb-labs/drivers-github-tools/setup@v2
97+
with:
98+
aws_role_arn: ${{ secrets.AWS_ROLE_ARN }}
99+
aws_region_name: ${{ vars.AWS_REGION_NAME }}
100+
aws_secret_id: ${{ secrets.AWS_SECRET_ID }}
101+
artifactory_username: ${{ vars.ARTIFACTORY_USERNAME }}
102+
- uses: mongodb-labs/drivers-github-tools/python-labs/post-publish@v2
103+
with:
104+
following_version: ${{ env.FOLLOWING_VERSION }}
105+
product_name: ${{ env.PRODUCT_NAME }}
106+
token: ${{ github.token }}
107+
dry_run: ${{ env.DRY_RUN }}

0 commit comments

Comments
 (0)