slack-github-action icon indicating copy to clipboard operation
slack-github-action copied to clipboard

Cannot parse env info in the payload

Open duthaho opened this issue 4 years ago • 2 comments

Description

How can I parse the env info to the payload to have the dynamic notification?

What type of issue is this? (place an x in one of the [ ])

  • [x] bug
  • [ ] enhancement (feature request)
  • [ ] question
  • [ ] documentation related
  • [x] example code related
- name: Send notify
        if: steps.changes.outputs.interview == 'true'
        uses: slackapi/[email protected]
        with:
          payload: '{"attachments": [{"color": "#3AA3E3", "author_name": "Pull request submitted by \"${{ env.AUTHOR_NAME }}\"", "title": "\"${{ env.PR_TITLE }}\"", "title_link": "\"${{ env.PR_RUN_LINK }}\""}]}'
        env:
          SLACK_WEBHOOK_URL: ${{ SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
          AUTHOR_NAME: $(jq --raw-output .pull_request.user.login $GITHUB_EVENT_PATH)
          AUTHOR_LINK: $(jq --raw-output .pull_request.user.html_url $GITHUB_EVENT_PATH)
          PR_TITLE: $(jq --raw-output .pull_request.title $GITHUB_EVENT_PATH)
          PR_RUN_LINK: $(jq --raw-output .pull_request.html_url $GITHUB_EVENT_PATH)
  • [ ] testing related
  • [ ] discussion

Requirements (place an x in each of the [ ])

  • [x] I've read and understood the Contributing guidelines and have done my best effort to follow them.
  • [x] I've read and agree to the Code of Conduct.
  • [x] I've searched for any related issues and avoided creating a duplicate issue.

Bug Report

Filling out the following details about bugs will help us solve your issue sooner.

Reproducible in:

package version:

node version:

OS version(s):

Steps to reproduce:

Expected result:

What you expected to happen

Actual result:

What actually happened

Attachments:

image

Logs, screenshots, screencast, sample project, funny gif, etc.

duthaho avatar Nov 27 '21 16:11 duthaho

Hey @duthaho,

I believe your YAML should tweak how to quote the payload property. We actually inject environment variables into the JSON payload to be delivered to the webhook in our GitHub Workflows for this project as part of the integration testing. Check this line of code out:

    - name: Post message to Slack via webhook
      id: slackWorkflow
      uses: ./
      with:
        # Workflow builder webhooks need to know the name of the keys in the payload in advance. Without normalizing, the github context payload keys can differ based on the GitHub trigger event type
        # Normalized payload with info pulled out from GitHub trigger event
        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 }}

Note that the payload is entirely quoted with double-quotes, whereas your example you use single quotes (if I understand correctly, escaping (i.e. \) does not work in YAML with single quotes). Maybe give that a shot and see if it works?

filmaj avatar Nov 29 '21 19:11 filmaj

hi @filmaj i have the same problem, but using a multiline yaml block...

    - name: commit-message
      run: echo "COMMIT_MSG=\"$(git log -1 --pretty=format:\"%s\")\"" >> $GITHUB_ENV
    - run: echo "msg: ${{ env.COMMIT_MSG }}"
    - uses: slackapi/[email protected]
      with:
        payload: |-
          {
            "blocks": [
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": "*:fire: ${{ github.actor }} broke the ${{ github.workflow }} job :fire:*\n\n${{ env.COMMIT_MSG }}\n${{github.sha}}"
                },
                "accessory": {
                  "type": "image",
                  "image_url": "https://tenor.com/view/mochi-mochi-peach-cat-cat-kitty-animated-cry-gif-17774448",
                  "alt_text": "images"
                }
              },
              {
                "type": "actions",
                "elements": [
                  {
                    "type": "button",
                    "text": {
                      "type": "plain_text",
                      "text": "View Job"
                    },
                    "url": "https://github.com//${{ github.repository}}/actions/runs/${{ github.run_id }}"
                  }
                ]
              }
            ]
          }
      env:
        SLACK_WEBHOOK_URL: ${{ inputs.slack_webhook_url }}

so the echo does of env.COMMIT_MSG works fine, but i cannot get it to work in the payload, its just empty, output like this:

|   '*:fire: nektos/act broke the test job :fire:*\n' +
|     '\n' +
|     '\n' +
|     'd7e9d942ce1d3467bc4012005d4ab19a55d63e28',

any advice here?

danturn avatar Nov 15 '22 11:11 danturn