run keep-alive every month #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
| # Keep Scheduled Workflows Alive | ||
| # | ||
| # Purpose: | ||
| # GitHub automatically disables scheduled workflows after 60 days of repository inactivity. | ||
| # This workflow prevents that by creating minimal activity when needed. | ||
| # | ||
| # How it works: | ||
| # 1. Runs on the first day of every month | ||
| # 2. Checks if the keep-alive PR exists | ||
| # 3. If PR doesn't exist, creates it with a persistent branch | ||
| # 4. Reopens the PR (if closed) and immediately closes it again | ||
| # 5. This activity prevents workflow deactivation | ||
| # | ||
| # Manual trigger: | ||
| # This workflow can also be triggered manually via workflow_dispatch if needed. | ||
| 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: Ensure keep-alive branch and PR exist | ||
| env: | ||
| GH_TOKEN: ${{ github.token }} | ||
| shell: bash | ||
| run: | | ||
| # Use a fixed branch name for persistence | ||
| branch_name="keep-alive-workflow" | ||
| # Configure git | ||
| git config --local user.name "github-actions[bot]" | ||
| git config --local user.email "github-actions[bot]@users.noreply.github.com" | ||
| # Check if branch exists remotely | ||
| if git ls-remote --heads origin "$branch_name" | grep -q "$branch_name"; then | ||
| echo "Branch $branch_name already exists" | ||
| else | ||
| echo "Creating new branch $branch_name" | ||
| git checkout -b "$branch_name" | ||
| git commit --allow-empty -m "chore: keep scheduled workflows alive | ||
| This branch is used by the keep-alive workflow to maintain repository activity. | ||
| GitHub disables scheduled workflows after 60 days of repository inactivity." | ||
| git push --set-upstream origin "$branch_name" | ||
| fi | ||
| # Check if PR exists (open or closed) | ||
| 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 new PR for keep-alive workflow" | ||
| gh pr create \ | ||
| --head "$branch_name" \ | ||
| --base ${{ github.event.repository.default_branch }} \ | ||
| --title "chore: keep scheduled workflows alive" \ | ||
| --body "This automated PR maintains repository activity to prevent scheduled workflows from being disabled. | ||
| GitHub automatically disables scheduled workflows after 60 days of repository inactivity. This PR is reopened and closed periodically by the keep-alive workflow to ensure continuous operation. | ||
| **Note:** This PR is managed by automation and will be reopened/closed automatically. Do not delete this PR or its branch." | ||
| # Get the newly created PR number | ||
| 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 already exists" | ||
| fi | ||
| # Store PR number for next step | ||
| echo "pr_number=$pr_number" >> $GITHUB_OUTPUT | ||
| id: setup | ||
| - name: Reopen and close PR to maintain activity | ||
| 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 | ||
| # Check PR state | ||
| pr_state=$(gh pr view "$pr_number" --json state --jq '.state') | ||
| echo "PR #$pr_number is currently: $pr_state" | ||
| # Reopen if closed | ||
| if [ "$pr_state" = "CLOSED" ]; then | ||
| echo "Reopening PR #$pr_number" | ||
| gh pr reopen "$pr_number" | ||
| sleep 2 | ||
| fi | ||
| # Close the PR with a timestamp comment | ||
| echo "Closing PR #$pr_number" | ||
| gh pr close "$pr_number" --comment "Keep-alive activity: $(date -u '+%Y-%m-%d %H:%M:%S UTC')" | ||
| echo "Keep-alive activity completed successfully" | ||