DEV Community

Peter Sankauskas
Peter Sankauskas

Posted on

Upgrade Slack GHA

If you like having your GitHub Actions workflows post messages to Slack, well, things just got a lot easier. Version 2 of slack-github-action has been released, and it makes your workflow file so much nicer because it supports YAML! Here is what it looks like.

BEFORE:

      - name: Post to Slack
        uses: slackapi/slack-github-action@v1
        env:
          SLACK_BOT_TOKEN: ${{ secrets.SLACK_BOT_TOKEN }}
        with:
          channel-id: "C3P0ANDR2D2"
          payload: |
            {
              "blocks": [
                {
                  "type": "section",
                  "text": {
                    "type": "mrkdwn",
                    "text": "My message goes here"
                  }
                }
              ]
            }
Enter fullscreen mode Exit fullscreen mode

AFTER:

      - name: Post to Slack
        uses: slackapi/slack-github-action@v2
        with:
          method: chat.postMessage
          token: ${{ secrets.SLACK_BOT_TOKEN }}
          payload: |
            channel: "C3P0ANDR2D2"
            blocks:
              - type: section
                text:
                  type: mrkdwn
                  text: "My message goes here"
Enter fullscreen mode Exit fullscreen mode

So much prettier and easier to read/maintain.

Note, you will have to shuffle some things around (like where the token is specified), and be explicit about the method you are using.

Here is an example of an update message

      - name: Update Slack with success
        if: ${{ success() }}
        uses: slackapi/slack-github-action@v2
        with:
          method: chat.update
          token: ${{ secrets.SLACK_BOT_TOKEN }}
          payload: |
            channel: "C3P0ANDR2D2"
            ts: ${{ steps.slack.outputs.ts }}
            blocks:
              - type: section
                text:
                  type: mrkdwn
                  text: "My updated test"
                fields:
                  - type: mrkdwn
                    text: "My updated field"
Enter fullscreen mode Exit fullscreen mode

I used Cursor to help me do all of the conversion from JSON to YAML, so this wasn't too painful of a change.

Happy Coding!

Top comments (0)