Skip to content

👀 Collect Stale Environments 👾 TaylorHalf #85

👀 Collect Stale Environments 👾 TaylorHalf

👀 Collect Stale Environments 👾 TaylorHalf #85

name: 👀 Collect Stale Environments
run-name: 👀 Collect Stale Environments 👾 ${{ github.actor }}
on:
push:
branches:
- feature/CB2-11814
# workflow_dispatch:
# inputs:
# dry-run:
# description: Don't perform action, just output information
# type: boolean
# default: false
permissions:
id-token: write
contents: write
jobs:
collect-stale-branches:
name: Collect Stale branches
env:
GH_TOKEN: ${{ secrets.GH_TOKEN }}
runs-on: X64
steps:
- name: checkout
uses: actions/checkout@v4
- name: List all branches with pagination
id: branches
run: |
page=1
per_page=100
branches=()
while true; do
response=$(gh api \
-H "Accept: application/vnd.github+json" \
-H "X-GitHub-Api-Version: 2022-11-28" \
/repos/dvsa/cvs-svc-app-logs/branches?per_page=$per_page\&page=$page \
--jq '.[].name | select(test("^(main|master|develop|devops|HEAD)$|^(release/.*)$") | not)')
if [[ -z "$response" ]]; then
break
fi
branches+=($response)
((page++))
done
branches_json=$(printf '%s\n' "${branches[@]}" | jq -R . | jq -s .)
echo "$branches_json" > branches.json
echo "branches=$(echo $branches_json | jq -c)" >> $GITHUB_OUTPUT
- name: Test Fetch Commit Dates for All Branches
id: stale_branches
run: |
active_cutoff=$(date -v -30d +%s)
for branch in $(jq -r '.[]' <<< '${{ steps.branches.outputs.branches }}'); do
commit_dates=$(gh api -H "Accept: application/vnd.github+json" -H "X-GitHub-Api-Version: 2022-11-28" \
/repos/dvsa/cvs-svc-app-logs/commits?sha="$branch" --jq '.[].commit.committer.date' 2>/dev/null)
# Get last commit for this branch
last_commit_date=$(gh api repos/$GITHUB_REPOSITORY/branches/$branch --jq '.commit.commit | .author.date')
# convert to epoch time
epoch=$(date -j -f "%Y-%m-%dT%H:%M:%SZ" "$last_commit_date" +"%s")
# Check if the branch is stale
if [ "$epoch" -ge "$active_cutoff" ]; then
echo "$branch is active"
active_branches+=("$branch")
else
echo "$branch is stale"
fi
done
active_branches_json=$(printf '%s\n' "${active_branches[@]}" | jq -R . | jq -s .)
echo "$active_branches_json" > active_branches.json
echo "active_branches=$(echo $active_branches_json | jq -c)"
- name: Upload both JSON files as a single artifact
uses: actions/upload-artifact@v4
with:
name: stale-branches-${{ github.repository }}
path: |
*branches.json
- name: Set GitHub Run ID as a Repository Variable
run: |
gh variable set ACTIVE_BRANCH_RUN --body "${{ github.run_id }}" --repo ${{ github.repository }}
- name: Confirm the Run ID was set
run: |
gh variable list --repo ${{ github.repository }}