diff --git a/.github/workflows/lagoon.yml b/.github/workflows/lagoon.yml index 90ec2fbae..133cdf679 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,34 @@ 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: | + LAGOON_DEPLOYS_LOG_URL=$(echo "https://ui.lagoon.dplplat01.dpl.reload.dk/projects/dpl-cms/dpl-cms-${{ env.LAGOON_ENVIRONMENT }}/deployments") + + 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') + + gh api --method POST "/repos/${{ env.OWNER }}/${{ env.REPO }}/deployments/$DEPLOYMENT_ID/statuses" \ + -f "state=in_progress" -f "log_url=$LAGOON_DEPLOYS_LOG_URL" + + 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..cb5b5bd87 100644 --- a/.lagoon.yml +++ b/.lagoon.yml @@ -12,23 +12,22 @@ 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." + ./dev-scripts/lagoon-set-gh-deploy-status.sh "in_progress" service: cli - run: name: If drupal is not installed @@ -124,22 +123,9 @@ 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'" - - # Setting the deployment status, on the GH deployment created earlier. - 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\", \ - \"environment_url\":\"$DRUPAL_URL\", \"log_url\":\"$LAGOON_DEPLOYS_LOG_URL\"}" + ./dev-scripts/lagoon-set-gh-deploy-status.sh "success" "$DRUPAL_URL" service: cli environments: diff --git a/dev-scripts/lagoon-error-handling.sh b/dev-scripts/lagoon-error-handling.sh index 1e2362c68..394ae1e0d 100755 --- a/dev-scripts/lagoon-error-handling.sh +++ b/dev-scripts/lagoon-error-handling.sh @@ -4,16 +4,7 @@ # along the failing details to the GH deployment. error_handler() { - DEPLOYMENT_STATUS="failure" - 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'" - - 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\",\"log_url\":\"$LAGOON_DEPLOYS_LOG_URL\",\"state\":\"$DEPLOYMENT_STATUS\"}" + ./dev-scripts/lagoon-set-gh-deploy-status.sh "failure" # As we've gotten rid of set -e in lagoon.yml, it is very important to exit # here, otherwise, lagoon will think a faulty deploy has succeeded. diff --git a/dev-scripts/lagoon-set-gh-deploy-status.sh b/dev-scripts/lagoon-set-gh-deploy-status.sh new file mode 100755 index 000000000..cf9d97de9 --- /dev/null +++ b/dev-scripts/lagoon-set-gh-deploy-status.sh @@ -0,0 +1,22 @@ +#!/bin/bash + +# Ensure the required arguments are passed +if [ "$#" -ne 1 ]; then + echo "Usage: $0 " + exit 1 +fi + +# Assign the state passed as an argument +STATE=$1 +DRUPAL_URL=${2:-""} + +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': '$STATE'" + +# GitHub API request to update 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\":\"$STATE\", \ + \"environment_url\":\"$DRUPAL_URL\", \"log_url\":\"$LAGOON_DEPLOYS_LOG_URL\"}"