From bbe61460eb2a96823952daf89423a2beee22c5dc Mon Sep 17 00:00:00 2001 From: Ewan Harris Date: Fri, 15 Dec 2023 16:37:18 +0000 Subject: [PATCH] Move to automated relase workflow --- .../actions/actions/get-prerelease/action.yml | 30 +++++++++++ .../actions/get-release-notes/action.yml | 42 +++++++++++++++ .../actions/actions/get-version/action.yml | 21 ++++++++ .../actions/actions/release-create/action.yml | 47 +++++++++++++++++ .github/actions/actions/tag-exists/action.yml | 36 +++++++++++++ .github/workflows/publish.yml | 51 +++++++++++++++++-- .shiprc | 2 +- .version | 1 + 8 files changed, 226 insertions(+), 4 deletions(-) create mode 100644 .github/actions/actions/get-prerelease/action.yml create mode 100644 .github/actions/actions/get-release-notes/action.yml create mode 100644 .github/actions/actions/get-version/action.yml create mode 100644 .github/actions/actions/release-create/action.yml create mode 100644 .github/actions/actions/tag-exists/action.yml create mode 100644 .version diff --git a/.github/actions/actions/get-prerelease/action.yml b/.github/actions/actions/get-prerelease/action.yml new file mode 100644 index 00000000..131f93d1 --- /dev/null +++ b/.github/actions/actions/get-prerelease/action.yml @@ -0,0 +1,30 @@ +name: Return a boolean indicating if the version contains prerelease identifiers + +# +# Returns a simple true/false boolean indicating whether the version indicates it's a prerelease or not. +# +# TODO: Remove once the common repo is public. +# + +inputs: + version: + required: true + +outputs: + prerelease: + value: ${{ steps.get_prerelease.outputs.PRERELEASE }} + +runs: + using: composite + + steps: + - id: get_prerelease + shell: bash + run: | + if [[ "${VERSION}" == *"beta"* || "${VERSION}" == *"alpha"* ]]; then + echo "PRERELEASE=true" >> $GITHUB_OUTPUT + else + echo "PRERELEASE=false" >> $GITHUB_OUTPUT + fi + env: + VERSION: ${{ inputs.version }} \ No newline at end of file diff --git a/.github/actions/actions/get-release-notes/action.yml b/.github/actions/actions/get-release-notes/action.yml new file mode 100644 index 00000000..5ce3f92e --- /dev/null +++ b/.github/actions/actions/get-release-notes/action.yml @@ -0,0 +1,42 @@ +name: Return the release notes extracted from the PR body + +# +# Returns the release notes from the content of a pull request linked to a release branch. It expects the branch name to be in the format release/vX.Y.Z, release/X.Y.Z, release/vX.Y.Z-beta.N. etc. +# +# TODO: Remove once the common repo is public. +# +inputs: + version: + required: true + repo_name: + required: false + repo_owner: + required: true + token: + required: true + +outputs: + release-notes: + value: ${{ steps.get_release_notes.outputs.RELEASE_NOTES }} + +runs: + using: composite + + steps: + - uses: actions/github-script@v7 + id: get_release_notes + with: + result-encoding: string + script: | + const { data: pulls } = await github.rest.pulls.list({ + owner: process.env.REPO_OWNER, + repo: process.env.REPO_NAME, + state: 'all', + head: `${process.env.REPO_OWNER}:release/${process.env.VERSION}`, + }); + core.setOutput('RELEASE_NOTES', pulls[0].body); + env: + GITHUB_TOKEN: ${{ inputs.token }} + REPO_OWNER: ${{ inputs.repo_owner }} + REPO_NAME: ${{ inputs.repo_name }} + VERSION: ${{ inputs.version }} \ No newline at end of file diff --git a/.github/actions/actions/get-version/action.yml b/.github/actions/actions/get-version/action.yml new file mode 100644 index 00000000..84814a39 --- /dev/null +++ b/.github/actions/actions/get-version/action.yml @@ -0,0 +1,21 @@ +name: Return the version extracted from the branch name + +# +# Returns the version from the .version file. +# +# TODO: Remove once the common repo is public. +# + +outputs: + version: + value: ${{ steps.get_version.outputs.VERSION }} + +runs: + using: composite + + steps: + - id: get_version + shell: bash + run: | + VERSION=$(head -1 .version) + echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT \ No newline at end of file diff --git a/.github/actions/actions/release-create/action.yml b/.github/actions/actions/release-create/action.yml new file mode 100644 index 00000000..a0db443d --- /dev/null +++ b/.github/actions/actions/release-create/action.yml @@ -0,0 +1,47 @@ +name: Create a GitHub release + +# +# Creates a GitHub release with the given version. +# +# TODO: Remove once the common repo is public. +# + +inputs: + token: + required: true + files: + required: false + name: + required: true + body: + required: true + tag: + required: true + commit: + required: true + draft: + default: false + required: false + prerelease: + default: false + required: false + fail_on_unmatched_files: + default: true + required: false + +runs: + using: composite + + steps: + - uses: softprops/action-gh-release@de2c0eb89ae2a093876385947365aca7b0e5f844 + with: + body: ${{ inputs.body }} + name: ${{ inputs.name }} + tag_name: ${{ inputs.tag }} + target_commitish: ${{ inputs.commit }} + draft: ${{ inputs.draft }} + prerelease: ${{ inputs.prerelease }} + fail_on_unmatched_files: ${{ inputs.fail_on_unmatched_files }} + files: ${{ inputs.files }} + env: + GITHUB_TOKEN: ${{ inputs.token }} \ No newline at end of file diff --git a/.github/actions/actions/tag-exists/action.yml b/.github/actions/actions/tag-exists/action.yml new file mode 100644 index 00000000..b8f33f6a --- /dev/null +++ b/.github/actions/actions/tag-exists/action.yml @@ -0,0 +1,36 @@ +name: Return a boolean indicating if a tag already exists for the repository + +# +# Returns a simple true/false boolean indicating whether the tag exists or not. +# +# TODO: Remove once the common repo is public. +# + +inputs: + token: + required: true + tag: + required: true + +outputs: + exists: + description: 'Whether the tag exists or not' + value: ${{ steps.tag-exists.outputs.EXISTS }} + +runs: + using: composite + + steps: + - id: tag-exists + shell: bash + run: | + GET_API_URL="https://api.github.com/repos/${GITHUB_REPOSITORY}/git/ref/tags/${TAG_NAME}" + http_status_code=$(curl -LI $GET_API_URL -o /dev/null -w '%{http_code}\n' -s -H "Authorization: token ${GITHUB_TOKEN}") + if [ "$http_status_code" -ne "404" ] ; then + echo "EXISTS=true" >> $GITHUB_OUTPUT + else + echo "EXISTS=false" >> $GITHUB_OUTPUT + fi + env: + TAG_NAME: ${{ inputs.tag }} + GITHUB_TOKEN: ${{ inputs.token }} \ No newline at end of file diff --git a/.github/workflows/publish.yml b/.github/workflows/publish.yml index d65b6c2c..afa1545d 100644 --- a/.github/workflows/publish.yml +++ b/.github/workflows/publish.yml @@ -1,10 +1,13 @@ name: Publish Release on: - workflow_dispatch: - release: + pull_request: types: - - published + - closed + workflow_dispatch: + +### TODO: Replace instances of './.github/actions/' with reference to the `dx-sdk-actions` repo is made public and this file is transferred over +### TODO: Also remove `get-prerelease`, `get-version`, `release-create`, `tag-create` and `tag-exists` actions from this repo's .github/actions folder once the repo is public. permissions: contents: read @@ -12,6 +15,7 @@ permissions: jobs: publish-pypi: + if: github.event_name == 'workflow_dispatch' || (github.event_name == 'pull_request' && github.event.pull_request.merged && startsWith(github.event.pull_request.head.ref, 'release/')) name: "PyPI" runs-on: ubuntu-latest environment: release @@ -22,6 +26,37 @@ jobs: with: fetch-depth: 0 fetch-tags: true + + # Get the version from the branch name + - id: get_version + uses: ./.github/actions/get-version + + # Get the prerelease flag from the branch name + - id: get_prerelease + uses: ./.github/actions/get-prerelease + with: + version: ${{ steps.get_version.outputs.version }} + + # Get the release notes + # This will expose the release notes as env.RELEASE_NOTES + - id: get_release_notes + uses: ./.github/actions/get-release-notes + with: + token: ${{ secrets.github-token }} + version: ${{ steps.get_version.outputs.version }} + repo_owner: ${{ github.repository_owner }} + repo_name: ${{ github.event.repository.name }} + + # Check if the tag already exists + - id: tag_exists + uses: ./.github/actions/tag-exists + with: + tag: ${{ steps.get_version.outputs.version }} + token: ${{ secrets.github-token }} + + # If the tag already exists, exit with an error + - if: steps.tag_exists.outputs.exists == 'true' + run: exit 1 - name: Configure Python uses: actions/setup-python@v5 @@ -44,3 +79,13 @@ jobs: - name: Publish release uses: pypa/gh-action-pypi-publish@release/v1 + + # Create a release for the tag + - uses: ./.github/actions/release-create + with: + token: ${{ secrets.github-token }} + name: ${{ steps.get_version.outputs.version }} + body: ${{ steps.get_release_notes.outputs.release-notes }} + tag: ${{ steps.get_version.outputs.version }} + commit: ${{ github.sha }} + prerelease: ${{ steps.get_prerelease.outputs.prerelease }} \ No newline at end of file diff --git a/.shiprc b/.shiprc index c8b249d9..ce24dbdd 100644 --- a/.shiprc +++ b/.shiprc @@ -1,6 +1,6 @@ { "files": { - "auth0/__init__.py": [] + ".version": [] }, "prefixVersion": false } diff --git a/.version b/.version new file mode 100644 index 00000000..1163055e --- /dev/null +++ b/.version @@ -0,0 +1 @@ +4.7.0 \ No newline at end of file