Create Release PR #92
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Create Release PR | |
on: | |
workflow_dispatch: | |
inputs: | |
release_version: | |
description: 'The release_version used for the release branch name, e.g. release/vx.x.x' | |
default: 'vx.x.x' | |
required: true | |
type: string | |
base_branch: | |
description: "The base branch to release from; only override this if preparing a release off of the non-default branch" | |
default: 'main' | |
required: true | |
type: string | |
env: | |
RELEASE_VERSION: ${{ inputs.release_version }} | |
RELEASE_BRANCH: release/${{ inputs.release_version }} | |
jobs: | |
create-release-pr: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Set Release Version and Branch to Check Out | |
id: set-release | |
run: | | |
if [[ $RELEASE_VERSION =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then | |
echo "release-tag: $RELEASE_VERSION" | |
echo "release-tag=$RELEASE_VERSION" >> $GITHUB_OUTPUT | |
else | |
echo "Version input doesn't match the regex pattern ^v[0-9]+\.[0-9]+\.[0-9]+$" | |
exit 1 | |
fi | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
ref: ${{ inputs.base_branch }} | |
- name: Create Release Branch if it does not exist | |
run: | | |
if ! git show-ref --verify --quiet "refs/remotes/origin/$RELEASE_BRANCH"; then | |
git checkout -b $RELEASE_BRANCH | |
git push --set-upstream origin $RELEASE_BRANCH | |
elif [[ $(git rev-parse --abbrev-ref HEAD) != "$RELEASE_BRANCH" ]]; then | |
echo "Current Branch: $(git rev-parse --abbrev-ref HEAD)" | |
echo "Release branch exists, make sure you're using the workflow from the release branch or delete the existing release branch." | |
exit 1 | |
else | |
echo "Release branch exists and used as workflow ref." | |
fi | |
- name: Get Latest Release | |
id: get-release | |
run: | | |
echo "Get the latest stable release" | |
tag=$(curl -L \ | |
--header "Accept: application/vnd.github.v3+json" \ | |
"https://api.github.com/repos/${{ github.repository }}/releases/latest" | jq -r '.tag_name') | |
echo "latest-tag=$tag" >> $GITHUB_OUTPUT | |
- name: Build Changelog | |
id: build-changelog | |
env: | |
PREVIOUS_VERSION: ${{ steps.get-release.outputs.latest-tag }} | |
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }} | |
run: | | |
CHANGELOG=$(curl -L \ | |
-X POST \ | |
-H "Accept: application/vnd.github+json" \ | |
-H "Authorization: Bearer ${{ github.token }}"\ | |
-H "X-GitHub-Api-Version: 2022-11-28" \ | |
https://api.github.com/repos/${{ github.repository }}/releases/generate-notes \ | |
-d '{"tag_name":"${{ env.RELEASE_VERSION }}","target_commitish":"${{ env.RELEASE_BRANCH }}","previous_tag_name":"${{ env.PREVIOUS_VERSION }}","configuration_file_path":".github/release.yml"}' \ | |
| jq -r '.body') | |
# The EOF steps are used to save multiline string in github: | |
# https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#example-of-a-multiline-string | |
EOF=$(dd if=/dev/urandom bs=15 count=1 status=none | base64) | |
echo "changelog<<$EOF" >> $GITHUB_OUTPUT | |
echo -e "${CHANGELOG}" >> $GITHUB_OUTPUT | |
echo "$EOF" >> $GITHUB_OUTPUT | |
- name: Update Changelog | |
env: | |
CHANGELOG_CONTENT: ${{ steps.build-changelog.outputs.changelog }} | |
PREVIOUS_VERSION: ${{ steps.get-release.outputs.latest-tag }} | |
run: | | |
echo "$(tail -n +2 CHANGELOG.md)" > CHANGELOG.md | |
echo -e "# Changelog\n\n# ${RELEASE_VERSION}\n\n${CHANGELOG_CONTENT}" | cat - CHANGELOG.md > temp && mv temp CHANGELOG.md | |
- name: Update version in setup.py | |
env: | |
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }} | |
run: | | |
python3 scripts/bump_version.py ${RELEASE_TAG:1} | |
- name: Commit Changes | |
uses: EndBug/[email protected] | |
env: | |
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }} | |
with: | |
message: "bump up version to ${{ env.RELEASE_TAG }}" | |
- name: Create Pull Request to ${{ inputs.base_branch }} | |
env: | |
CHANGELOG_CONTENT: ${{ steps.build-changelog.outputs.changelog }} | |
PREVIOUS_VERSION: ${{ steps.get-release.outputs.latest-tag }} | |
GH_TOKEN: ${{ github.token }} | |
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }} | |
run: | | |
PULL_REQUEST_URL=$(gh pr create --base "${{ inputs.base_branch }}" \ | |
--title "FOR REVIEW ONLY: ${{ github.event.repository.name }} $RELEASE_TAG changelog and version bump" \ | |
--label "Skip-Release-Notes" \ | |
--body "${CHANGELOG_CONTENT}" | tail -n 1) | |
if [[ $PULL_REQUEST_URL =~ ^https://github.com/${{ github.repository }}/pull/[0-9]+$ ]]; then | |
PULL_REQUEST_NUM=$(echo $PULL_REQUEST_URL | sed 's:.*/::') | |
echo "pull-request-url=$PULL_REQUEST_URL" >> $GITHUB_ENV | |
echo "pull-request-num=$PULL_REQUEST_NUM" >> $GITHUB_ENV | |
echo "Pull request to ${{ inputs.base_branch }} created: $PULL_REQUEST_URL" | |
else | |
echo "There was an issue creating the pull request to ${{ inputs.base_branch }} branch." | |
exit 1 | |
fi | |
- name: Send Slack Message | |
id: slack | |
uses: slackapi/[email protected] | |
env: | |
RELEASE_TAG: ${{ steps.set-release.outputs.release-tag }} | |
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} | |
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK | |
RELEASE_WORKFLOW: "workflows/release.yml" | |
with: | |
payload: | | |
{ | |
"blocks": [ | |
{ | |
"type": "header", | |
"text": { | |
"type": "plain_text", | |
"text": "${{ github.event.repository.name }} Release PR for ${{ env.RELEASE_TAG }}" | |
} | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "*Approval needed for*:\nPull Request to ${{ inputs.base_branch }}: ${{ env.pull-request-url}}" | |
} | |
}, | |
{ | |
"type": "section", | |
"text": { | |
"type": "mrkdwn", | |
"text": "*After approval*\nRelease and Deploy SDK using the <${{ env.RELEASE_WORKFLOW }}> with the following parameters:\n*release_pr_number*: ${{ env.pull-request-num }}\n*release_version*: ${{ env.RELEASE_VERSION }}" | |
} | |
} | |
] | |
} |