Update calculate-build-duration.yml #4
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: calculate-build-duration | |
on: | |
push: | |
branches: [ "main" ] | |
pull_request: | |
branches: [ "main" ] | |
workflow_dispatch: | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
# only needed if we have a second job where we would reference this | |
outputs: | |
start_time: ${{ steps.set-start-time.outputs.start_time }} | |
steps: | |
- name: Set start time | |
id: set-start-time | |
run: echo "start_time=$(date +%s)" >> $GITHUB_OUTPUT | |
- uses: actions/checkout@v4 | |
- name: Write GitHub context to log | |
env: | |
GITHUB_CONTEXT: ${{ toJSON(github) }} | |
run: echo "$GITHUB_CONTEXT" | |
- name: Write job context to log | |
env: | |
JOB_CONTEXT: ${{ toJSON(job) }} | |
run: echo "$JOB_CONTEXT" | |
- run: sleep 5s | |
- name: Calculate build duration (simple) | |
run: | | |
START_TIME=${{ steps.set-start-time.outputs.start_time }} | |
END_TIME=$(date +%s) | |
DURATION_SECONDS=$((END_TIME - START_TIME)) | |
DURATION_MINUTES=$((DURATION_SECONDS / 60)) | |
echo "Build duration (in seconds): $DURATION_SECONDS second(s)" | |
echo "Build duration (in minutes): $DURATION_MINUTES minute(s)" | |
- name: Calculate build duration in minutes using GitHub API | |
env: | |
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
run: | | |
JOB_URL="${{ github.api_url }}/repos/${{ github.repository }}/actions/runs/${{ github.run_id }}/jobs" | |
JOB_JSON=$(curl -s -H "Authorization: token $GITHUB_TOKEN" $JOB_URL) | |
START_TIME=$(echo $JOB_JSON | jq -r '.jobs[] | select(.name == "build") | .started_at' | xargs -I {} date -j -f "%Y-%m-%dT%H:%M:%SZ" "{}" +%s) | |
END_TIME=$(date +%s) | |
DURATION_SECONDS=$(( (END_TIME - START_TIME) )) | |
DURATION_MINUTES=$(( (END_TIME - START_TIME) / 60 )) | |
echo "Build duration: $DURATION_SECONDS seconds" | |
echo "Build duration: $DURATION_MINUTES minutes" |