Skip to content

Commit

Permalink
Creating the GH deployment through action, finding ID in lagoon.yml
Browse files Browse the repository at this point in the history
  • Loading branch information
rasben committed Oct 3, 2024
1 parent fbd530d commit 62a1063
Show file tree
Hide file tree
Showing 4 changed files with 61 additions and 52 deletions.
44 changes: 27 additions & 17 deletions .github/workflows/lagoon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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/[email protected]
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
Expand Down
36 changes: 11 additions & 25 deletions .lagoon.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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:
Expand Down
11 changes: 1 addition & 10 deletions dev-scripts/lagoon-error-handling.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
22 changes: 22 additions & 0 deletions dev-scripts/lagoon-set-gh-deploy-status.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#!/bin/bash

# Ensure the required arguments are passed
if [ "$#" -ne 1 ]; then
echo "Usage: $0 <state>"
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\"}"

0 comments on commit 62a1063

Please sign in to comment.