Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed to be able to use env of Step #159

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 29 additions & 11 deletions .github/resources/payload-notification.json
Original file line number Diff line number Diff line change
@@ -1,13 +1,31 @@
{
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some of the fields in this payload are not showing up correctly. Attached is a screenshot of the GitHub CI run from #163 (which is a copy of this PR), and how it looks like in our Slack workspace where the message gets posted:

Screenshot 2023-02-28 at 3 32 26 PM

In particular, it seems the following fields do not work as desired from the integration_test_file_payload test from main.yml: env.JOB_STATUS, github.payload.head_commit.url, github.payload.head_commit.author.name, github.payload.head_commit.message.

Any idea why this might be happening?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same seems to happen for us

"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 }}>"
}]
}]
}
3 changes: 2 additions & 1 deletion .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: ./
Expand All @@ -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
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