diff --git a/.github/resources/payload-notification.json b/.github/resources/payload-notification.json index 13fdef46..16fc0e93 100644 --- a/.github/resources/payload-notification.json +++ b/.github/resources/payload-notification.json @@ -1,13 +1,31 @@ { - "text": "Incoming Webhook test for slack send", - "blocks": [ - { - "type": "section", - "text": { - "type": "plain_text", - "text": "Content of the event name: {{ github.event_name }}", - "emoji": true - } - } - ] + "text": "A GitHub Action Event ${{ github.eventName }} has ${{ env.JOB_STATUS }}", + "attachments": [{ + "color": "${{ env.ATTACHMENT_COLOR }}", + "fields": [{ + "title": "Repository", + "short": false, + "value": "<${{ github.payload.repository.html_url }}|${{ github.payload.repository.full_name }}>" + }, { + "title": "Ref", + "short": false, + "value": "${{ github.ref }}" + }, { + "title": "Commit", + "short": false, + "value": "<${{ github.payload.head_commit.url }}|${{ github.sha }}>" + }, { + "title": "Author", + "short": false, + "value": "${{ github.payload.head_commit.author.name }}" + }, { + "title": "Message", + "short": false, + "value": "${{ github.payload.head_commit.message }}" + }, { + "title": "Workflow", + "short": false, + "value": "<${{ github.payload.repository.html_url }}/actions/runs/${{ github.runId }}|${{ github.workflow }}>" + }] + }] } diff --git a/.github/workflows/main.yml b/.github/workflows/main.yml index 8d7a6efd..2d443395 100644 --- a/.github/workflows/main.yml +++ b/.github/workflows/main.yml @@ -103,7 +103,6 @@ jobs: steps: - uses: actions/checkout@v3 - run: npm ci && npm run build - - run: echo "${{ github.event_name }}" - name: Post message to Slack with Payload path id: slackPayloadFile uses: ./ @@ -112,6 +111,8 @@ jobs: env: SLACK_WEBHOOK_URL: ${{ secrets.SLACK_INCOMING_WEBHOOK_URL }} SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK + JOB_STATUS: ${{ job.status }} + ATTACHMENT_COLOR: ${{ (job.status == 'success' && 'good') || (job.status == 'failure' && 'danger') || 'warning' }} # Use the output from the `slackIncoming` step - name: Check Action output is not empty diff --git a/README.md b/README.md index da049670..27146785 100644 --- a/README.md +++ b/README.md @@ -66,6 +66,9 @@ or SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }} ``` +> **Warning** +> [github actions contexts](https://docs.github.com/en/actions/learn-github-actions/contexts) is not available in payload-file-path. Instead, you can use the context from [@actions/github](https://github.com/actions/toolkit/tree/main/packages/github) or the env defined in the step. + ## Technique 2: Slack App By creating a new Slack app or using an existing one, this approach allows your GitHub Actions job to post a message in a Slack channel or direct message by utilizing the [chat.postMessage](https://api.slack.com/methods/chat.postMessage) API method. Using this approach you can instantly post a message without setting up Slack workflows. diff --git a/src/slack-send.js b/src/slack-send.js index ed350e87..86c99ac4 100644 --- a/src/slack-send.js +++ b/src/slack-send.js @@ -39,8 +39,8 @@ module.exports = async function slackSend(core) { try { payload = await fs.readFile(path.resolve(payloadFilePath), 'utf-8'); // parse github context variables - const context = { github: github.context }; - const payloadString = payload.replace('$', ''); + const context = { github: github.context, env: process.env }; + const payloadString = payload.replaceAll('${{', '{{'); payload = markup.up(payloadString, context); } catch (error) { // passed in payload file path was invalid