Can you get the time of a previous job?

Is there a way for me to retrieve the time a previous job took for finishing?
I have two separate jobs, where the second one depends on the first one finishing and I would like to use the time of the first job to display in the second one.

Is there a way?

In the second job, you can try using the “List jobs for a workflow run” API to get the timestamps (started_at and completed_at) of the first job, and then use the get timestamps to calculate the duration time of the first job.
A simple example:

    name: Job 1
    runs-on: ubuntu-latest
      some steps in job1

    name: Job 2
    needs: [job1]
    runs-on: ubuntu-latest
      - name: Get duration time of job1
        run: |
          echo "====================== Get timestamps of job1 ======================"
          pat=${{ secrets.GITHUB_TOKEN }}
          base64AuthInfo='$pat | base64 -e'
          response=$(curl --location --request GET \
          --url '${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs' \
          --header 'Authorization: $base64AuthInfo' \
          --header 'Content-Type: application/json')
          started_at=$(echo $response | jq -r '.jobs[] | select(.name=="Job 1") | .started_at')
          echo "started_at = $started_at"
          completed_at=$(echo $response | jq -r '.jobs[] | select(.name=="Job 1")| .completed_at')
          echo "completed_at = $completed_at"
          echo "====================== Calculate duration time of job1 ======================"
          started_time=$(date -d "$started_at" +%s)
          completed_time=$(date -d "$completed_at" +%s)
          interval=$(( $completed_time - $started_time ))
          echo "interval = $interval"s
          duration_time=$(echo "$(($interval/60))m $(($interval%60))s")
          echo "duration_time = $duration_time"

In the select function (select(.name==“Job 1”)) of jq, if you have set a name for job1, use the this name here. Just like as my above example is using the name “Job 1”.

    name: Job 1

If you do not set name for job1, the job id (job1) will be used to set the job name by default, so you need to use the job id in the select function (select(.name==“job1”)).

1 Like


How are things going? Is my suggestion helpful to you? Have you tried it?
If you have any updates or questions about this ticket, feel free to tell me.