From 792e6e60e4ac0a021feea01b2921d418bcc8c270 Mon Sep 17 00:00:00 2001 From: Benjamin Rasmussen Date: Thu, 3 Oct 2024 13:59:20 +0200 Subject: [PATCH] Creating the GH deployment through action, finding ID in lagoon.yml --- .github/workflows/lagoon.yml | 38 ++++++++++++++++++++---------------- .lagoon.yml | 33 +++++++++++++++---------------- 2 files changed, 37 insertions(+), 34 deletions(-) diff --git a/.github/workflows/lagoon.yml b/.github/workflows/lagoon.yml index 90ec2fbae..2e6cd7dfa 100644 --- a/.github/workflows/lagoon.yml +++ b/.github/workflows/lagoon.yml @@ -4,9 +4,10 @@ on: name: Lagoon integration env: - LAGOON_HOST: "dplplat01.dpl.reload.dk" - LAGOON_PROJECT: "dpl-cms" - + LAGOON_ENVIRONMENT: "pr-${{github.event.number}}" + GH_TOKEN: ${{ secrets.GH_DEPLOYMENT_TOKEN }} + OWNER: ${{ github.repository_owner }} + REPO: ${{ github.event.repository.name }} jobs: BranchNameLength: name: Check branch length @@ -22,25 +23,28 @@ jobs: /^.{1,100}$/ errorMessage: 'Branch name too long. This cannot be deployed to Lagoon.' + # Creating the deployment that Lagoon will look for, and add status to. + CreateDeployment: + name: Creating deployment + runs-on: ubuntu-latest + if: ${{ github.event.action == 'opened' || github.event.action == 'reopened' }} + steps: + - run: | + DEPLOYMENT_ID=$(echo '{ + "ref": "${{ github.head_ref || github.ref_name }}", + "environment": "${{ env.LAGOON_ENVIRONMENT }}", + "auto_merge": false, + "required_contexts": [] + }' | gh api --method POST "/repos/${{ env.OWNER }}/${{ env.REPO }}/deployments" --input - --jq '.id') + echo "deployment_id=$DEPLOYMENT_ID" >> $GITHUB_OUTPUT + + # When we close the pull request, we want to set the environment as inactive. CloseEnvironment: name: Close environment runs-on: ubuntu-latest if: ${{ github.event.action == 'closed' }} - permissions: - # Give the default GITHUB_TOKEN permission to close deployments. - deployments: write steps: - - name: Generate environment data - id: environment - run: | - echo ::set-output name=id::pr-${{github.event.number}} - - name: Close environment - uses: bobheadxi/deployments@v1.5.0 - with: - step: deactivate-env - token: ${{ secrets.GITHUB_TOKEN }} - env: ${{ steps.environment.outputs.id }} - debug: ${{ runner.debug && 'true' || 'false' }} + - run: gh api --method DELETE "/repos/${{ env.OWNER }}/${{ env.REPO }}/environments/${{ env.LAGOON_ENVIRONMENT }}" # We only permit the integration with Lagoon to run if the user is # authorized. This saves on resources and ensures we only spin up sites for diff --git a/.lagoon.yml b/.lagoon.yml index 59d5c8d40..04326dfe8 100644 --- a/.lagoon.yml +++ b/.lagoon.yml @@ -12,23 +12,25 @@ tasks: - run: name: Create new GH deployment command: | - GH_DEPLOYMENT=$(curl -L -X POST -H "Authorization: Bearer $GH_DEPLOYMENT_TOKEN" \ - https://api.github.com/repos/danskernesdigitalebibliotek/dpl-cms/deployments \ - -d "{\"ref\":\"$LAGOON_PR_HEAD_BRANCH\", \"environment\":\"$LAGOON_ENVIRONMENT\", \ - \"description\":\"Triggered by Lagoon\", \"transient_environment\": true, \ - \"auto_merge\": false, \"required_contexts\": []}") + GH_DEPLOYMENTS=$(curl -H "Authorization: Bearer $GH_DEPLOYMENT_TOKEN" \ + "https://api.github.com/repos/danskernesdigitalebibliotek/dpl-cms/deployments?ref=$LAGOON_PR_HEAD_BRANCH&environment=$LAGOON_ENVIRONMENT") + GH_DEPLOYMENT_ID=$(echo "$GH_DEPLOYMENTS" | grep '"id":' | head -n 1 | sed 's/[^0-9]*\([0-9]*\).*/\1/') - echo "$GH_DEPLOYMENT" - GH_DEPLOYMENT_ID=$(echo "$GH_DEPLOYMENT" | grep -m 1 '"id":' | sed -E 's/.*"id": ([0-9]+),.*/\1/') - - echo "Created GH deployment with ID '$GH_DEPLOYMENT_ID'" + echo "$GH_DEPLOYMENTS" + echo "Found GH deployment with ID '$GH_DEPLOYMENT_ID'" # The only way to keep a value between tasks, is saving it in a # file. Environment variables are killed between tasks, apart from # the global ones. echo "$GH_DEPLOYMENT_ID" > /tmp/GH_DEPLOYMENT_ID - LAGOON_DEPLOYS_LOG_URL=$(echo "https://ui.lagoon.dplplat01.dpl.reload.dk/projects/$LAGOON_PROJECT/dpl-cms-$LAGOON_ENVIRONMENT/deployments") - echo "$LAGOON_DEPLOYS_URL" > /tmp/LAGOON_DEPLOYS_LOG_URL + LAGOON_DEPLOYS_LOG_URL="https://ui.lagoon.dplplat01.dpl.reload.dk/projects/$LAGOON_PROJECT/dpl-cms-$LAGOON_ENVIRONMENT/deployments" + echo "$LAGOON_DEPLOYS_LOG_URL" > /tmp/LAGOON_DEPLOYS_LOG_URL + + + echo "Creating a initial pending deployment status." + curl -L -X POST -H "Authorization: Bearer $GH_DEPLOYMENT_TOKEN" \ + https://api.github.com/repos/danskernesdigitalebibliotek/dpl-cms/deployments/$GH_DEPLOYMENT_ID/statuses \ + -d "{\"environment\":\"$LAGOON_ENVIRONMENT\",\"state\":\"in_progress\", \"log_url\":\"$LAGOON_DEPLOYS_LOG_URL\"}" service: cli - run: name: If drupal is not installed @@ -124,21 +126,18 @@ tasks: - run: name: Setting Deployment status success command: | - DEPLOYMENT_STATUS="success" DRUPAL_URL=$(drush browse) # Read the deployment ID from the file GH_DEPLOYMENT_ID=$(cat /tmp/GH_DEPLOYMENT_ID) LAGOON_DEPLOYS_LOG_URL=$(cat /tmp/LAGOON_DEPLOYS_LOG_URL) - echo "Setting GH deployment status '$GH_DEPLOYMENT_ID': '$DEPLOYMENT_STATUS'" + echo "Setting success GH deployment status '$GH_DEPLOYMENT_ID'" # Setting the deployment status, on the GH deployment created earlier. - curl -L \ - -X POST \ - -H "Authorization: Bearer $GH_DEPLOYMENT_TOKEN" \ + curl -L -X POST -H "Authorization: Bearer $GH_DEPLOYMENT_TOKEN" \ https://api.github.com/repos/danskernesdigitalebibliotek/dpl-cms/deployments/$GH_DEPLOYMENT_ID/statuses \ - -d "{\"environment\":\"$LAGOON_ENVIRONMENT\",\"state\":\"$DEPLOYMENT_STATUS\", \ + -d "{\"environment\":\"$LAGOON_ENVIRONMENT\",\"state\":\"success\", \ \"environment_url\":\"$DRUPAL_URL\", \"log_url\":\"$LAGOON_DEPLOYS_LOG_URL\"}" service: cli