Skip to content

Commit

Permalink
ci: add CF Pages -> Slack notifications (#2270)
Browse files Browse the repository at this point in the history
This allows to track deployment.
  • Loading branch information
sileht authored Sep 26, 2023
1 parent 5ea8185 commit 74ce97a
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 1 deletion.
26 changes: 26 additions & 0 deletions .github/workflows/deploy-cf.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
name: Deployment trigger
permissions: read-all

on:
push:
branches:
- main

concurrency:
# yamllint disable-line rule:line-length
group: "${{ github.workflow }}-${{ github.ref }}"
cancel-in-progress: true

jobs:
deploy:
timeout-minutes: 5
runs-on: ubuntu-20.04
steps:
- name: Deployment start
if: always() && github.event_name == 'push' && github.ref == 'refs/heads/main'
# yamllint disable-line rule:line-length
# nosemgrep: yaml.github-actions.security.third-party-action-not-pinned-to-commit-sha.third-party-action-not-pinned-to-commit-sha
uses: Mergifyio/gha-slack-notification@main
with:
type: deploy-start
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_DEPLOYMENT_WEBHOOK_URL_RELEASES }}
49 changes: 49 additions & 0 deletions cf-build.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash

set -e
set -o pipefail

slack_message () {
local message="$1"
local filename="docs-build-${CF_PAGES_COMMIT_SHA}.log"

local payload=$(
cat <<SLACK_EOF
{
"blocks": [
{
"type": "section",
"text": {
"type": "mrkdwn",
"text": "${message}"
},
"accessory": {
"type": "button",
"text": {
"type": "plain_text",
"text": "Cloudflare log",
"emoji": true
},
"url": "https://dash.cloudflare.com/799c85a11fd788c9c914fa97c52d971e/pages/view/docs-cf-prod"
}
}
]
}
SLACK_EOF
)
echo -en "$payload" | curl -s -H "Content-type: application/json" -d @- "$SLACK_DEPLOYMENT_WEBHOOK_URL_RELEASES"
if [ "$conclusion" == "failure" ]; then
echo -en "$payload" | curl -s -H "Content-type: application/json" -d @- "$SLACK_DEPLOYMENT_WEBHOOK_URL_PROD"
curl -F file=@build.log -F "initial_comment=$filename" -F channels=CJBEQQPFG -H "Authorization: Bearer $SLACK_DEPLOYMENT_BOT_TOKEN" https://slack.com/api/files.upload
fi
}

echo "Starting build..." > build.log
slack_message "*Building (${CF_PAGES_BRANCH}/${CF_PAGES_COMMIT_SHA}) of docs *"
exit() { slack_message "*Deployment (${CF_PAGES_BRANCH}/${CF_PAGES_COMMIT_SHA}) of docs finished ${emoji}*\\\nConclusion: ${conclusion}"; }
conclusion="failure" emoji="💥"
trap exit EXIT

gatsby build 2>&1 | tee -a build.log

conclusion="success" emoji="🦾"
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
"private": true,
"scripts": {
"build": "gatsby build",
"cf-build": "bash ./cf-build.sh",
"serve": "gatsby serve",
"develop": "gatsby develop",
"eslint": "eslint src",
Expand Down Expand Up @@ -90,4 +91,4 @@
"optionalDependencies": {
"@parcel/watcher-linux-x64-glibc": "^2.3.0"
}
}
}

0 comments on commit 74ce97a

Please sign in to comment.