Repository navigation
Update GitHub Clones Metrics #6
This file contains hidden or 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: Update GitHub Clones Metrics | |
| on: | |
| schedule: | |
| # Run daily at 3 AM UTC. GitHub keeps only 14 days of clone traffic, | |
| # so a daily run leaves a wide margin before data is lost. | |
| - cron: '0 3 * * *' | |
| workflow_dispatch: | |
| permissions: | |
| contents: write | |
| pull-requests: write | |
| # Every run pushes to shared data branches; never let two runs overlap. | |
| concurrency: | |
| group: github-clones | |
| cancel-in-progress: false | |
| env: | |
| CSV: docs/usage-metrics/clones.csv | |
| jobs: | |
| collect-clones: | |
| runs-on: ubuntu-latest | |
| timeout-minutes: 30 | |
| steps: | |
| - name: Checkout main | |
| uses: actions/checkout@3d3c42e5aac5ba805825da76410c181273ba90b1 # v7.0.1 | |
| with: | |
| # Data branches are always rebuilt on top of main, whatever ref triggered the run. | |
| ref: main | |
| - name: Set up Go | |
| uses: actions/setup-go@b7ad1dad31e06c5925ef5d2fc7ad053ef454303e # v7.0.0 | |
| with: | |
| go-version-file: 'usage-metrics/go.mod' | |
| - name: Resolve dates and branches | |
| id: dates | |
| run: | | |
| TODAY=$(date -u +%Y-%m-%d) | |
| YESTERDAY=$(date -u -d "$TODAY -1 day" +%Y-%m-%d) | |
| MONTH=$(date -u +%Y-%m) | |
| PREV_LAST_DAY=$(date -u -d "$MONTH-01 -1 day" +%Y-%m-%d) | |
| PREV_MONTH=${PREV_LAST_DAY%-*} | |
| { | |
| echo "today=$TODAY" | |
| echo "yesterday=$YESTERDAY" | |
| echo "branch=$MONTH-clones" | |
| echo "prev_branch=$PREV_MONTH-clones" | |
| echo "prev_month=$PREV_MONTH" | |
| echo "prev_last_day=$PREV_LAST_DAY" | |
| } >> "$GITHUB_OUTPUT" | |
| echo "Data branch: $MONTH-clones (previous: $PREV_MONTH-clones, last day $PREV_LAST_DAY)" | |
| - name: Configure git and fetch the data branches | |
| env: | |
| BRANCH: ${{ steps.dates.outputs.branch }} | |
| PREV_BRANCH: ${{ steps.dates.outputs.prev_branch }} | |
| run: | | |
| git config user.name "github-actions[bot]" | |
| git config user.email "github-actions[bot]@users.noreply.github.com" | |
| for b in "$BRANCH" "$PREV_BRANCH"; do | |
| if git ls-remote --exit-code --heads origin "$b" > /dev/null; then | |
| git fetch --depth=1 origin "+refs/heads/$b:refs/remotes/origin/$b" | |
| fi | |
| done | |
| - name: Finalise the previous month and open its pull request | |
| env: | |
| GH_TOKEN: ${{ secrets.GITHUB_TOKEN }} | |
| # Fine-grained PAT with "Administration: read" on this repository. | |
| # The traffic API needs push access, which GITHUB_TOKEN cannot be granted. | |
| TRAFFIC_TOKEN: ${{ secrets.TRAFFIC_TOKEN }} | |
| PREV_BRANCH: ${{ steps.dates.outputs.prev_branch }} | |
| PREV_MONTH: ${{ steps.dates.outputs.prev_month }} | |
| PREV_LAST_DAY: ${{ steps.dates.outputs.prev_last_day }} | |
| TODAY: ${{ steps.dates.outputs.today }} | |
| run: | | |
| if ! git rev-parse --verify --quiet "origin/$PREV_BRANCH" > /dev/null; then | |
| echo "No branch $PREV_BRANCH on origin, nothing to finalise" | |
| exit 0 | |
| fi | |
| EXISTING_PR=$(gh pr list --head "$PREV_BRANCH" --state all --json number --jq '.[0].number') | |
| if [ -n "$EXISTING_PR" ]; then | |
| echo "PR #$EXISTING_PR already exists for $PREV_BRANCH" | |
| exit 0 | |
| fi | |
| # Rebuild the branch on main, restore its CSV, and upsert the final numbers for the | |
| # month's last days: the API window still covers them during the first days of the new month. | |
| git checkout -B "$PREV_BRANCH" origin/main | |
| git checkout "origin/$PREV_BRANCH" -- "$CSV" | |
| (cd usage-metrics && GH_TOKEN="$TRAFFIC_TOKEN" go run collect.go clones -csv "../$CSV" -until "$PREV_LAST_DAY") | |
| git add "$CSV" | |
| if git diff --staged --quiet; then | |
| echo "$PREV_BRANCH has no data beyond main, nothing to publish" | |
| exit 0 | |
| fi | |
| git commit -m "chore(metrics): update github clones ($TODAY)" | |
| git push --force-with-lease -u origin "$PREV_BRANCH" | |
| gh pr create \ | |
| --head "$PREV_BRANCH" \ | |
| --base main \ | |
| --title "chore(metrics): update github clones for $PREV_MONTH" \ | |
| --body "Automated update of GitHub clone traffic for $PREV_MONTH, collected daily by the Update GitHub Clones Metrics workflow. Merging it publishes the data at https://golang.testcontainers.org/usage-metrics/clones/." | |
| - name: Update the current month | |
| env: | |
| TRAFFIC_TOKEN: ${{ secrets.TRAFFIC_TOKEN }} | |
| BRANCH: ${{ steps.dates.outputs.branch }} | |
| PREV_BRANCH: ${{ steps.dates.outputs.prev_branch }} | |
| YESTERDAY: ${{ steps.dates.outputs.yesterday }} | |
| TODAY: ${{ steps.dates.outputs.today }} | |
| run: | | |
| # The branch is rebuilt from main on every run: one commit, only the CSV differs. | |
| git checkout -B "$BRANCH" origin/main | |
| if git rev-parse --verify --quiet "origin/$BRANCH" > /dev/null; then | |
| git checkout "origin/$BRANCH" -- "$CSV" | |
| elif git rev-parse --verify --quiet "origin/$PREV_BRANCH" > /dev/null; then | |
| # New month: start from last month's rows so this branch stays a superset of main. | |
| git checkout "origin/$PREV_BRANCH" -- "$CSV" | |
| fi | |
| # Never store the in-progress UTC day; it is completed on tomorrow's run. | |
| (cd usage-metrics && GH_TOKEN="$TRAFFIC_TOKEN" go run collect.go clones -csv "../$CSV" -until "$YESTERDAY") | |
| git add "$CSV" | |
| if git diff --staged --quiet; then | |
| echo "No new clone data for $BRANCH" | |
| exit 0 | |
| fi | |
| git commit -m "chore(metrics): update github clones ($TODAY)" | |
| git push --force-with-lease -u origin "$BRANCH" |