Skip to content

Commit

Permalink
ci: add release preparation workflow (#203)
Browse files Browse the repository at this point in the history
  • Loading branch information
lkstrp authored Sep 16, 2024
1 parent 674ca1e commit 6bcc368
Showing 1 changed file with 96 additions and 43 deletions.
139 changes: 96 additions & 43 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,107 @@ on:
tags:
- v*.*.*

env:
PREPARATION_COMMIT: '[github-actions.ci] auto update powerplants.csv for release ${{ github.ref_name }}'

jobs:
check-preparation:
name: Check if release is prepared
runs-on: ubuntu-latest
outputs:
prepared: ${{ steps.validate.outputs.prepared }}
steps:
- uses: actions/checkout@v3

- name: Validate commit message
id: validate
run: |
# Check if last commit is the expected commit message
COMMIT_MESSAGE=$(git log -1 --pretty=%B)
echo "Expected: '${{ env.PREPARATION_COMMIT }}'"
echo "Received: '$COMMIT_MESSAGE'"
prepared="false"
if [[ "$COMMIT_MESSAGE" == "${{ env.PREPARATION_COMMIT }}" ]]; then
prepared="true"
fi
echo "prepared=$prepared" >> $GITHUB_OUTPUT
prepare-release:
name: Prepare release
needs: [check-preparation]
if: ${{ needs.check-preparation.outputs.prepared == 'false' }}
runs-on: ubuntu-latest
steps:
- name: Generate token for PyPSA Bot
id: generate-token
uses: actions/create-github-app-token@v1
with:
app-id: ${{ vars.PYPSA_BOT_ID }}
private-key: ${{ secrets.PYPSA_BOT_PRIVATE_KEY }}

- uses: actions/checkout@v4
with:
fetch-depth: 0

- name: Find the branch for commit/ tag
run: |
branch=$(git branch -r --contains ${{ github.sha }} | grep -v 'HEAD' | head -n 1 | sed 's|origin/||' | sed 's/^[[:space:]]*//;s/[[:space:]]*$//')
echo "Branch found: $branch"
echo "BRANCH_NAME=$branch" >> $GITHUB_ENV
- uses: actions/checkout@v4
with:
fetch-depth: 0
ref: ${{ env.BRANCH_NAME }}
token: ${{ steps.generate-token.outputs.token }}

# Start of preparation script

- uses: actions/setup-python@v5
with:
python-version: 3.12

- name: Install package
run: |
python -m pip install uv
uv pip install --system ".[dev]"
- name: Create dataset
run: |
import powerplantmatching as pm
df = pm.powerplants(update=True)
df.to_csv("powerplants.csv", index_label="id")
shell: python

# End of preparation script

- name: Remove previous tag
run: |
git tag -d ${{ github.ref_name }}
git push origin --delete ${{ github.ref_name }}
- name: Commit changes
uses: stefanzweifel/git-auto-commit-action@v5
with:
branch: ${{ env.BRANCH_NAME }}
commit_message: '${{ env.PREPARATION_COMMIT }}'
file_pattern: 'powerplants.csv'
tagging_message: '${{ github.ref_name }}'
push_options: '${{ github.ref_name }}'

build:
# Build the Python SDist and wheel, performs metadata and readme linting
name: Build and verify package
needs: [check-preparation]
if: ${{ needs.check-preparation.outputs.prepared == 'true' }}
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: hynek/build-and-inspect-python-package@v2

release:
# Publish a GitHub release from the given git tag
name: Create GitHub Release
name: Create GitHub release
needs: [build]
runs-on: ubuntu-latest
steps:
Expand All @@ -29,7 +118,6 @@ jobs:
generate_release_notes: true

publish:
# Publish the built SDist and wheel from "dist" job to PyPI
name: Publish to PyPI
needs: [build]
runs-on: ubuntu-latest
Expand All @@ -39,43 +127,8 @@ jobs:
permissions:
id-token: write
steps:
- uses: actions/download-artifact@v4
with:
name: Packages
path: dist
- uses: pypa/gh-action-pypi-publish@release/v1

update-dataset:
name: Update powerplants.csv in repository
needs: [build]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0 # Needed for setuptools_scm

- name: Set up Python
uses: actions/setup-python@v5
with:
python-version: 3.12

- name: Install package
run: |
python -m pip install uv
uv pip install --system "$(ls dist/*.whl)[dev]"
- name: Create dataset
run: |
import powerplantmatching as pm
df = pm.powerplants(update=True)
df.to_csv("powerplants.csv", index_label="id")
shell: python

- name: Create Pull Request
uses: peter-evans/create-pull-request@v6
- uses: actions/download-artifact@v4
with:
commit-message: '[github-actions.ci] auto update `powerplants.csv`'
title: 'Update `powerplants.csv`'
body: 'This PR updates the `powerplants.csv` dataset after a new release.'
branch: 'auto-update-powerplants-csv'
base: 'master'
name: Packages
path: dist
- uses: pypa/gh-action-pypi-publish@release/v1

0 comments on commit 6bcc368

Please sign in to comment.