Skip to content

Instantly share code, notes, and snippets.

@chadwcarlson
Last active August 10, 2022 15:43
Show Gist options
  • Save chadwcarlson/96d6c4cb8c6b371ab04e256554dde728 to your computer and use it in GitHub Desktop.
Save chadwcarlson/96d6c4cb8c6b371ab04e256554dde728 to your computer and use it in GitHub Desktop.
Updating a Platform.sh integration's `target_url` to display failed activity logs
---
name: Post-deploy
on:
push:
branches-ignore:
- 'master'
env:
PLATFORMSH_CLI_TOKEN: ${{ secrets.CLI_TOKEN }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
jobs:
build:
runs-on: ubuntu-latest
name: 'Get environment URL'
outputs:
commit_status: ${{ steps.status.outputs.env_status }}
env_url: ${{ steps.url.outputs.env_url }}
integration_status: ${{ steps.wait.outputs.integration }}
steps:
- uses: actions/checkout@v2
- name: 'Await deployment'
id: wait
run: |
COMMIT_STATUS="pending"
sleep 10
STATUSES=$(curl -s https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA | jq -r 'length')
if [ $STATUSES == 0 ]; then
echo "Not on a Platform.sh integrated environment. Skipping."
echo "::set-output name=integration::none"
else
until [ "$COMMIT_STATUS" == "success" ] || [ "$COMMIT_STATUS" == "failure" ]; do
sleep 10
ENV_URL=$(curl -s https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA | jq -r '.[0].target_url')
COMMIT_STATUS=$(curl -s https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA | jq -r '.[0].state')
done
echo "Environment deployed. Finished."
echo "::set-output name=integration::platformsh"
fi
- name: 'Pass status'
id: status
if: steps.wait.outputs.integration == 'platformsh'
run: |
COMMIT_STATUS=$(curl -s https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA | jq -r '.[0].state')
echo "::set-output name=env_status::$COMMIT_STATUS"
- name: 'Pass URL'
id: url
if: steps.wait.outputs.integration == 'platformsh'
run: |
ENV_URL=$(curl -s https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA | jq -r '.[0].target_url')
echo "::set-output name=env_url::$ENV_URL"
displaylogs:
name: 'Display logs on failed deploy'
runs-on: ubuntu-latest
needs: build
if: needs.build.outputs.commit_status == 'failure'
steps:
- name: 'Install the Platform.sh CLI'
run: |
# Install Platform.sh CLI
curl -sS https://platform.sh/cli/installer | php
- name: 'Retrieve the logs'
id: activity
run: |
# Get data.
IFS='-' read -ra my_array <<< "${{ needs.build.outputs.env_url }}"
ENVIRONMENT="pr-${my_array[1]}"
IFS="." read -ra my_array <<< "${my_array[3]}"
PROJECT="${my_array[0]}"
ACTIVITY=$(~/.platformsh/bin/platform project:curl -p $PROJECT /environments/$ENVIRONMENT/activities | jq -r '.[0].id')
LOG_URL=https://console.platform.sh/projects/$PROJECT/$ENVIRONMENT/log/$ACTIVITY
echo "::set-output name=log_url::$LOG_URL"
- name: 'Update integration target_url'
run: |
curl -s --location --request POST --header "Authorization: Bearer $GITHUB_TOKEN" \
--header 'Content-Type: application/json' \
--data-raw '{"state": "failure", "target_url": "${{ steps.activity.outputs.log_url }}", "context": "platformsh", "description": "Platform.sh: Failed to deploy environment. View the activity log details."}' \
https://api.github.com/repos/$GITHUB_REPOSITORY/statuses/$GITHUB_SHA
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment