v1.13.0
TestFail.yml / New push to repo (push) Failing after 7s
botPostMessage.yml / New push to repo (push) Successful in 8s
webhookCustomJSON.yml / New push to repo (push) Failing after 3s

This commit is contained in:
Steve Gill
2021-05-17 14:43:37 -07:00
parent f4755fc113
commit bae4c4881c
10 changed files with 112 additions and 91 deletions
+14
View File
@@ -0,0 +1,14 @@
on: [push]
jobs:
new_push_job:
runs-on: ubuntu-latest
name: New push to repo
steps:
- name: Should Fail
id: slack
uses: slackapi/[email protected]
# Use the output from the `hello` step
- name: Get the output time
run: echo "The time was ${{ steps.hello.outputs.time }}"
@@ -7,7 +7,7 @@ jobs:
steps:
- name: Publish to slack
id: slack
uses: slackapi/[email protected]2.0
uses: slackapi/[email protected]3.0
with:
channel-id: 'CTAAHRA79'
slack-message: 'posting from a github action!'
@@ -7,11 +7,10 @@ jobs:
steps:
- name: Publish to slack
id: slack
uses: slackapi/[email protected]2.0
uses: slackapi/[email protected]3.0
with:
payload: "{\"key\":\"value\",\"foo\":\"bar\"}"
env:
SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
# Use the output from the `hello` step
+26 -4
View File
@@ -5,11 +5,13 @@ Send data into Slack using this GitHub Action! This package has two different te
1) Send data to Slack's Workflow Builder (requires a paid Slack instance).
2) Send data via a Slack app to post to a specific channel (use an existing custom app or create a new one).
The recommended way to use this action is with Slack's Workflow Builder (if your on a paid Slack plan).
The recommended way to use this action is with Slack's Workflow Builder (if you're on a paid Slack plan).
## Slack Workflow Builder Route
Sending data to [Slack's Workflow builder](https://slack.com/intl/en-ca/features/workflow-automation) is the recommended way to use this action. This action will flatten and stringify the json payload from GitHub and send it over to a Slack Workflow webhook URL. Follow [these steps to create a Slack workflow using webhooks](https://slack.com/intl/en-ca/help/articles/360041352714-Create-more-advanced-workflows-using-webhooks). The Slack workflow webhook url will be in the form of `https://hooks.slack.com/workflows/....`. Add this workflow
Sending data to [Slack's Workflow builder](https://slack.com/intl/en-ca/features/workflow-automation) is the recommended way to use this action. This action will send data into Slack via a webhook URL. Follow [these steps to create a Slack workflow using webhooks](https://slack.com/intl/en-ca/help/articles/360041352714-Create-more-advanced-workflows-using-webhooks). The Slack workflow webhook url will be in the form of `https://hooks.slack.com/workflows/....`. The payload sent by this GitHub action will be flattened (all nested keys moved top level) and stringified since Slack's workflow builder only supports top level string values in payloads.
You need to define expected variables in the payload the webhook will receive. If these variables are missing in the payload, an error is returned.
### Setup
@@ -21,15 +23,35 @@ Sending data to [Slack's Workflow builder](https://slack.com/intl/en-ca/features
### Usage
```
- name: Send data to Slack workflow
- name: Send GitHub Action trigger data to Slack workflow
id: slack
uses: slackapi/slack-github-action@v1.4.0
uses: slackapi/slack-github-action@v1.13.0
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
```
or
```
- name: Send custom JSON data to Slack workflow
id: slack
uses: slackapi/[email protected]
with:
payload: "{\"key\":\"value\",\"foo\":\"bar\"}" # This data can be any valid JSON from a previous step in the GitHub Action
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
```
## Slack App Route
This route allows
## Setup
* [Create a slack app]() for your workspace (alternatively use an existing app you have already created and installed)
* Add the `chat.write` scope under **OAuth & Permissions**
* Install the app to your workspace
### Usage
```
+26 -40
View File
@@ -16051,35 +16051,7 @@ const axios = __nccwpck_require__(8020);
try {
const botToken = process.env.SLACK_BOT_TOKEN;
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
console.log('botToken', botToken, typeof botToken)
console.log('webhookUrl', webhookUrl, typeof webhookUrl)
let payload = core.getInput('payload');
console.log('payload:')
console.log(payload)
console.log(typeof payload, payload.length)
if (payload.length < 1) {
console.log('undefined payload');
console.log('no payload passed in, using payload that triggered the GitHub Action')
// Get the JSON webhook payload for the event that triggered the workflow
payload = github.context.payload;
} else {
console.log('in else')
try {
// confirm it is valid json
payload = JSON.parse(payload);
console.log('vaid json')
} catch (e) {
// passed in payload wasn't valid json
console.error("passed in payload was invalid")
throw 'Need to provide valid json payload'
}
}
// console.log(`The event payload: ${JSON.stringify(payload, undefined, 2)}`);
if (botToken === undefined && webhookUrl === undefined) {
throw 'Need to provide at least one botToken or webhookUrl'
@@ -16088,9 +16060,6 @@ try {
if (botToken.length > 0) {
const message = core.getInput('slack-message');
const channelId = core.getInput('channel-id');
console.log('message', message, typeof message)
console.log('channelId', channelId, typeof channelId)
const web = new WebClient(botToken);
if(channelId.length > 0 && message.length > 0) {
@@ -16101,10 +16070,25 @@ try {
}
}
console.log('webhookURL type', typeof webhookUrl)
if (typeof webhookUrl !== 'undefined' && webhookUrl.length > 0) {
console.log('flattening');
// send flat payload to webhookUrl
if (payload.length < 1) {
// No Payload was passed in
console.log('no custom payload was passed in, using default payload that triggered the GitHub Action')
// Get the JSON webhook payload for the event that triggered the workflow
payload = github.context.payload;
} else {
try {
// confirm it is valid json
payload = JSON.parse(payload);
} catch (e) {
// passed in payload wasn't valid json
console.error("passed in payload was invalid JSON")
throw 'Need to provide valid JSON payload'
}
}
// flatten JSON payload (no nested attributes)
const flatPayload = flatten(payload);
// workflow builder requires values to be strings
@@ -16113,18 +16097,20 @@ try {
flatPayload[key] = '' + flatPayload[key];
})
console.log('flattened Payload');
console.log(flatPayload);
console.log('sending webhook')
axios.post(webhookUrl, flatPayload)
axios.post(webhookUrl, flatPayload).then(response => {
// Successful post!
}).catch(err => {
console.log("axios post failed, double check the payload being sent includes the keys Slack expects")
console.log(payload)
console.log(err)
throw err
})
}
const time = (new Date()).toTimeString();
core.setOutput("time", time);
} catch (error) {
console.log('error in big try')
core.setFailed(error.message);
}
@@ -7,7 +7,7 @@ jobs:
steps:
- name: Publish to slack channel via bot token
id: slack
uses: slackapi/slack-github-action@v1.4.0
uses: slackapi/slack-github-action@v1.13.0
with:
channel-id: 'SLACK_CHANNEL_ID' # ID of Slack Channel you want to post to
slack-message: 'posting from a github action!' # The message you want to post
@@ -0,0 +1,14 @@
on: [push]
jobs:
new_push_job:
runs-on: ubuntu-latest
name: New push to repo
steps:
- name: Send GitHub trigger payload to Slack Workflow Builder
id: slack
uses: slackapi/[email protected]
with:
payload: "{\"key\":\"value\",\"foo\":\"bar\"}"
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
@@ -5,8 +5,8 @@ jobs:
runs-on: ubuntu-latest
name: New push to repo
steps:
- name: Send data to Slack Workflow Builder
- name: Send GitHub trigger payload to Slack Workflow Builder
id: slack
uses: slackapi/slack-github-action@v1.4.0
uses: slackapi/slack-github-action@v1.13.0
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
+26 -40
View File
@@ -7,35 +7,7 @@ const axios = require('axios');
try {
const botToken = process.env.SLACK_BOT_TOKEN;
const webhookUrl = process.env.SLACK_WEBHOOK_URL;
console.log('botToken', botToken, typeof botToken)
console.log('webhookUrl', webhookUrl, typeof webhookUrl)
let payload = core.getInput('payload');
console.log('payload:')
console.log(payload)
console.log(typeof payload, payload.length)
if (payload.length < 1) {
console.log('undefined payload');
console.log('no payload passed in, using payload that triggered the GitHub Action')
// Get the JSON webhook payload for the event that triggered the workflow
payload = github.context.payload;
} else {
console.log('in else')
try {
// confirm it is valid json
payload = JSON.parse(payload);
console.log('vaid json')
} catch (e) {
// passed in payload wasn't valid json
console.error("passed in payload was invalid")
throw 'Need to provide valid json payload'
}
}
// console.log(`The event payload: ${JSON.stringify(payload, undefined, 2)}`);
if (botToken === undefined && webhookUrl === undefined) {
throw 'Need to provide at least one botToken or webhookUrl'
@@ -44,9 +16,6 @@ try {
if (botToken.length > 0) {
const message = core.getInput('slack-message');
const channelId = core.getInput('channel-id');
console.log('message', message, typeof message)
console.log('channelId', channelId, typeof channelId)
const web = new WebClient(botToken);
if(channelId.length > 0 && message.length > 0) {
@@ -57,10 +26,25 @@ try {
}
}
console.log('webhookURL type', typeof webhookUrl)
if (typeof webhookUrl !== 'undefined' && webhookUrl.length > 0) {
console.log('flattening');
// send flat payload to webhookUrl
if (payload.length < 1) {
// No Payload was passed in
console.log('no custom payload was passed in, using default payload that triggered the GitHub Action')
// Get the JSON webhook payload for the event that triggered the workflow
payload = github.context.payload;
} else {
try {
// confirm it is valid json
payload = JSON.parse(payload);
} catch (e) {
// passed in payload wasn't valid json
console.error("passed in payload was invalid JSON")
throw 'Need to provide valid JSON payload'
}
}
// flatten JSON payload (no nested attributes)
const flatPayload = flatten(payload);
// workflow builder requires values to be strings
@@ -69,17 +53,19 @@ try {
flatPayload[key] = '' + flatPayload[key];
})
console.log('flattened Payload');
console.log(flatPayload);
console.log('sending webhook')
axios.post(webhookUrl, flatPayload)
axios.post(webhookUrl, flatPayload).then(response => {
// Successful post!
}).catch(err => {
console.log("axios post failed, double check the payload being sent includes the keys Slack expects")
console.log(payload)
console.log(err)
throw err
})
}
const time = (new Date()).toTimeString();
core.setOutput("time", time);
} catch (error) {
console.log('error in big try')
core.setFailed(error.message);
}
+1 -1
View File
@@ -1,6 +1,6 @@
{
"name": "slack-github-action",
"version": "1.12.0",
"version": "1.13.0",
"description": "The official slack github action. Use this to send data into your Slack workspace",
"main": "dist/index.js",
"scripts": {