Skip to content

Commit

Permalink
Fixed to be able to use env of Step
Browse files Browse the repository at this point in the history
Use `event` github context object property rather than payload for repository and commit payload file integration test.

Tweak repository and commit fields in payload file test.

Tweak author field in payload file test.

Drop message, link to user profile for actor field.
  • Loading branch information
hnarimiya authored and Filip Maj committed May 17, 2023
1 parent 4eb7313 commit 2c89a30
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 16 deletions.
36 changes: 25 additions & 11 deletions .github/resources/payload-notification.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,27 @@
{
"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.repository.html_url }}/commit/${{ github.sha }}|${{ github.sha }}>"
}, {
"title": "Author",
"short": false,
"value": "<https://github.com/${{ github.actor }}|${{ github.actor }}>"
}, {
"title": "Workflow",
"short": false,
"value": "<${{ github.payload.repository.html_url }}/actions/runs/${{ github.runId }}|${{ github.workflow }}>"
}]
}]
}
9 changes: 6 additions & 3 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ jobs:
id: slackThreadResponse
uses: ./
with:
channel-id: ${{ secrets.SLACK_CHANNEL_ID }}
channel-id: ${{ secrets.SLACK_CHANNEL_ID }}
payload: |
{
"text": "This message should be posted as a response in thread",
Expand Down Expand Up @@ -72,7 +72,7 @@ jobs:
payload: "{\"author\":\"${{ github.event.sender.login }}\",\"url\":\"${{ env.URL}}\", \"repoName\":\"${{ github.event.repository.full_name }}\", \"status\":\"${{ job.status }}\"}"
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}


# Use the output from the `slackWorkflow` step
- name: Check Action output is not empty
Expand Down Expand Up @@ -103,7 +103,8 @@ jobs:
steps:
- uses: actions/checkout@v3
- run: npm ci && npm run build
- run: echo "${{ github.event_name }}"
- name: Dump out GitHub Context
run: echo '${{ toJSON(github) }}'
- name: Post message to Slack with Payload path
id: slackPayloadFile
uses: ./
Expand All @@ -112,6 +113,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
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 2 additions & 2 deletions src/slack-send.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 2c89a30

Please sign in to comment.