diff --git a/README.md b/README.md index 631e9bba..6dfeae7d 100644 --- a/README.md +++ b/README.md @@ -164,7 +164,7 @@ achieved by adding this step to a job in your GitHub workflow and inviting the bot associated with your app to the channel for posting: ```yaml -- name: Post to a Slack channel +- name: Post text to a Slack channel uses: slackapi/slack-github-action@v2.0.0 with: method: chat.postMessage @@ -180,7 +180,7 @@ More complex message layouts, such as messages made with [Block Kit][block-kit] blocks, can also be sent with one of the Slack API methods: ```yaml -- name: Post to a Slack channel +- name: Post blocks to a Slack channel uses: slackapi/slack-github-action@v2.0.0 with: method: chat.postMessage @@ -203,7 +203,7 @@ outputs from past steps as inputs to current ones: ```yaml - name: Initiate the deployment launch sequence - id: slack + id: launch_sequence uses: slackapi/slack-github-action@v2.0.0 with: method: chat.postMessage @@ -226,7 +226,7 @@ outputs from past steps as inputs to current ones: token: ${{ secrets.SLACK_BOT_TOKEN }} payload: | channel: ${{ secrets.SLACK_CHANNEL_ID }} - ts: "${{ steps.slack.outputs.ts }}" + ts: "${{ steps.launch_sequence.outputs.ts }}" text: "Deployment finished! :rocket:" attachments: - color: "28a745" @@ -278,7 +278,7 @@ convenience of the [`files.uploadV2`][files.uploadV2] method: payload: | channel_id: ${{ secrets.SLACK_CHANNEL_ID }} initial_comment: "the results are in!" - file: "results.out" + file: "./path/to/results.out" filename: "results-${{ github.sha }}.out" ``` @@ -307,6 +307,12 @@ Gather a Slack incoming webhook URL: 6. [Add this Action as a step][job-step] to your GitHub workflow and provide an input payload to send as a message. +The webhook URL will resemble something like so: + +```txt +https://hooks.slack.com/services/T0123456789/B1001010101/7IsoQTrixdUtE971O1xQTm4T +``` + #### Usage Add the collected webhook from above to a GitHub workflow and configure the step @@ -342,7 +348,7 @@ The `errors` option defaults to `false` so failed requests do not cause the step to fail. This result can still be gathered from the `ok` output. ```yaml -- name: Send GitHub Action data to a Slack workflow +- name: Attempt to call an unknown method uses: slackapi/slack-github-action@v2.0.0 with: errors: true @@ -366,7 +372,7 @@ The `payload-delimiter` option will flatten the input payload using the provided delimiter and will also make values stringified: ```yaml -- name: Send GitHub Action data to a Slack workflow +- name: Flatten the default GitHub payload uses: slackapi/slack-github-action@v2.0.0 with: payload-delimiter: "_" @@ -385,7 +391,6 @@ input payload with the `payload-templated` option: ```yaml - name: Send custom JSON data to Slack workflow - id: slack uses: slackapi/slack-github-action@v2.0.0 with: payload-file-path: "./payload-slack-content.json" diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/JSON_payload.yml b/example-workflows/Technique_1_Slack_Workflow_Builder/JSON_payload.yml deleted file mode 100644 index 38ea305e..00000000 --- a/example-workflows/Technique_1_Slack_Workflow_Builder/JSON_payload.yml +++ /dev/null @@ -1,16 +0,0 @@ -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/slack-github-action@v2.0.0 - with: - webhook: ${{ secrets.SLACK_WEBHOOK_URL }} - webhook-type: webhook-trigger - payload: | - key: value - foo: bar diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/JSON_payload_from_file.yml b/example-workflows/Technique_1_Slack_Workflow_Builder/JSON_payload_from_file.yml deleted file mode 100644 index f8ae7f41..00000000 --- a/example-workflows/Technique_1_Slack_Workflow_Builder/JSON_payload_from_file.yml +++ /dev/null @@ -1,14 +0,0 @@ -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/slack-github-action@v2.0.0 - with: - payload-file-path: "./example-workflows/Technique_1_Slack_Workflow_Builder/payloads/example.json" - webhook: ${{ secrets.SLACK_WEBHOOK_URL }} - webhook-type: webhook-trigger diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/README.md b/example-workflows/Technique_1_Slack_Workflow_Builder/README.md new file mode 100644 index 00000000..14413975 --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/README.md @@ -0,0 +1,81 @@ +# Technique 1: Slack Workflow Builder + +> :memo: This technique uses Workflow Builder, which requires +> [a Slack paid plan][plans]. + +This technique sends data to Slack using a webhook to start a workflow created +using Slack [Workflow Builder][wfb]. + +## Setup + +The Slack app manifest included with each example showcases steps of a workflow +in [Workflow Builder][wfb] along with the [webhook trigger][triggers] inputs. + +For details on how to setup this technique in GitHub Actions, read the +[`README.md`][setup]. + +## Example workflows + +1. [**Format generated files**](#format-generated-files): Message outputs from + prior steps. +2. [**Post release announcements**](#post-release-announcements): Share releases + to a channel. +3. [**Update a channel topic**](#update-a-channel-topic): Highlight the current + build status. + +### Format generated files + +Convert build outputs from earlier GitHub Action steps into a Slack message. + +This example uses data from a payload file to [send a message][send_message] to +a hardcoded channel. + +**Related files**: + +- [`builds.data.json`](./builds.data.json): Payload file being sent. +- [`builds.gha.yml`](./builds.gha.yml): GitHub Actions workflow. +- [`builds.manifest.json`](./builds.manifest.json): Slack app manifest. +- [`builds.trigger.json`](./builds.trigger.json): Slack webhook trigger. + +### Post release announcements + +Select a channel to post news about the most recent release to. + +This example uses [Slack functions][functions] and inline inputs to do the +following: + +1. Open a form to select a channel. +2. Send a message to the selected channel. +3. React with a `:tada:` emoji. + +**Related files**: + +- [`announcements.gha.yml`](./announcements.gha.yml): GitHub Actions workflow. +- [`announcements.manifest.json`](./announcements.manifest.json): Slack app + manifest. +- [`announcements.trigger.json`](./announcements.trigger.json): Slack webhook + trigger. + +### Update a channel topic + +Show the latest commit status in the header of a channel. + +This example uses the default GitHub event [context][event-context] and +[payload][event-payload] to [update a channel topic][update_channel_topic]. + +**Related files**: + +- [`topic.gha.yml`](./topic.gha.yml): GitHub Actions workflow. +- [`topic.manifest.json`](./topic.manifest.json): Slack app manifest. +- [`topic.trigger.json`](./topic.trigger.json): Slack webhook trigger. + +[event-context]: https://github.com/actions/toolkit/blob/main/packages/github/src/context.ts#L6 +[event-payload]: https://docs.github.com/en/webhooks/webhook-events-and-payloads +[functions]: https://api.slack.com/automation/functions +[giphy]: https://giphy.com +[send_message]: https://api.slack.com/reference/functions/send_message +[plans]: https://slack.com/pricing +[setup]: https://github.com/slackapi/slack-github-action/blob/main/README.md#technique-1-slack-workflow-builder +[triggers]: https://api.slack.com/automation/triggers/webhook +[update_channel_topic]: https://api.slack.com/reference/functions/update_channel_topic +[wfb]: https://slack.com/features/workflow-automation diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/announcements.gha.yml b/example-workflows/Technique_1_Slack_Workflow_Builder/announcements.gha.yml new file mode 100644 index 00000000..412c7424 --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/announcements.gha.yml @@ -0,0 +1,22 @@ +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json + +name: Post release announcements +on: + release: + types: + - published +jobs: + run: + name: Share recent changes with a channel + runs-on: ubuntu-latest + steps: + - name: Draft with these release notes details + uses: slackapi/slack-github-action@v2.0.0 + with: + webhook: ${{ secrets.SLACK_WEBHOOK_URL }} + webhook-type: webhook-trigger + payload: | + "draft_channel": "${{ secrets.SLACK_CHANNEL_ID }}", + "release_notes": ${{ toJSON(github.event.release.body) }}, + "release_repository": "${{ github.repository }}", + "release_version": "${{ github.event.release.tag_name }}", diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/announcements.manifest.json b/example-workflows/Technique_1_Slack_Workflow_Builder/announcements.manifest.json new file mode 100644 index 00000000..f8af8e26 --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/announcements.manifest.json @@ -0,0 +1,113 @@ +{ + "_metadata": { + "major_version": 2 + }, + "display_information": { + "name": "Slack GitHub Actions Releaser", + "description": "Sharing the latest news in channel" + }, + "features": { + "app_home": { + "messages_tab_enabled": false + }, + "bot_user": { + "display_name": "Slack GitHub Actions Releaser" + } + }, + "oauth_config": { + "scopes": { + "bot": ["chat:write", "chat:write.public", "reactions:write"] + } + }, + "settings": { + "org_deploy_enabled": true + }, + "workflows": { + "release_announcement": { + "title": "Release announcement", + "description": "Share excitement around the latest changes", + "input_parameters": { + "properties": { + "draft_channel": { + "type": "slack#/types/channel_id" + }, + "release_notes": { + "type": "string" + }, + "release_repository": { + "type": "string" + }, + "release_version": { + "type": "string" + } + }, + "required": [ + "draft_channel", + "release_notes", + "release_repository", + "release_version" + ] + }, + "steps": [ + { + "id": "0", + "function_id": "slack#/functions/send_message", + "inputs": { + "channel_id": "{{inputs.draft_channel}}", + "message": "A new release of was tagged! :rocket:", + "interactive_blocks": [ + { + "type": "actions", + "elements": [ + { + "type": "button", + "text": { + "type": "plain_text", + "text": "Share" + }, + "action_id": "share" + } + ] + } + ] + } + }, + { + "id": "1", + "function_id": "slack#/functions/open_form", + "inputs": { + "title": "Share the release notes", + "interactivity": "{{steps.0.interactivity}}", + "submit_label": "Share", + "fields": { + "elements": [ + { + "name": "announcement_channel", + "title": "Select an announcements channel", + "type": "slack#/types/channel_id" + } + ], + "required": ["announcement_channel"] + } + } + }, + { + "id": "2", + "function_id": "slack#/functions/send_message", + "inputs": { + "channel_id": "{{steps.1.fields.announcement_channel}}", + "message": "A new release of was tagged! :rocket:\n```{{inputs.release_notes}}```" + } + }, + { + "id": "3", + "function_id": "slack#/functions/add_reaction", + "inputs": { + "message_context": "{{steps.2.message_context}}", + "emoji": "tada" + } + } + ] + } + } +} diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/announcements.trigger.json b/example-workflows/Technique_1_Slack_Workflow_Builder/announcements.trigger.json new file mode 100644 index 00000000..0b8579a8 --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/announcements.trigger.json @@ -0,0 +1,20 @@ +{ + "type": "webhook", + "name": "Release webhook", + "description": "Share the most recent changes", + "workflow": "#/workflows/release_announcement", + "inputs": { + "draft_channel": { + "value": "{{data.draft_channel}}" + }, + "release_notes": { + "value": "{{data.release_notes}}" + }, + "release_repository": { + "value": "{{data.release_repository}}" + }, + "release_version": { + "value": "{{data.release_version}}" + } + } +} diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/builds.data.json b/example-workflows/Technique_1_Slack_Workflow_Builder/builds.data.json new file mode 100644 index 00000000..57427528 --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/builds.data.json @@ -0,0 +1,6 @@ +{ + "status": "done", + "task": "Downstream staging rollout", + "errors": "", + "runner": "github-actions" +} diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/builds.gha.yml b/example-workflows/Technique_1_Slack_Workflow_Builder/builds.gha.yml new file mode 100644 index 00000000..92843100 --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/builds.gha.yml @@ -0,0 +1,23 @@ +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json + +name: Format generated files +on: + push: + branches: + - main +jobs: + run: + name: Write structured data as a message + runs-on: ubuntu-latest + steps: + - name: Checkout the repo + uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Start the Slack workflow + uses: slackapi/slack-github-action@v2.0.0 + with: + payload-file-path: "./example-workflows/Technique_1_Slack_Workflow_Builder/builds.data.json" + webhook: ${{ secrets.SLACK_WEBHOOK_URL }} + webhook-type: webhook-trigger diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/builds.manifest.json b/example-workflows/Technique_1_Slack_Workflow_Builder/builds.manifest.json new file mode 100644 index 00000000..8bfc4874 --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/builds.manifest.json @@ -0,0 +1,100 @@ +{ + "_metadata": { + "major_version": 2 + }, + "display_information": { + "name": "Slack GitHub Actions Builder", + "description": "Sharing the latest builds in channel" + }, + "features": { + "app_home": { + "messages_tab_enabled": false + }, + "bot_user": { + "display_name": "Slack GitHub Actions Builder" + } + }, + "oauth_config": { + "scopes": { + "bot": ["chat:write", "chat:write.public"] + } + }, + "settings": { + "org_deploy_enabled": true + }, + "workflows": { + "build_outputs": { + "title": "Build outputs", + "description": "Write details of the most recent build", + "input_parameters": { + "properties": { + "errors": { + "type": "string" + }, + "runner": { + "type": "string" + }, + "status": { + "type": "string" + }, + "task": { + "type": "string" + } + }, + "required": ["errors", "runner", "status", "task"] + }, + "steps": [ + { + "id": "0", + "function_id": "slack#/functions/send_message", + "inputs": { + "channel_id": "C0123456789", + "message": [ + { + "type": "rich_text", + "elements": [ + { + "type": "rich_text_section", + "elements": [ + { + "type": "text", + "text": "{{inputs.task}}: ", + "style": { + "bold": true + } + }, + { + "type": "text", + "text": "{{inputs.status}}", + "style": { + "code": true + } + } + ] + }, + { + "type": "rich_text_quote", + "elements": [ + { + "type": "text", + "text": "{{inputs.errors}} \n" + }, + { + "type": "emoji", + "name": "robot_face" + }, + { + "type": "text", + "text": " {{inputs.runner}}" + } + ] + } + ] + } + ] + } + } + ] + } + } +} diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/builds.trigger.json b/example-workflows/Technique_1_Slack_Workflow_Builder/builds.trigger.json new file mode 100644 index 00000000..2aba47d3 --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/builds.trigger.json @@ -0,0 +1,20 @@ +{ + "type": "webhook", + "name": "Status sharing", + "description": "Update a channel with build results", + "workflow": "#/workflows/build_outputs", + "inputs": { + "errors": { + "value": "{{data.errors}}" + }, + "runner": { + "value": "{{data.runner}}" + }, + "status": { + "value": "{{data.status}}" + }, + "task": { + "value": "{{data.task}}" + } + } +} diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/default_GitHub_Trigger_payload.yml b/example-workflows/Technique_1_Slack_Workflow_Builder/default_GitHub_Trigger_payload.yml deleted file mode 100644 index aa7f98a8..00000000 --- a/example-workflows/Technique_1_Slack_Workflow_Builder/default_GitHub_Trigger_payload.yml +++ /dev/null @@ -1,14 +0,0 @@ -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/slack-github-action@v2.0.0 - with: - payload-delimiter: "_" - webhook: ${{ secrets.SLACK_WEBHOOK_URL }} - webhook-type: webhook-trigger diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/payloads/example.json b/example-workflows/Technique_1_Slack_Workflow_Builder/payloads/example.json deleted file mode 100644 index 6f212ceb..00000000 --- a/example-workflows/Technique_1_Slack_Workflow_Builder/payloads/example.json +++ /dev/null @@ -1,3 +0,0 @@ -{ - "text": "${{ github.workflow }} workflow failed on ${{ env.DEFAULT_BRANCH }} branch in the ${{ github.event.repository.name }} repository!" -} diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/topic.gha.yml b/example-workflows/Technique_1_Slack_Workflow_Builder/topic.gha.yml new file mode 100644 index 00000000..a1194be2 --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/topic.gha.yml @@ -0,0 +1,18 @@ +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json + +name: Update a channel topic +on: + push: + branches: + - main +jobs: + run: + name: Keep track of the main branch + runs-on: ubuntu-latest + steps: + - name: Update the channel topic + uses: slackapi/slack-github-action@v2.0.0 + with: + payload-delimiter: "_" + webhook: ${{ secrets.SLACK_WEBHOOK_URL }} + webhook-type: webhook-trigger diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/topic.manifest.json b/example-workflows/Technique_1_Slack_Workflow_Builder/topic.manifest.json new file mode 100644 index 00000000..c720576a --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/topic.manifest.json @@ -0,0 +1,52 @@ +{ + "_metadata": { + "major_version": 2 + }, + "display_information": { + "name": "Slack GitHub Actions Commits", + "description": "Reading the latest pushes" + }, + "features": { + "app_home": { + "messages_tab_enabled": false + }, + "bot_user": { + "display_name": "Slack GitHub Actions Commits" + } + }, + "oauth_config": { + "scopes": { + "bot": ["channels:manage", "groups:write.topic"] + } + }, + "settings": { + "org_deploy_enabled": true + }, + "workflows": { + "follow_recent_commits": { + "title": "Follow recent commits", + "description": "Write details of the most recent build", + "input_parameters": { + "properties": { + "commit_message": { + "type": "string" + }, + "commit_repository": { + "type": "string" + } + }, + "required": ["commit_message", "commit_repository"] + }, + "steps": [ + { + "id": "0", + "function_id": "slack#/functions/update_channel_topic", + "inputs": { + "channel_id": "C0123456789", + "topic": ":large_green_square: *{{inputs.commit_repository}}* {{inputs.commit_message}}" + } + } + ] + } + } +} diff --git a/example-workflows/Technique_1_Slack_Workflow_Builder/topic.trigger.json b/example-workflows/Technique_1_Slack_Workflow_Builder/topic.trigger.json new file mode 100644 index 00000000..302fec0a --- /dev/null +++ b/example-workflows/Technique_1_Slack_Workflow_Builder/topic.trigger.json @@ -0,0 +1,14 @@ +{ + "type": "webhook", + "name": "Status sharing", + "description": "Update a channel with commit details", + "workflow": "#/workflows/follow_recent_commits", + "inputs": { + "commit_message": { + "value": "{{data.payload_head_commit_message}}" + }, + "commit_repository": { + "value": "{{data.payload_repository_full_name}}" + } + } +} diff --git a/example-workflows/Technique_2_Slack_API_Method/README.md b/example-workflows/Technique_2_Slack_API_Method/README.md new file mode 100644 index 00000000..8e1f0205 --- /dev/null +++ b/example-workflows/Technique_2_Slack_API_Method/README.md @@ -0,0 +1,40 @@ +# Technique 2: Slack API method + +A [token][tokens], such as a bot or user token, must be used to call one of +[the Slack API methods][methods] with this technique. + +## Setup + +For details on how to set up this technique in GitHub Actions, read the +[`README.md`][setup]. + +## Example workflows + +1. [**Direct message the author**](#direct-message-the-author): Write to the + Slack user with a matching email. +2. [**Invite a usergroup to channel**](#invite-a-usergroup-to-channel): Create a + channel and invite members. + +### Direct message the author + +Send a direct message to the user that pushed the most recent commits. + +This example uses the email of the pusher to find the user to send a message to. + +**Related files**: + +- [`author.yml`](./author.yml): GitHub Actions workflow. + +### Invite a usergroup to channel + +Create a channel after a bug is reported and add members of a usergroup. + +This example chains multiple Slack API methods together to help fix bugs fast. + +**Related files**: + +- [`invite.yml`](./invite.yml): GitHub Actions workflow. + +[methods]: https://api.slack.com/methods +[setup]: https://github.com/slackapi/slack-github-action?tab=readme-ov-file#technique-2-slack-api-method +[tokens]: https://api.slack.com/concepts/token-types diff --git a/example-workflows/Technique_2_Slack_API_Method/author.yml b/example-workflows/Technique_2_Slack_API_Method/author.yml new file mode 100644 index 00000000..0e62d513 --- /dev/null +++ b/example-workflows/Technique_2_Slack_API_Method/author.yml @@ -0,0 +1,56 @@ +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json + +name: Direct message the author +on: + push: +jobs: + run: + name: Send a notification of recent changes + runs-on: ubuntu-latest + steps: + - name: Checkout the repo + uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Find correspondences + id: email + uses: slackapi/slack-github-action@v2.0.0 + with: + method: users.lookupByEmail # https://api.slack.com/methods/users.lookupByEmail + token: ${{ secrets.SLACK_BOT_TOKEN }} + payload: | + email: ${{ github.event.pusher.email }} + + - name: Search email detail + if: ${{ steps.email.outputs.ok }} + run: | + SLACK_USER_ID=$(echo '${{ steps.email.outputs.response }}' | jq -r '.user.id') + echo "SLACK_USER_ID=$SLACK_USER_ID" >> $GITHUB_ENV + + - name: Send a direct message + if: ${{ steps.email.outputs.ok }} + uses: slackapi/slack-github-action@v2.0.0 + with: + errors: true + method: chat.postMessage # https://api.slack.com/methods/chat.postMessage + token: ${{ secrets.SLACK_BOT_TOKEN }} + payload: | + "channel": "${{ env.SLACK_USER_ID }}", + "text": "${{ github.repository }} had a change!", + "blocks": [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": ":large_green_square: : `${{ github.sha }}`" + } + }, + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": ${{ toJSON(github.event.head_commit.message) }} + } + } + ] diff --git a/example-workflows/Technique_2_Slack_API_Method/invite.yml b/example-workflows/Technique_2_Slack_API_Method/invite.yml new file mode 100644 index 00000000..e19f7b9a --- /dev/null +++ b/example-workflows/Technique_2_Slack_API_Method/invite.yml @@ -0,0 +1,56 @@ +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json + +name: Invite a usergroup to channel +on: + issues: + types: + - labeled +jobs: + run: + name: Respond to reports of a new problem + runs-on: ubuntu-latest + if: ${{ github.event.label.name == 'bug' }} + steps: + - name: Create a new Slack channel for communications + id: conversation + uses: slackapi/slack-github-action@v2.0.0 + with: + errors: true + method: conversations.create # https://api.slack.com/methods/conversations.create + token: ${{ secrets.SLACK_BOT_TOKEN }} + payload: | + name: issue-${{ github.event.issue.number }} + + - name: Send the issue link into the Slack channel + uses: slackapi/slack-github-action@v2.0.0 + with: + method: chat.postMessage # https://api.slack.com/methods/chat.postMessage + token: ${{ secrets.SLACK_BOT_TOKEN }} + payload: | + channel: ${{ steps.conversation.outputs.channel_id }} + text: "An issue was opened : ${{ github.event.issue.html_url }}" + + - name: Gather information of those to add + id: members + uses: slackapi/slack-github-action@v2.0.0 + with: + errors: true + method: usergroups.users.list # https://api.slack.com/methods/usergroups.users.list + token: ${{ secrets.SLACK_BOT_TOKEN }} + payload: | + usergroup: ${{ secrets.SLACK_USERGROUP_ID }} + + - name: Combine the list of usergroup users + run: | + SLACK_USERGROUP_USER_IDS=$(echo '${{ steps.members.outputs.response }}' | jq -r '.users | join(",")' ) + echo "SLACK_USERGROUP_USER_IDS=$SLACK_USERGROUP_USER_IDS" >> $GITHUB_ENV + + - name: Add the usergroup to the channel + uses: slackapi/slack-github-action@v2.0.0 + with: + errors: true + method: conversations.invite # https://api.slack.com/methods/conversations.invite + token: ${{ secrets.SLACK_BOT_TOKEN }} + payload: | + channel: ${{ steps.conversation.outputs.channel_id }} + users: ${{ env.SLACK_USERGROUP_USER_IDS }} diff --git a/example-workflows/Technique_2_Slack_App/JSON_payload.yml b/example-workflows/Technique_2_Slack_App/JSON_payload.yml deleted file mode 100644 index c4ee7b00..00000000 --- a/example-workflows/Technique_2_Slack_App/JSON_payload.yml +++ /dev/null @@ -1,32 +0,0 @@ -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/slack-github-action@v2.0.0 - with: - token: ${{ secrets.SLACK_BOT_TOKEN }} - method: chat.postMessage - payload: | - channel: "C0123456789" - text: "messages make meeting" - blocks: - - type: "divider" - type: "image" - title: - type: "plain_text" - text: "Slack Slack Slack" - emoji: true - image_url: "https://media.makeameme.org/created/a-slack-this.jpg" - alt_text: "marg" - - type: "actions" - elements: - - type": "button" - text: - type: "plain_text" - text: "${{ github.sha }}" - url: "https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}" diff --git a/example-workflows/Technique_2_Slack_App/JSON_payload_as_text.yml b/example-workflows/Technique_2_Slack_App/JSON_payload_as_text.yml deleted file mode 100644 index 5ab0c5da..00000000 --- a/example-workflows/Technique_2_Slack_App/JSON_payload_as_text.yml +++ /dev/null @@ -1,16 +0,0 @@ -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/slack-github-action@v2.0.0 - with: - method: chat.postMessage - token: ${{ secrets.SLACK_BOT_TOKEN }} - payload: | - text: "posting from a github action" - channel: "C0123456789" diff --git a/example-workflows/Technique_2_Slack_App/main.yml b/example-workflows/Technique_2_Slack_App/main.yml deleted file mode 100644 index e0e859dd..00000000 --- a/example-workflows/Technique_2_Slack_App/main.yml +++ /dev/null @@ -1,16 +0,0 @@ -on: [push] - -jobs: - new_push_job: - runs-on: ubuntu-latest - name: New push to repo - steps: - - name: Publish to slack channel via bot token - id: slack - uses: slackapi/slack-github-action@v2.0.0 - with: - method: chat.postMessage - token: ${{ secrets.SLACK_BOT_TOKEN }} - payload: | - channel: "C0123456789" - text: "posting from a github action!" diff --git a/example-workflows/Technique_3_Slack_Incoming_Webhook/README.md b/example-workflows/Technique_3_Slack_Incoming_Webhook/README.md new file mode 100644 index 00000000..5c388cbb --- /dev/null +++ b/example-workflows/Technique_3_Slack_Incoming_Webhook/README.md @@ -0,0 +1,57 @@ +# Technique 3: Slack incoming webhook + +This technique uses this Action to post a message to a channel or direct message +with [incoming webhooks][incoming-webhook] and a Slack app. + +Incoming webhooks follow the same [formatting][formatting] patterns as other +Slack messaging APIs. Posted messages can be as short as a single line of text, +include additional interactivity with [interactive components][interactivity], +or be formatted with [Block Kit][block-kit] to build visual components. + +## Setup + +For details on how to setup this technique in GitHub Actions, read the +[`README.md`][setup]. + +## Example workflows + +1. [Post an inline text message](#post-an-inline-text-message) +2. [Post an inline block message](#post-an-inline-block-message) +3. [Post blocks found in a file](#post-blocks-found-in-a-file) + +### Post an inline text message + +Write a line of text after a push event is received. + +This example uses incoming webhooks to post a plain text message. + +**Related files**: + +- [`text.yml`](./text.yml): GitHub Actions workflow. + +### Post an inline block message + +Format a response to recent adventures. + +This example uses incoming webhooks to post a message with Block Kit. + +**Related files**: + +- [`blocks.yml`](./blocks.yml): GitHub Actions workflow. + +### Post blocks found in a file + +Link to the GitHub Actions job in progress. + +This example uses file data when posting to an incoming webhook. + +**Related files**: + +- [`saved.data.json`](./saved.data.json): Payload file being sent. +- [`saved.gha.yml`](./saved.gha.yml): GitHub Actions workflow. + +[block-kit]: https://api.slack.com/surfaces/messages#complex_layouts +[formatting]: https://api.slack.com/reference/surfaces/formatting +[incoming-webhook]: https://api.slack.com/messaging/webhooks +[interactivity]: https://api.slack.com/messaging/interactivity +[setup]: https://github.com/slackapi/slack-github-action?tab=readme-ov-file#technique-3-slack-incoming-webhook diff --git a/example-workflows/Technique_3_Slack_Incoming_Webhook/main.yml b/example-workflows/Technique_3_Slack_Incoming_Webhook/blocks.yml similarity index 55% rename from example-workflows/Technique_3_Slack_Incoming_Webhook/main.yml rename to example-workflows/Technique_3_Slack_Incoming_Webhook/blocks.yml index eecb8dc8..b6909a98 100644 --- a/example-workflows/Technique_3_Slack_Incoming_Webhook/main.yml +++ b/example-workflows/Technique_3_Slack_Incoming_Webhook/blocks.yml @@ -1,34 +1,36 @@ -on: [push] +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json +name: Post an inline block message +on: + push: + branches: + - main jobs: - new_push_job: + run: + name: Share a travel review to channel runs-on: ubuntu-latest - name: New push to repo steps: - - name: Send GitHub trigger payload to Slack Workflow Builder - id: slack + - name: Write the review uses: slackapi/slack-github-action@v2.0.0 with: webhook: ${{ secrets.SLACK_WEBHOOK_URL }} - webhook-type: webhook-trigger + webhook-type: incoming-webhook payload: | - "text": "Danny Torrence left a 1 star review for your property." - "blocks": + text: "Danny Torrence left a 1 star review for your property." + blocks: - type: "section" text: type: "mrkdwn" text: "Danny Torrence left the following review for your property:" - type: "section" - block_id: "section567" - text: { + text: type: "mrkdwn" - text: " \\n :star: \\n Doors had too many axe holes, guest in room 237 was far too rowdy, whole place felt stuck in the 1920s." + text: " :star: \n Doors had too many axe holes, guest in room 237 was far too rowdy, whole place felt stuck in the 1920s." accessory: type: "image" image_url: "https://is5-ssl.mzstatic.com/image/thumb/Purple3/v4/d3/72/5c/d3725c8f-c642-5d69-1904-aa36e4297885/source/256x256bb.jpg" alt_text: "Haunted hotel image" - type: "section" - block_id: "section789" fields: - type: "mrkdwn" - text: "*Average Rating*\\n1.0" + text: "*Average Rating*: 1.0" diff --git a/example-workflows/Technique_3_Slack_Incoming_Webhook/saved.data.json b/example-workflows/Technique_3_Slack_Incoming_Webhook/saved.data.json new file mode 100644 index 00000000..7233cc09 --- /dev/null +++ b/example-workflows/Technique_3_Slack_Incoming_Webhook/saved.data.json @@ -0,0 +1,34 @@ +{ + "channel": "${{ env.SLACK_CHANNEL_ID }}", + "text": "Messages met made meetings meet", + "blocks": [ + { + "type": "section", + "text": { + "type": "mrkdwn", + "text": "Slack notifications sound following the most recent commit pushed `${{ github.sha }}`" + }, + "accessory": { + "type": "button", + "text": { + "type": "plain_text", + "text": "Inspect", + "emoji": true + }, + "value": "actions", + "url": "https://github.com/${{ github.payload.repository.full_name }}/actions/runs/${{ github.runId }}", + "action_id": "run" + } + }, + { + "type": "image", + "title": { + "type": "plain_text", + "text": "@slackbot has the answers", + "emoji": true + }, + "image_url": "https://media.makeameme.org/created/a-slack-this.jpg", + "alt_text": "Baby Yoda professes the power of a Slack meeting" + } + ] +} diff --git a/example-workflows/Technique_3_Slack_Incoming_Webhook/saved.gha.yml b/example-workflows/Technique_3_Slack_Incoming_Webhook/saved.gha.yml new file mode 100644 index 00000000..dfd755a3 --- /dev/null +++ b/example-workflows/Technique_3_Slack_Incoming_Webhook/saved.gha.yml @@ -0,0 +1,26 @@ +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json + +name: Post blocks found in a file +on: + push: + branches: + - main +jobs: + run: + name: Share a travel review to channel + runs-on: ubuntu-latest + steps: + - name: Checkout the repo + uses: actions/checkout@v4 + with: + persist-credentials: false + + - name: Forward a saved message + uses: slackapi/slack-github-action@v2.0.0 + with: + payload-file-path: "./example-workflows/Technique_3_Slack_Incoming_Webhook/saved.data.json" + payload-templated: true + webhook: ${{ secrets.SLACK_WEBHOOK_URL }} + webhook-type: incoming-webhook + env: + SLACK_CHANNEL_ID: ${{ secrets.SLACK_CHANNEL_ID }} diff --git a/example-workflows/Technique_3_Slack_Incoming_Webhook/text.yml b/example-workflows/Technique_3_Slack_Incoming_Webhook/text.yml new file mode 100644 index 00000000..d92af4ef --- /dev/null +++ b/example-workflows/Technique_3_Slack_Incoming_Webhook/text.yml @@ -0,0 +1,19 @@ +# yaml-language-server: $schema=https://json.schemastore.org/github-workflow.json + +name: Post an inline text message +on: + push: + branches: + - main +jobs: + run: + name: Leave a kind message after updates + runs-on: ubuntu-latest + steps: + - name: Greet the reader + uses: slackapi/slack-github-action@v2.0.0 + with: + webhook: ${{ secrets.SLACK_WEBHOOK_URL }} + webhook-type: incoming-webhook + payload: | + text: "Greetings!"