Skip to content

Upgrade one Python dependency #3

Upgrade one Python dependency

Upgrade one Python dependency #3

name: Upgrade one Python dependency
on:
workflow_dispatch:
inputs:
branch:
description: "Target branch to create requirements PR against"
required: true
default: "master"
type: string
package:
description: "Name of package to upgrade"
required: true
type: string
version:
description: "Version number to upgrade to in constraints.txt (only needed if pinned)"
default: ""
type: string
change_desc:
description: |
Description of change, for commit message and PR. (What does the new version add or fix?)
default: ""
type: string
defaults:
run:
shell: bash # making this explicit opts into -e -o pipefail
jobs:
upgrade-one-python-dependency:
runs-on: ubuntu-latest
steps:
- name: Check out target branch
uses: actions/checkout@v4
with:
ref: "${{ inputs.branch }}"
- name: Set up Python environment
uses: actions/setup-python@v5
with:
python-version: "3.11"
- name: Update any pinned dependencies
env:
NEW_VERSION: "${{ inputs.version }}"
PACKAGE: "${{ inputs.package }}"
run: |
sed 's/^\('$PACKAGE'[^#]*\)==[^ #]\+/\1=='$NEW_VERSION'/' -i requirements/constraints.txt
- name: Run make upgrade-package
env:
PACKAGE: "${{ inputs.package }}"
run: |
make upgrade-package package="$PACKAGE"
- name: PR preflight
env:
CHANGE_DESC: "${{ inputs.change_desc }}"
run: |
if git diff --exit-code; then
# Fail early (and avoid quiet failure of create-pull-request action)
echo "Error: No changes, so not creating PR." | tee -a "$GITHUB_STEP_SUMMARY"
exit 1
else
# There are changes to commit, so prep some info for the PR.
# This is honestly a lot to go through just to say "add two newlines
# on the end if the variable isn't empty" but I guess this is what we
# have to do to get a conditional delimiter.
if [[ -z "$CHANGE_DESC" ]]; then
echo "body_prefix=" >> "$GITHUB_ENV"
else
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64)
echo "body_prefix<<$EOF" >> "$GITHUB_ENV"
echo "$CHANGE_DESC" >> "$GITHUB_ENV"
echo "" >> "$GITHUB_ENV"
echo "" >> "$GITHUB_ENV"
echo "$EOF" >> "$GITHUB_ENV"
fi
fi
- name: Make a PR
id: make-pr
uses: peter-evans/create-pull-request@v6
with:
branch: "${{ github.triggering_actor }}/upgrade-${{ inputs.package }}"
branch-suffix: short-commit-hash
add-paths: |
requirements
scripts/**/requirements*
commit-message: |
feat: Upgrade Python dependency ${{ inputs.package }}
${{ env.body_prefix }}Commit generated by workflow `${{ github.workflow_ref }}`
title: "feat: Upgrade Python dependency ${{ inputs.package }}"
body: >-
${{ env.body_prefix }}PR generated by workflow `${{ github.workflow_ref }}`
on behalf of @${{ github.triggering_actor }}.
assignees: "${{ github.triggering_actor }}"
reviewers: "${{ github.triggering_actor }}"
- name: Job summary
env:
PR_URL: "${{ steps.make-pr.outputs.pull-request-url }}"
run: |
if [[ -z "$PR_URL" ]]; then
echo "PR not created; see log for more information" | tee -a "$GITHUB_STEP_SUMMARY"
exit 1
else
echo "PR created or updated: $PR_URL" | tee -a "$GITHUB_STEP_SUMMARY"
fi