admin管理员组

文章数量:1122832

I have a Github Workflow and a JSON file in my repo. The directory looks something like this (The directories referenced in the code is slightly different but it doesn't really matter)

.github/workflows/my_workflow.yml

.github/workflows/data.json

In the code below I want to replace the JSON content that is assigned to the payload key with a reference to an actual json file: .github/workflows/data.json

The goal is to create a script where instead of hardcoding the JSON to the payload key, all that data is referenced from a JSON file that can be swapped out.

This query does what I was looking for but it created another problem: Created job that takes content of JSON file, passes to Slack API but content does not interpolate

name: GitHub Push Slack Notification
on: [push, pull_request]
permissions:
  contents: write
jobs:
  notify-slack:
    runs-on: ubuntu-latest
    steps:
      - name: Send message to Slack
        uses: slackapi/[email protected]
        with:
          payload: |
            {
              "blocks": [
                  {
                    "type": "section",
                    "text": {
                      "type": "mrkdwn",
                      "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n</${{ github.repository }}/actions/runs/${{ github.run_id }}|View details>\n\n*Developer:* ${{ github.actor }}\n*Repository:* ${{ github.repository }}\n*Branch:* ${{ github.ref_name }}\n*Commit Message:* ${{ github.event.head_commit.message }}"
                    }
                  },
                  {
                    "type": "divider"
                  }
                ]
             }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

The json file contents are the following:

{
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n</${{ github.repository }}/actions/runs/${{ github.run_id }}|View details>\n\n*Developer:* ${{ github.actor }}\n*Repository:* ${{ github.repository }}\n*Branch:* ${{ github.ref_name }}\n*Commit Message:* ${{ github.event.head_commit.message }}"
      }
    },
    {
      "type": "divider"
    }
  ]
}

EDIT

I have tried a number of things including creating an artifact out of the json content and then trying to reference the artifact from the Slack job. Nothing seems to work.

Each individual Job below works when I run this workflow. I can't figure out how to replace the JSON assigned to payload with the json that is created via the artifact (if there is an easier approach please feel free to demonstrate it)

I want to do (something like) this:

payload: directory/to/file.json

Code:

name: Echo JSON Content

on: [push, pull_request]
permissions:
  contents: write

jobs:

 # Works
  read-json:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the repository
        uses: actions/checkout@v3  # Ensure the repository is checked out

      - name: Read JSON content
        run: |
          # Check if the file exists before trying to read it
          if [ -f slack_message_payload/data.json ]; then
            cat slack_message_payload/data.json > json_payload.json
          else
            echo "Error: slack_message_payload/data.json not found!"
            exit 1
          fi

      - name: Upload JSON content as artifact
        uses: actions/upload-artifact@v3
        with:
          name: json-payload
          path: json_payload.json

 # Works
  use-json:
    needs: read-json
    runs-on: ubuntu-latest
    steps:
      - name: Download JSON artifact
        uses: actions/download-artifact@v3
        with:
          name: json-payload

      - name: Use JSON content
        run: |
          # Read the downloaded artifact and print the content
          json_content=$(cat json_payload.json)
          echo "The JSON content is: $json_content"

 # Works
  notify-slack:
    runs-on: ubuntu-latest
    steps:
      - name: Send message to Slack
        uses: slackapi/[email protected]
        with:
          payload: |
            {
              "blocks": [
                  {
                    "type": "section",
                    "text": {
                      "type": "mrkdwn",
                      "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n</${{ github.repository }}/actions/runs/${{ github.run_id }}|View details>\n\n*Developer:* ${{ github.actor }}\n*Repository:* ${{ github.repository }}\n*Branch:* ${{ github.ref_name }}\n*Commit Message:* ${{ github.event.head_commit.message }}"
                    }
                  },
                  {
                    "type": "divider"
                  }
                ]
             }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

Another try

I tried doing this and got the error below.

name: GitHub Push Slack Notification with JSON Payload

on: [push, pull_request]

jobs:
  read-json:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Read JSON content from file and set output
        id: read_json
        run: |
          # Read the JSON content from the file
          json_content=$(cat slack_message_payload/data.json)

          # Print the content for debugging
          echo "JSON content: $json_content"

          # Set the content as an output for use in another job
          echo "::set-output name=json_data::$json_content"

  notify-slack:
    needs: read-json  # This job depends on the 'read-json' job
    runs-on: ubuntu-latest

    steps:
      - name: Send message to Slack
        uses: slackapi/[email protected]
        with:
          payload: ${{ needs.read-json.outputs.json_data }}  # Use the JSON content from the previous job
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

From what I can tell the directory and file is available. Here is the error (I didn't post the entire thing it is actually much larger):

Run slackapi/[email protected]
  with:
    payload-file-path-parsed: true
  env:
    SLACK_WEBHOOK_URL: ***
    SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
no custom payload was passed in, using default payload that triggered the GitHub Action
axios post failed, double check the payload being sent includes the keys Slack expects
{"message":"Request failed with status code 400","name":"AxiosError","stack":"AxiosError: Request failed with status code 400\n    at settle (/home/runner/work/_actions/slackapi/slack-github-action/v1.27.0/dist/index.js:47327:12)\n    at IncomingMessage.handleStreamEnd (/home/runner/work/_actions/slackapi/slack-github-action/v1.27.0/dist/index.js:48443:11)\n 

Another Edit.

I read that echo "json_data=$json_content" >> $GITHUB_OUTPUT replaces ::set-output

name: GitHub Push Slack Notification with JSON Payload

on: [push, pull_request]

jobs:
  read-json:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Read JSON content from file and set output
        id: read_json
        run: |
          # Read the JSON content from the file
          json_content=$(cat ./slack_message_payload/data.json)

          # Print the content for debugging
          echo "JSON content: $json_content"

          # Set the content as an output for use in another job using GITHUB_OUTPUT
          echo "json_data=$json_content" >> $GITHUB_OUTPUT  # This replaces ::set-output

  notify-slack:
    needs: read-json  # This job depends on the 'read-json' job
    runs-on: ubuntu-latest

    steps:
      - name: Send message to Slack
        uses: slackapi/[email protected]
        with:
          payload: ${{ needs.read-json.outputs.json_data }}  # Use the JSON content from the previous job
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

ERROR

Run # Read the JSON content from the file
JSON content: {
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n</${{ github.repository }}/actions/runs/${{ github.run_id }}|View details>\n\n*Developer:* ${{ github.actor }}\n*Repository:* ${{ github.repository }}\n*Branch:* ${{ github.ref_name }}\n*Commit Message:* ${{ github.event.head_commit.message }}"
      }
    },
    {
      "type": "divider"
    }
  ]
}
Error: Unable to process file command 'output' successfully.
Error: Invalid format '  "blocks": ['

I have a Github Workflow and a JSON file in my repo. The directory looks something like this (The directories referenced in the code is slightly different but it doesn't really matter)

.github/workflows/my_workflow.yml

.github/workflows/data.json

In the code below I want to replace the JSON content that is assigned to the payload key with a reference to an actual json file: .github/workflows/data.json

The goal is to create a script where instead of hardcoding the JSON to the payload key, all that data is referenced from a JSON file that can be swapped out.

This query does what I was looking for but it created another problem: Created job that takes content of JSON file, passes to Slack API but content does not interpolate

name: GitHub Push Slack Notification
on: [push, pull_request]
permissions:
  contents: write
jobs:
  notify-slack:
    runs-on: ubuntu-latest
    steps:
      - name: Send message to Slack
        uses: slackapi/[email protected]
        with:
          payload: |
            {
              "blocks": [
                  {
                    "type": "section",
                    "text": {
                      "type": "mrkdwn",
                      "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|View details>\n\n*Developer:* ${{ github.actor }}\n*Repository:* ${{ github.repository }}\n*Branch:* ${{ github.ref_name }}\n*Commit Message:* ${{ github.event.head_commit.message }}"
                    }
                  },
                  {
                    "type": "divider"
                  }
                ]
             }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

The json file contents are the following:

{
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|View details>\n\n*Developer:* ${{ github.actor }}\n*Repository:* ${{ github.repository }}\n*Branch:* ${{ github.ref_name }}\n*Commit Message:* ${{ github.event.head_commit.message }}"
      }
    },
    {
      "type": "divider"
    }
  ]
}

EDIT

I have tried a number of things including creating an artifact out of the json content and then trying to reference the artifact from the Slack job. Nothing seems to work.

Each individual Job below works when I run this workflow. I can't figure out how to replace the JSON assigned to payload with the json that is created via the artifact (if there is an easier approach please feel free to demonstrate it)

I want to do (something like) this:

payload: directory/to/file.json

Code:

name: Echo JSON Content

on: [push, pull_request]
permissions:
  contents: write

jobs:

 # Works
  read-json:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout the repository
        uses: actions/checkout@v3  # Ensure the repository is checked out

      - name: Read JSON content
        run: |
          # Check if the file exists before trying to read it
          if [ -f slack_message_payload/data.json ]; then
            cat slack_message_payload/data.json > json_payload.json
          else
            echo "Error: slack_message_payload/data.json not found!"
            exit 1
          fi

      - name: Upload JSON content as artifact
        uses: actions/upload-artifact@v3
        with:
          name: json-payload
          path: json_payload.json

 # Works
  use-json:
    needs: read-json
    runs-on: ubuntu-latest
    steps:
      - name: Download JSON artifact
        uses: actions/download-artifact@v3
        with:
          name: json-payload

      - name: Use JSON content
        run: |
          # Read the downloaded artifact and print the content
          json_content=$(cat json_payload.json)
          echo "The JSON content is: $json_content"

 # Works
  notify-slack:
    runs-on: ubuntu-latest
    steps:
      - name: Send message to Slack
        uses: slackapi/[email protected]
        with:
          payload: |
            {
              "blocks": [
                  {
                    "type": "section",
                    "text": {
                      "type": "mrkdwn",
                      "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|View details>\n\n*Developer:* ${{ github.actor }}\n*Repository:* ${{ github.repository }}\n*Branch:* ${{ github.ref_name }}\n*Commit Message:* ${{ github.event.head_commit.message }}"
                    }
                  },
                  {
                    "type": "divider"
                  }
                ]
             }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

Another try

I tried doing this and got the error below.

name: GitHub Push Slack Notification with JSON Payload

on: [push, pull_request]

jobs:
  read-json:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Read JSON content from file and set output
        id: read_json
        run: |
          # Read the JSON content from the file
          json_content=$(cat slack_message_payload/data.json)

          # Print the content for debugging
          echo "JSON content: $json_content"

          # Set the content as an output for use in another job
          echo "::set-output name=json_data::$json_content"

  notify-slack:
    needs: read-json  # This job depends on the 'read-json' job
    runs-on: ubuntu-latest

    steps:
      - name: Send message to Slack
        uses: slackapi/[email protected]
        with:
          payload: ${{ needs.read-json.outputs.json_data }}  # Use the JSON content from the previous job
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

From what I can tell the directory and file is available. Here is the error (I didn't post the entire thing it is actually much larger):

Run slackapi/[email protected]
  with:
    payload-file-path-parsed: true
  env:
    SLACK_WEBHOOK_URL: ***
    SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK
no custom payload was passed in, using default payload that triggered the GitHub Action
axios post failed, double check the payload being sent includes the keys Slack expects
{"message":"Request failed with status code 400","name":"AxiosError","stack":"AxiosError: Request failed with status code 400\n    at settle (/home/runner/work/_actions/slackapi/slack-github-action/v1.27.0/dist/index.js:47327:12)\n    at IncomingMessage.handleStreamEnd (/home/runner/work/_actions/slackapi/slack-github-action/v1.27.0/dist/index.js:48443:11)\n 

Another Edit.

I read that echo "json_data=$json_content" >> $GITHUB_OUTPUT replaces ::set-output

name: GitHub Push Slack Notification with JSON Payload

on: [push, pull_request]

jobs:
  read-json:
    runs-on: ubuntu-latest

    steps:
      - name: Checkout repository
        uses: actions/checkout@v3

      - name: Read JSON content from file and set output
        id: read_json
        run: |
          # Read the JSON content from the file
          json_content=$(cat ./slack_message_payload/data.json)

          # Print the content for debugging
          echo "JSON content: $json_content"

          # Set the content as an output for use in another job using GITHUB_OUTPUT
          echo "json_data=$json_content" >> $GITHUB_OUTPUT  # This replaces ::set-output

  notify-slack:
    needs: read-json  # This job depends on the 'read-json' job
    runs-on: ubuntu-latest

    steps:
      - name: Send message to Slack
        uses: slackapi/[email protected]
        with:
          payload: ${{ needs.read-json.outputs.json_data }}  # Use the JSON content from the previous job
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
          SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

ERROR

Run # Read the JSON content from the file
JSON content: {
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|View details>\n\n*Developer:* ${{ github.actor }}\n*Repository:* ${{ github.repository }}\n*Branch:* ${{ github.ref_name }}\n*Commit Message:* ${{ github.event.head_commit.message }}"
      }
    },
    {
      "type": "divider"
    }
  ]
}
Error: Unable to process file command 'output' successfully.
Error: Invalid format '  "blocks": ['
Share Improve this question edited Nov 27, 2024 at 18:13 William asked Nov 22, 2024 at 19:08 WilliamWilliam 4,55818 gold badges65 silver badges116 bronze badges 4
  • There are multiple issues here. For the file version, download artifact in the same job and then use its path for payload-file-path. See Gathering details of the payload from a saved file. For JSON, it's a multiline. Use jq to compact it to a single line and that should work. Otherwise, set it as a multiline string. – Azeem Commented Nov 23, 2024 at 6:00
  • Also, the job level output param is missing. See Passing information between jobs to fix that. – Azeem Commented Nov 23, 2024 at 6:02
  • I see the pieces but I have no idea how to weave it together. – William Commented Nov 26, 2024 at 17:56
  • I posted another question where I got closer with less code but ran into a code interpolation problem: stackoverflow.com/questions/79227929/… – William Commented Nov 26, 2024 at 18:50
Add a comment  | 

1 Answer 1

Reset to default 0 +100

Here's a working example with payload-file-path:

name: slack_test

on: workflow_dispatch

jobs:
  ci:
    runs-on: ubuntu-latest

    steps:
    - name: Payload
      env:
        payload: |
          {
            "blocks": [
              {
                "type": "section",
                "text": {
                  "type": "mrkdwn",
                  "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n<https://github.com/${{ github.repository }}/actions/runs/${{ github.run_id }}|View details>\n\n*Developer:* ${{ github.actor }}\n*Repository:* ${{ github.repository }}\n*Branch:* ${{ github.ref_name }}\n*Commit Message:* ${{ github.event.head_commit.message }}"
                }
              },
              {
                "type": "divider"
              }
            ]
          }
      run: |
        echo "$payload" > payload.json
        jq . payload.json

    - name: Send message to Slack
      uses: slackapi/slack-github-action@v2
      with:
        webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
        webhook-type: incoming-webhook
        payload-file-path: payload.json
        payload-templated: true

payload-templated: true is required for parsing templated variables in the payload file.

Workflow run output:

Slack message:


UPDATE # 1

With the separate payload file and payload-templated: true, the expressions ${{...}} are not being properly substituted. On slack message, such values are being shown as ???.

The issue has already been reported and being discussed here: https://github.com/slackapi/slack-github-action/issues/203

Workflow:

name: slack_test

on: workflow_dispatch

jobs:
  set-payload:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        sparse-checkout: |
          slack_payload.json
    - name: Upload payload file
      uses: actions/upload-artifact@v4
      with:
        name: slack_payload
        path: slack_payload.json

  send-message:
    needs: set-payload
    runs-on: ubuntu-latest

    steps:
    - name: Download payload file
      uses: actions/download-artifact@v4
      with:
        name: slack_payload

    - name: Send message to Slack
      uses: slackapi/slack-github-action@v2
      with:
        webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
        webhook-type: incoming-webhook
        payload-file-path: slack_payload.json
        payload-templated: true

UPDATE # 2

Here's the version with env vars substitutions that is working fine:

name: slack_test

on: workflow_dispatch

jobs:
  slack:
    runs-on: ubuntu-latest

    steps:
    - name: Checkout
      uses: actions/checkout@v4
      with:
        sparse-checkout: |
          slack_payload.json

    - name: Set env vars
      env:
        GITHUB_EVENT_HEAD_COMMIT_MESSAGE: '${{ github.event.head_commit.message }}'
      uses: iamazeem/substitute-action@v1
      with:
        input-files: |
          slack_payload.json

    - name: Dump payload file
      run: cat slack_payload.json

    - name: Send message to Slack
      uses: slackapi/slack-github-action@v2
      with:
        webhook: ${{ secrets.SLACK_WEBHOOK_URL }}
        webhook-type: incoming-webhook
        payload-file-path: slack_payload.json

Payload file with default env vars:

{
  "blocks": [
    {
      "type": "section",
      "text": {
        "type": "mrkdwn",
        "text": ":large_green_circle: *Build succeeded* :large_green_circle:\n<https://github_com/${GITHUB_REPOSITORY}/actions/runs/${GITHUB_RUN_ID}|View details>\n\n*Developer:* ${GITHUB_ACTOR}\n*Repository:* ${GITHUB_REPOSITORY}\n*Branch:* ${GITHUB_REF_NAME}\n*Commit Message:* ${GITHUB_EVENT_HEAD_COMMIT_MESSAGE}"
      }
    },
    {
      "type": "divider"
    }
  ]
}

Any other non-default variable may be set and used in the payload file as needed. For example, GITHUB_EVENT_HEAD_COMMIT_MESSAGE is not available as the default variable.

Output (slack message):

本文标签: How to reference a JSON files content from GIthub Actions and assign it to Slack PayloadStack Overflow