From 3ca962e6d798ea32472322a1600390993b9a253f Mon Sep 17 00:00:00 2001 From: Mikkel Jakobsen Date: Fri, 17 May 2024 17:55:10 +0200 Subject: [PATCH] Add gh workflow functionality that creates pr on branch changes --- .github/workflows/create-cms-pr.yml | 65 +++++++++++++++++++ .../create-release-on-branch-changes.yml | 10 +++ 2 files changed, 75 insertions(+) create mode 100644 .github/workflows/create-cms-pr.yml diff --git a/.github/workflows/create-cms-pr.yml b/.github/workflows/create-cms-pr.yml new file mode 100644 index 000000000..101b77595 --- /dev/null +++ b/.github/workflows/create-cms-pr.yml @@ -0,0 +1,65 @@ +name: Create CMS PR +on: + workflow_run: + workflows: ["Create release on branch changes"] + types: + - completed + +jobs: + invoke-external-pr-workflow: + runs-on: ubuntu-latest + steps: + - uses: actions/download-artifact@v4 + with: + name: release-info.json + github-token: ${{ secrets.GITHUB_TOKEN }} + run-id: ${{ github.event.workflow_run.id }} + + - name: Set env variables + run: | + RELEASE_BRANCH=$(jq -r '.release_branch' release-info.json) + RELEASE_DOWNLOAD_URL=$(jq -r '.release_download_url' release-info.json) + DEPENDENCY_PACKAGE=$(jq -r '.dependency_package' release-info.json) + echo "RELEASE_BRANCH=$RELEASE_BRANCH" >> $GITHUB_ENV + echo "RELEASE_DOWNLOAD_URL=$RELEASE_DOWNLOAD_URL" >> $GITHUB_ENV + echo "DEPENDENCY_PACKAGE=$DEPENDENCY_PACKAGE" >> $GITHUB_ENV + + - name: Invoke external PR workflow + id: invoke_external_pr_workflow + uses: fjogeleit/http-request-action@v1 + with: + url: ${{ env.REQUEST_URL}} + method: 'POST' + customHeaders: | + { + "Accept": "application/vnd.github+json", + "Authorization": "Bearer ${{ secrets.CMS_PR_CREATION_PAT }}" + } + data: | + { + "event_type": "create_pr", + "client_payload": { + "branch": "${{ env.RELEASE_BRANCH }}", + "build_url": "${{ env.RELEASE_DOWNLOAD_URL }}", + "dependency_package": "${{ env.DEPENDENCY_PACKAGE }}" + } + } + env: + REQUEST_URL: ${{ format('https://api.github.com/repos/{0}/dispatches', vars.REMOTE_REPO_GITHUB_HANDLE) }} + # Only invoke external PR workflow if the release branch is not develop or master + if: ${{ !contains(fromJSON('["develop", "master"]'), env.RELEASE_BRANCH) }} + + - name: Adding summary + run: | + echo "Requested external PR at: ${REMOTE_REPO_GITHUB_HANDLE}" >> $GITHUB_STEP_SUMMARY + echo "Based on the [${{ env.RELEASE_BRANCH }}](${{ env.BRANCH_URL }}) branch" >> $GITHUB_STEP_SUMMARY + if: ${{ steps.invoke_external_pr_workflow.outcome == 'success' }} + env: + BRANCH_URL: "${{ github.event.repository.html_url }}/tree/${{ env.RELEASE_BRANCH }}" + REMOTE_REPO_GITHUB_HANDLE: "${{ vars.REMOTE_REPO_GITHUB_HANDLE }}" + - name: Adding summary about skip if branch is develop or master + # TODO: remove hardcoded org/repo + run: | + echo "Skipped PR creation because the branch was: $RELEASE_BRANCH " >> $GITHUB_STEP_SUMMARY + echo "...which is not considered being a release branch. " >> $GITHUB_STEP_SUMMARY + if: ${{ contains(fromJSON('["develop", "master"]'), env.RELEASE_BRANCH) }} diff --git a/.github/workflows/create-release-on-branch-changes.yml b/.github/workflows/create-release-on-branch-changes.yml index 8574a7d27..6ed3e26b2 100644 --- a/.github/workflows/create-release-on-branch-changes.yml +++ b/.github/workflows/create-release-on-branch-changes.yml @@ -85,6 +85,16 @@ jobs: body: ${{ env.RELEASE_DESCRIPTION }} files: ${{ env.DIST_FILENAME }} + - name: Write release information in artifact file + run: | + printf '{ + "release_branch": "${{ env.RELEASE_BRANCH }}", + "dependency_package": "danskernesdigitalebibliotek/dpl-design-system", + "release_download_url": "${{ env.DOWNLOAD_URL }}" + }' >> release-info.json + env: + DOWNLOAD_URL: "${{ github.event.repository.html_url }}/releases/download/${{ env.TAG_NAME }}/${{ env.DIST_FILENAME }}" + - name: Adding summary run: | echo "Release created 🚀😎 at: ${{ steps.create-release.outputs.url }}" >> $GITHUB_STEP_SUMMARY