-
Notifications
You must be signed in to change notification settings - Fork 2
Keep scheduled workflows alive #128
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
f5d404e
Initial plan
Copilot 1dc3437
Add keep-alive workflow to prevent scheduled workflow deactivation
Copilot 6a88054
Add error handling to keep-alive workflow
Copilot 58da803
Address code review feedback: improve regex, branch handling, and doc…
Copilot 5b30f0d
Add comprehensive documentation to keep-alive workflow
Copilot cfa441d
Simplify keep-alive workflow to reuse same PR every ~59 days
Copilot 769babd
run keep-alive every month
TimHess 37588ed
Merge branch 'main' into copilot/add-scheduled-repo-keep-alive-job
TimHess f031123
move commit message and pr body to env vars for easier formatting
TimHess ba20ecb
add step to check for activity in the last 30 days to reduce noise
TimHess 34c7b1f
Clarify why workflow runs every 30 days instead of 60
Copilot bb3a97c
Reduce verbosity, add draft PR flag, and document empty commits
Copilot 4dc69d2
further comment cleanup
TimHess 095a2d7
Update .github/workflows/keep-alive.yml
TimHess 94cb8e1
pr feedback & polish
TimHess File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,126 @@ | ||
| # Prevents scheduled workflows from being disabled by GitHub after 60 days of repository inactivity. | ||
| name: Keep Scheduled Workflows Alive | ||
|
|
||
| on: | ||
| schedule: | ||
| - cron: "0 0 1 * *" | ||
| workflow_dispatch: | ||
|
|
||
| permissions: | ||
| contents: write | ||
| pull-requests: write | ||
| issues: read | ||
|
|
||
| jobs: | ||
| keep-alive: | ||
| name: Keep repository GitHub Actions active | ||
| runs-on: ubuntu-latest | ||
| timeout-minutes: 5 | ||
|
|
||
| steps: | ||
| - name: Git checkout | ||
| uses: actions/checkout@v6 | ||
| with: | ||
| persist-credentials: false | ||
|
|
||
| - name: Check for recent activity | ||
| id: check_activity | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| shell: bash | ||
| run: | | ||
| default_branch="${{ github.event.repository.default_branch }}" | ||
| if [ -z "$default_branch" ]; then | ||
| default_branch="main" | ||
| fi | ||
|
|
||
| echo "Checking activity on branch: $default_branch" | ||
| last_commit_date=$(gh api "repos/${{ github.repository }}/commits/$default_branch" --jq '.commit.committer.date') | ||
| echo "Last commit date: $last_commit_date" | ||
|
|
||
| last_commit_seconds=$(date -d "$last_commit_date" +%s) | ||
| current_seconds=$(date +%s) | ||
| days_diff=$(( (current_seconds - last_commit_seconds) / 86400 )) | ||
| echo "Days since last activity: $days_diff" | ||
|
|
||
| if [ "$days_diff" -lt 30 ]; then | ||
| echo "Active (< 30 days). No keep-alive needed." | ||
| echo "run_keep_alive=false" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "Inactive ($days_diff days). Running keep-alive." | ||
| echo "run_keep_alive=true" >> $GITHUB_OUTPUT | ||
| fi | ||
|
|
||
| - name: Ensure keep-alive branch and PR exist | ||
| if: ${{ steps.check_activity.outputs.run_keep_alive == 'true' }} | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| COMMIT_MSG: | | ||
| chore: keep scheduled workflows alive | ||
| PR_BODY: | | ||
| Draft PR maintained by the keep-alive workflow. Do not merge or delete. | ||
| shell: bash | ||
| run: | | ||
| branch_name="keep-alive-workflow" | ||
| default_branch="${{ github.event.repository.default_branch }}" | ||
| if [ -z "$default_branch" ]; then | ||
| default_branch="main" | ||
| fi | ||
|
|
||
| git config --local user.name "github-actions[bot]" | ||
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | ||
|
|
||
| if git ls-remote --heads origin "$branch_name" | grep -q "$branch_name"; then | ||
| echo "Branch exists" | ||
| git fetch origin "$branch_name" | ||
| git checkout "$branch_name" | ||
| git pull origin "$branch_name" | ||
| else | ||
| echo "Creating branch" | ||
| git checkout -b "$branch_name" | ||
| fi | ||
|
|
||
| git commit --allow-empty -m "$COMMIT_MSG" | ||
| git push origin "$branch_name" | ||
|
|
||
| pr_number=$(gh pr list --head "$branch_name" --state all --json number --jq '.[0].number' 2>/dev/null || echo "") | ||
|
|
||
| if [ -z "$pr_number" ]; then | ||
| echo "Creating draft PR" | ||
| gh pr create --draft --head "$branch_name" --base "$default_branch" \ | ||
| --title "chore: keep scheduled workflows alive" --body "$PR_BODY" | ||
|
|
||
| pr_number=$(gh pr list --head "$branch_name" --state all --json number --jq '.[0].number') | ||
| echo "Created PR #$pr_number" | ||
| else | ||
| echo "PR #$pr_number exists" | ||
| fi | ||
|
|
||
| echo "pr_number=$pr_number" >> $GITHUB_OUTPUT | ||
| id: setup | ||
|
|
||
| - name: Reopen and close PR to maintain activity | ||
| if: ${{ steps.check_activity.outputs.run_keep_alive == 'true' }} | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| shell: bash | ||
| run: | | ||
| pr_number="${{ steps.setup.outputs.pr_number }}" | ||
|
|
||
| if [ -z "$pr_number" ]; then | ||
| echo "Error: PR number not found" | ||
| exit 1 | ||
| fi | ||
|
|
||
| pr_state=$(gh pr view "$pr_number" --json state --jq '.state') | ||
| echo "PR #$pr_number is $pr_state" | ||
|
|
||
| if [[ "$pr_state" == "CLOSED" ]]; then | ||
| echo "Reopening PR #$pr_number" | ||
| gh pr reopen "$pr_number" | ||
| fi | ||
|
|
||
| echo "Closing PR #$pr_number" | ||
| gh pr close "$pr_number" --comment "Keep-alive: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" | ||
|
|
||
| echo "Keep-alive completed" | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.