Last active
August 10, 2022 15:43
-
-
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
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
--- | |
name: Post-deploy | |
on: | |
push: | |
branches-ignore: | |
- 'master' | |
env: | |
PLATFORMSH_CLI_TOKEN: ${{ secrets.CLI_TOKEN }} | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
ACC_MIN: 0.80 | |
BPR_MIN: 0.90 | |
PRF_MIN: 0.90 | |
SEO_MIN: 0.75 | |
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 | |
test: | |
name: 'Status tests' | |
runs-on: ubuntu-latest | |
needs: build | |
if: needs.build.outputs.commit_status == 'success' | |
steps: | |
- name: 'Application should deploy successfully' | |
uses: lakuapik/gh-actions-http-status@v1 | |
with: | |
sites: '["${{ needs.build.outputs.env_url }}"]' | |
expected: '[200]' | |
audit: | |
name: 'Lighthouse Audit' | |
runs-on: ubuntu-latest | |
needs: build | |
if: needs.build.outputs.commit_status == 'success' | |
outputs: | |
audit_data: ${{ steps.report.outputs.report_data }} | |
steps: | |
- name: Audit live URL | |
uses: jakejarvis/lighthouse-action@master | |
with: | |
url: ${{ needs.build.outputs.env_url }}admin | |
- name: Upload results as an artifact | |
uses: actions/upload-artifact@master | |
with: | |
name: report | |
path: './report' | |
- name: Pass report data | |
id: report | |
run: | | |
export REPORT=$(ls report | grep \.json$) | |
echo "Download the full report under *Artifacts* at: https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
echo "::set-output name=report_data::$REPORT" | |
- name: Accessibility test | |
run: | | |
REPORT=$(ls report | grep \.json$) | |
SCORE=$(cat report/$REPORT | jq -r '.categories.accessibility.score') | |
if [ 1 -eq "$(echo "${SCORE} < ${{ env.ACC_MIN }}" | bc)" ]; then | |
echo "Threshold Failed:" | |
echo " - Score: $SCORE" | |
echo " - Threshold: ${{ env.ACC_MIN }}" | |
echo "Download the full report under *Artifacts* at: https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
exit 1 | |
else | |
echo "Passed:" | |
echo " - Score: $SCORE" | |
echo " - Threshold: ${{ env.ACC_MIN }}" | |
echo "Download the full report under *Artifacts* at: https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
fi | |
- name: Best practices test | |
run: | | |
REPORT=$(ls report | grep \.json$) | |
SCORE=$(cat report/$REPORT | jq -r '.categories."best-practices".score') | |
if [ 1 -eq "$(echo "${SCORE} < ${{ env.BPR_MIN }}" | bc)" ]; then | |
echo "Threshold Failed:" | |
echo " - Score: $SCORE" | |
echo " - Threshold: ${{ env.BPR_MIN }}" | |
echo "Download the full report under *Artifacts* at: https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
exit 1 | |
else | |
echo "Passed:" | |
echo " - Score: $SCORE" | |
echo " - Threshold: ${{ env.BPR_MIN }}" | |
echo "Download the full report under *Artifacts* at: https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
fi | |
- name: Performance test | |
run: | | |
REPORT=$(ls report | grep \.json$) | |
SCORE=$(cat report/$REPORT | jq -r '.categories.performance.score') | |
if [ 1 -eq "$(echo "${SCORE} < ${{ env.PRF_MIN }}" | bc)" ]; then | |
echo "Threshold Failed:" | |
echo " - Score: $SCORE" | |
echo " - Threshold: ${{ env.PRF_MIN }}" | |
echo "Download the full report under *Artifacts* at: https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
exit 1 | |
else | |
echo "Passed:" | |
echo " - Score: $SCORE" | |
echo " - Threshold: ${{ env.PRF_MIN }}" | |
echo "Download the full report under *Artifacts* at: https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
fi | |
- name: SEO test | |
run: | | |
REPORT=$(ls report | grep \.json$) | |
SCORE=$(cat report/$REPORT | jq -r '.categories.seo.score') | |
if [ 1 -eq "$(echo "${SCORE} < ${{ env.SEO_MIN }}" | bc)" ]; then | |
echo "Threshold Failed:" | |
echo " - Score: $SCORE" | |
echo " - Threshold: ${{ env.SEO_MIN }}" | |
echo "Download the full report under *Artifacts* at: https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
exit 1 | |
else | |
echo "Passed:" | |
echo " - Score: $SCORE" | |
echo " - Threshold: ${{ env.SEO_MIN }}" | |
echo "Download the full report under *Artifacts* at: https://github.com/$GITHUB_REPOSITORY/actions/runs/$GITHUB_RUN_ID" | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment