Slack integration for Google Cloud Build, using Google Cloud Functions to post messages to Slack when a build reaches a specific state.
- Create a Slack app, and copy the webhook URL:
export SLACK_WEBHOOK_URL=my-slack-webhook-url
- Set the
PROJECT_ID
variable:
export PROJECT_ID=my-project-id
- [Optionally] Set a github token to obtain github commit author info in slack messages if applicable. Please refer to the current limitations.
export GITHUB_TOKEN=my-token
- [Optionally] Set the status you want a message for, here are the default ones:
export GC_SLACK_STATUS="SUCCESS FAILURE TIMEOUT INTERNAL_ERROR"
- Create the function with setup.sh (Option 1) OR serverless framework (Option 2)
- [Optionally] Set a specific
BUCKET_NAME
and aFUNCTION_NAME
. - Create the function:
. ./setup.sh
# OR
npm run setup
- Install
serverless
npm install serverless -g
-
Ensure that the value of
project.credentials
inserverless.yml
points to credentials with appropriate roles Serverless can use to create resources in your Project.
serverless deploy
The teardown script will delete the function FUNCTION_NAME
, and the bucket BUCKET_NAME
.
. ./teardown.sh
# OR
npm run teardown
serverless remove
Each build invokes 3 times the function:
- when the build is queued
- when the build starts
- when the build reaches a final status.
Here is the GCF pricing for calculation.
Yes if deploying with the setup script, specify the BUCKET_NAME
:
exports BUCKET_NAME=my-bucket
If deploying with the serverless framework however, this option is not yet available in the Google Cloud Functions provider plugin, but hopefully will be in the near future as an issue has been opened.
If you use the setup script with the same FUNCTION_NAME
, it will update the existing function.
If you use serverless, simply re running serverless deploy
will update the existing function.
After creating an application on Slack, active the Incoming Webhooks. You'll find the url on that page:
In the case where a BUCKET_NAME
is not defined, a random one is generated. And in order to delete it during the teardown, the variable has to be exported from the setup script.
For github commit author info to be displayed, the cloud source repositories must be in the form of github_<OWNER>_<REPO>
and there cannot be underscores in either <OWNER>
or <REPO>
. A possible solution to bypass this limitation would be to retrieve owner and repo info directly from GitHubEventsConfig.