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

AS_TOOK returns undefined when ran in multiple parallel workflow jobs

Open dimisjim opened this issue 3 years ago • 8 comments

Code used in multiple parallel jobs:

- name: Notify Slack
        if: contains(steps.deploy.outputs.result, 'successfully deployed')
        uses: 8398a7/action-slack@v3
        with:
          status: custom
          fields: workflow,job,commit,repo,ref,author,took
          custom_payload: |
            {
              username: 'github-actions',
              attachments: [{
                color: 'good',
                text: `blabla was successfully deployed with commit: ${process.env.AS_COMMIT} by ${{ github.actor }} via ${process.env.AS_WORKFLOW} & took ${process.env.AS_TOOK} to complete.`,
              }]
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_DEPLOYMENTS }}

Returns: image

Jobs look like this int he visual graph: image

It also happens when there is a single slack notif job that runs on its own, but still depends on multiple previous parallel jobs.

same code works for a job that's not ran in parallel with others

dimisjim avatar Apr 12 '21 09:04 dimisjim

Wouldn't it be fixed by adding MATRIX_CONTEXT: ${{ toJson(matrix) }} ?

refs: https://action-slack.netlify.app/fields

8398a7 avatar Apr 12 '21 10:04 8398a7

hmm this is not a matrix per se. Jobs run in parallel, by defining the same "needs" dependencies to previous jobs.

dimisjim avatar Apr 12 '21 10:04 dimisjim

I see. Maybe it's a pattern I'm not recognizing. Can you provide a minimal yaml that reproduces the problem?

8398a7 avatar Apr 12 '21 10:04 8398a7

Sure here you go:

name: CI main

on:
  pull_request:
  push:
    branches: 
      - master

jobs:
  InitialJob:
    runs-on: [self-hosted]

    steps:
      - name: Do stuff
        run: echo "I am doing stuff"

  ParallelJob1:
    runs-on: [self-hosted]
    needs: InitialJob

    steps:
      - name: Do stuff
        run: echo "I am doing stuff"

  ParallelJob2:
    runs-on: [self-hosted]
    needs: InitialJob

    steps:
      - name: Do stuff
        run: echo "I am doing stuff"

  ParallelJob3:
    runs-on: [self-hosted]
    needs: InitialJob

    steps:
      - name: Do stuff
        run: echo "I am doing stuff"

  NotifySlack:
    runs-on: [self-hosted]
    needs: [ParallelJob1, ParallelJob2, ParallelJob3]

    steps:
      - name: Notify Slack
        uses: 8398a7/action-slack@v3
        with:
          status: custom
          fields: workflow,job,commit,repo,ref,took
          custom_payload: |
            {
              username: 'github-actions',
              attachments: [{
                color: 'good',
                text: `This workflow took ${process.env.AS_TOOK} to complete.`,
              }]
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_DEPLOYMENTS }}

dimisjim avatar Apr 12 '21 10:04 dimisjim

Tried to use the matrix env var as suggested in https://github.com/8398a7/action-slack/issues/135#issuecomment-817685006 but got the same "undefined" result

dimisjim avatar Apr 12 '21 10:04 dimisjim

Thank you. I will investigate based on this.

8398a7 avatar Apr 12 '21 11:04 8398a7

I've done it the hacky way, using the GET /repos/{owner}/{repo}/actions/runs/{run_id} | jq -r '.created_at' API and doing a comparison like this, in a self-hosted centos 7 based runner:

      - name: Get workflow Run ID
        uses: octokit/[email protected]
        id: workflowRunId
        with:
          route: GET /repos/org/repo/actions/runs/${{ github.run_id }}
        env:
          GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
          
      - name: Get time diff between now and workflow run creation time
        id: workflowDuration
        run: |
          rpm -qa | grep dateutils || rpm -i https://download.opensuse.org/repositories/utilities/RHEL_7/x86_64/dateutils-0.4.4-44.1.x86_64.rpm
          NOW=`date -u +"%Y-%m-%dT%H:%M:%SZ"`
          DIFF=`ddiff -i "%Y-%m-%dT%H:%M:%SZ" "${{ fromJson(steps.workflowRunId.outputs.data).created_at }}" $NOW -f "%H°%M'%S\" to complete."`
          echo "::set-output name=time::$(echo $DIFF)"
      - name: Notify Slack
        uses: 8398a7/action-slack@v3
        with:
          status: custom
          fields: workflow,job,commit,repo,ref
          custom_payload: |
            {
              username: 'github-actions',
              attachments: [{
                color: 'good',
                text: `Took ${{ steps.workflowDuration.outputs.time }}`,
              }]
            }
        env:
          SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL_DEPLOYMENTS }}

dimisjim avatar Apr 13 '21 07:04 dimisjim

If there is any update on this, I would be very interested. I have a similar situation where it's only getting the time fro the latest job.

I don't know if it is possible, but maybe you could get the workflowRun time instead of the specific job?

Cussa avatar Jun 20 '23 14:06 Cussa