-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Creating the GH deployment through action, finding ID in lagoon.yml
- Loading branch information
Showing
4 changed files
with
61 additions
and
52 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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/[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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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\"}" |