Fixed to be able to use env of Step (#200)
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. Co-authored-by: hideki narimiya <[email protected]>
This commit is contained in:
co-authored by
hideki narimiya
parent
4eb7313607
commit
81a1dc0cd9
@@ -1,13 +1,27 @@
|
|||||||
{
|
{
|
||||||
"text": "Incoming Webhook test for slack send",
|
"text": "A GitHub Action Event ${{ github.eventName }} has ${{ env.JOB_STATUS }}",
|
||||||
"blocks": [
|
"attachments": [{
|
||||||
{
|
"color": "${{ env.ATTACHMENT_COLOR }}",
|
||||||
"type": "section",
|
"fields": [{
|
||||||
"text": {
|
"title": "Repository",
|
||||||
"type": "plain_text",
|
"short": false,
|
||||||
"text": "Content of the event name: {{ github.event_name }}",
|
"value": "<${{ github.payload.repository.html_url }}|${{ github.payload.repository.full_name }}>"
|
||||||
"emoji": true
|
}, {
|
||||||
}
|
"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 }}>"
|
||||||
|
}]
|
||||||
|
}]
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -36,7 +36,7 @@ jobs:
|
|||||||
id: slackThreadResponse
|
id: slackThreadResponse
|
||||||
uses: ./
|
uses: ./
|
||||||
with:
|
with:
|
||||||
channel-id: ${{ secrets.SLACK_CHANNEL_ID }}
|
channel-id: ${{ secrets.SLACK_CHANNEL_ID }}
|
||||||
payload: |
|
payload: |
|
||||||
{
|
{
|
||||||
"text": "This message should be posted as a response in thread",
|
"text": "This message should be posted as a response in thread",
|
||||||
@@ -72,7 +72,7 @@ jobs:
|
|||||||
payload: "{\"author\":\"${{ github.event.sender.login }}\",\"url\":\"${{ env.URL}}\", \"repoName\":\"${{ github.event.repository.full_name }}\", \"status\":\"${{ job.status }}\"}"
|
payload: "{\"author\":\"${{ github.event.sender.login }}\",\"url\":\"${{ env.URL}}\", \"repoName\":\"${{ github.event.repository.full_name }}\", \"status\":\"${{ job.status }}\"}"
|
||||||
env:
|
env:
|
||||||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
|
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
|
||||||
|
|
||||||
|
|
||||||
# Use the output from the `slackWorkflow` step
|
# Use the output from the `slackWorkflow` step
|
||||||
- name: Check Action output is not empty
|
- name: Check Action output is not empty
|
||||||
@@ -103,7 +103,8 @@ jobs:
|
|||||||
steps:
|
steps:
|
||||||
- uses: actions/checkout@v3
|
- uses: actions/checkout@v3
|
||||||
- run: npm ci && npm run build
|
- 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
|
- name: Post message to Slack with Payload path
|
||||||
id: slackPayloadFile
|
id: slackPayloadFile
|
||||||
uses: ./
|
uses: ./
|
||||||
@@ -112,6 +113,8 @@ jobs:
|
|||||||
env:
|
env:
|
||||||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_INCOMING_WEBHOOK_URL }}
|
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_INCOMING_WEBHOOK_URL }}
|
||||||
SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
|
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
|
# Use the output from the `slackIncoming` step
|
||||||
- name: Check Action output is not empty
|
- name: Check Action output is not empty
|
||||||
|
|||||||
@@ -66,6 +66,9 @@ or
|
|||||||
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
|
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
|
## 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.
|
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.
|
||||||
|
|||||||
+2
-2
@@ -39,8 +39,8 @@ module.exports = async function slackSend(core) {
|
|||||||
try {
|
try {
|
||||||
payload = await fs.readFile(path.resolve(payloadFilePath), 'utf-8');
|
payload = await fs.readFile(path.resolve(payloadFilePath), 'utf-8');
|
||||||
// parse github context variables
|
// parse github context variables
|
||||||
const context = { github: github.context };
|
const context = { github: github.context, env: process.env };
|
||||||
const payloadString = payload.replace('$', '');
|
const payloadString = payload.replaceAll('${{', '{{');
|
||||||
payload = markup.up(payloadString, context);
|
payload = markup.up(payloadString, context);
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
// passed in payload file path was invalid
|
// passed in payload file path was invalid
|
||||||
|
|||||||
Reference in New Issue
Block a user