Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AS_TOOK returns undefined when ran in multiple parallel workflow jobs #135

Open
dimisjim opened this issue Apr 12, 2021 · 8 comments
Open

Comments

@dimisjim
Copy link
Contributor

dimisjim commented Apr 12, 2021

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

@8398a7
Copy link
Owner

8398a7 commented Apr 12, 2021

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

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

@dimisjim
Copy link
Contributor Author

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

@8398a7
Copy link
Owner

8398a7 commented Apr 12, 2021

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

@dimisjim
Copy link
Contributor Author

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
Copy link
Contributor Author

Tried to use the matrix env var as suggested in #135 (comment) but got the same "undefined" result

@8398a7
Copy link
Owner

8398a7 commented Apr 12, 2021

Thank you. I will investigate based on this.

@dimisjim
Copy link
Contributor Author

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 }}

@Cussa
Copy link

Cussa commented Jun 20, 2023

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants