From c6be2543e5e4b018e5eb624d5023ab4bf9120914 Mon Sep 17 00:00:00 2001 From: Ramez Weissa Date: Wed, 21 Aug 2024 13:23:48 +0300 Subject: [PATCH] feat: trigger deploying to control-plane review on opening a PR --- .../deploy-to-control-plane-review.yml | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/.github/workflows/deploy-to-control-plane-review.yml b/.github/workflows/deploy-to-control-plane-review.yml index 435c7ab3..f332b750 100644 --- a/.github/workflows/deploy-to-control-plane-review.yml +++ b/.github/workflows/deploy-to-control-plane-review.yml @@ -6,19 +6,21 @@ name: Deploy Review App to Control Plane on: # Allows you to run this workflow manually from the Actions tab workflow_dispatch: -# inputs: -# pr_number: -# description: 'Pull request number' -# required: false -# Triggers the workflow on pull request events -# pull_request: -# branches: -# - main # or the branch you want to trigger the workflow + # inputs: + # pr_number: + # description: 'Pull request number' + # required: false + + # Triggers the workflow on pull request events + pull_request: + branches: + - master # Convert the GitHub secret variables to environment variables for use by the Control Plane CLI env: CPLN_ORG: ${{secrets.CPLN_ORG_STAGING}} CPLN_TOKEN: ${{secrets.CPLN_TOKEN_STAGING}} + PR_NUMBER: ${{github.event.number}} jobs: deploy-to-control-plane-review: @@ -31,13 +33,15 @@ jobs: - name: Get PR number run: | echo "GITHUB_REPOSITORY: \"$GITHUB_REPOSITORY\"" - REF=${{ github.ref }} - REF=${REF#refs/heads/} # Remove 'refs/heads/' prefix - echo "REF: \"$REF\"" - API_RESPONSE=$(curl --location --request GET "https://api.github.com/repos/$GITHUB_REPOSITORY/pulls?state=open" \ - --header 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}') - echo "API_RESPONSE: $API_RESPONSE" - PR_NUMBER=$(echo $API_RESPONSE | jq '.[] | select(.head.ref=="'$REF'") | .number') + if [ -z "$PR_NUMBER" ]; then + echo "PR_NUMBER is not in the trigger event. Fetching PR number from open PRs." + REF=${{ github.ref }} + REF=${REF#refs/heads/} # Remove 'refs/heads/' prefix + echo "REF: \"$REF\"" + API_RESPONSE=$(curl --location --request GET "https://api.github.com/repos/$GITHUB_REPOSITORY/pulls?state=open" \ + --header 'Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}') + PR_NUMBER=$(echo $API_RESPONSE | jq '.[] | select(.head.ref=="'$REF'") | .number') + fi echo "PR_NUMBER: $PR_NUMBER" if [ -z "$PR_NUMBER" ]; then echo "PR_NUMBER is not set. Aborting." @@ -54,4 +58,4 @@ jobs: - uses: ./.github/actions/deploy-to-control-plane with: app_name: ${{ env.APP_NAME }} - org: ${{ secrets.CPLN_ORG_STAGING }} + org: ${{ env.CPLN_ORG }}