'Is there a variable in Github Actions that holds the total duration of a workflow run?

I'd like to add to the Slack message that notifies that a Github workflow run has finished, the total duration it took to run.

Does anyone know how can it be achieved? I couldn't find "total duration" in any of the contexts...



Solution 1:[1]

I don't think it's available from a context, but you could do it with two separate run steps:

  1. Store the current time as a timestamp (first step in workflow):

    - name: Set start timestamp
      id: start
      run: |
        printf '::set-output name=timestamp::%(%s)T\n'
    
  2. Calculate difference (at end of workflow):

    - name: Calculate duration
      run: |
        printf -v now '%(%s)T'
        duration=$((now - ${{ steps.start.outputs.timestamp }}))
        # Do something with duration, set as output to then use in Slack message
        # or similar
    

This puts the duration in seconds into the duration variable.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Benjamin W.