📑

GHAのslack-github-actionをv1からv2へのバージョンアップ時にpayloadのattachmentsで困っている人用

2024/12/24に公開

✨🎄✨メリークリスマス✨🎄✨

結論だけ書く。

v1

例えば以下のようなステップ定義だとすると

...

- name: send workflow result to slack channel
  uses: slackapi/slack-github-action@v1.27.0
  with:
    payload: |
      {
        "attachments": [
          {
            "pretext": "",
            "color": "${{ inputs.conclusion == 'success' && '36a64f' || 'f26268' }}",
            "fields": [
              {
                "title": "Workflow",
                "short": true,
                "value": "${{ env.WORKFLOW }}"
              },
              {
                "title": "Action URL",
                "short": true,
                "value": "${{ env.ACTION_URL }}"
              },
              {
                "title": "Title",
                "short": true,
                "value": "${{ env.PR_TITLE }}"
              },
              {
                "title": "Pull Request URL",
                "short": true,
                "value": "${{ env.PR_URL }}"
              }
            ]
          }
        ]
      }
  env:
    WORKFLOW: ${{ github.workflow }}
    ACTION_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
    PR_TITLE: ${{ github.event.pull_request.title }}
    PR_URL: ${{ github.event.pull_request.html_url || github.event.head_commit.url }}
    SLACK_WEBHOOK_URL: ${{ inputs.webhook_url }}
    SLACK_WEBHOOK_TYPE: INCOMING_WEBHOOK

以下のように定義し直すと動く。

v2

...

- name: send workflow result to slack channel
  uses: slackapi/slack-github-action@v2.0.0
  with:
    webhook-type: incoming-webhook
    payload: |
      attachments:
        - color: "${{ inputs.conclusion == 'success' && '36a64f' || 'f26268' }}"
          fields:
            - title: "Workflow"
              short: true
              value: "${{ env.WORKFLOW }}"
            - title: "Action URL"
              short: true
              value: "${{ env.ACTION_URL }}"
            - title: "Title"
              short: true
              value: "${{ env.PR_TITLE }}"
            - title: "Pull Request URL"
              short: true
              value: "${{ env.PR_URL }}"
    env:
      WORKFLOW: ${{ github.workflow }}
      ACTION_URL: ${{ github.server_url }}/${{ github.repository }}/actions/runs/${{ github.run_id }}
      PR_TITLE: ${{ github.event.pull_request.title }}
      PR_URL: ${{ github.event.pull_request.html_url || github.event.head_commit.url }}
      SLACK_WEBHOOK_URL: ${{ inputs.webhook_url }}

コード全体の例はこちら

おわり

✨🎄✨メリークリスマス✨🎄✨

Discussion