#!/usr/bin/env bash #This will keep the GitHub Action active by making periodic empty commits. #export the variables to set values other than defaults before calling the script. # $ curl -fsSL action-keepalive.bos.li | bash : <<'EXAMPLE' This script keeps a GitHub Action workflow active by making periodic empty commits (default) or using the GitHub API (when USE_API is true). To enable keep-alive, add a step to your workflow file as shown in the examples below. A checkout step is required before invoking this script. This is needed to check the last repo activity or make a commit. Use 'env' in the keep-alive step to configure how this script should function. GitHub disables repo Actions after 60 days of inactivity. You can set the DAYS_ELAPSED parameter to a value < 60 days to trigger API/commit after the last activity. Set this to 0 to trigger every time and you may also eliminate the checkout step requirement if USE_API is also true. The following options are available by setting 'env' variables. All are optional except GH_TOKEN, which must be set to ${{ github.token }} when USE_API is true. ------------------------------------------------------------------------------------------------------------- Option Default Value Description ------------------------------------------------------------------------------------------------------------- DAYS_ELAPSED 55 Number of days since last activity to trigger keep-alive via API or GitHub commit. Use "0" to run keep-alive every time and skip the requirement of checkout step if USE_API is also true. USE_API false Can be "true" or "false". If true, uses API mode for keep-alive; otherwise, makes empty commits to keep the repo active. Options available if USE_API is false, i.e., commit mode for keep-alive: COMMIT_USERNAME SBKeepAliveBot Username used for empty commits COMMIT_EMAIL SB@keepaliven Email used for empty commits COMMIT_MESSAGE Keeping alive Message used for empty commits Options available if USE_API is true, i.e., API mode for keep-alive: GH_TOKEN (required to set) Must always be set to ${{ github.token }} WORKFLOWS If not set, re-enables the current workflow file via API. You can also provide a space-delimited list of workflow filenames, e.g., "WFfile1.yaml WFfile2.yml WFfile3.yml" ------------------------------------------------------------------------------------------------------------- Action permissions required for the workflow run: If USE_API is false or unset, i.e., commit mode: contents: write If USE_API is true: actions: write ############################################################################################################# # Sample workflow file: .github/workflows/example-workflow.yaml name: Keep Alive example on: schedule: - cron: "0 */12 * * *" workflow_dispatch: permissions: contents: write # For non-API, i.e commit mode actions: write # For API use mode (when USE_API is true) jobs: check-expiry: runs-on: ubuntu-latest steps: - uses: actions/checkout@v4 - name: Keep alive by empty commits env: DAYS_ELAPSED: 55 # Def. 50 if omitted run: curl -fsSL action-keepalive.bos.li | bash #OR - name: Keep alive by commits with customization env: DAYS_ELAPSED: 55 # Def. 50 if omitted COMMIT_USERNAME: SBKeepAliveBot # Def. 'SBKeepAliveBot' if omitted COMMIT_EMAIL: 'SB@keepalive' # Def. 'SB@keepalive' if omitted COMMIT_MESSAGE: 'Keeping alive' # Def. 'Keeping alive' if omitted run: curl -fsSL action-keepalive.bos.li | bash #OR - name: Keep alive by Re-enabling workflow using API env: DAYS_ELAPSED: 55 # Def. 50 if omitted USE_API: true # Def. is false if omitted, i.e, commits will be used GH_TOKEN: ${{ github.token }} # Required if USE_API is true run: curl -fsSL action-keepalive.bos.li | bash #OR - name: Keep alive by Re-enabling specified workflow(s) using API env: DAYS_ELAPSED: 55 # Def. 50 if omitted USE_API: true # Def. is false if omitted, i.e, commits will be used GH_TOKEN: ${{ github.token }} # Required if USE_API is true WORKFLOWS: WF1.yaml WF2.yml # By default it will just re-enable the running workflow run: curl -fsSL action-keepalive.bos.li | bash EXAMPLE : "${DAYS_ELAPSED:=50}" : "${USE_API:=false}" : "${COMMIT_USERNAME:=SBKeepAliveBot}" : "${COMMIT_EMAIL:=SB@keepalive}" : "${COMMIT_MESSAGE:=Keeping alive}" : "${WORKFLOWS:=}" set -o nounset set -o errexit set -o pipefail if [ "${TRACE-0}" -eq 1 ]; then set -o xtrace; fi if [[ "$DAYS_ELAPSED" == "0" ]]; then DAYS_AGO=1 echo "Not checking last commit, will run Keep alive" else # Get the last commit date for the current branch LAST_COMMIT_DATE=$(git log -1 --format="%ct") CURRENT_DATE=$(date "+%s") TIME_DIFFERENCE=$((CURRENT_DATE - LAST_COMMIT_DATE)) DAYS_AGO=$((TIME_DIFFERENCE / (60 * 60 * 24))) echo "Last commit is '$DAYS_AGO' days ago" fi if [ "$DAYS_AGO" -gt "$DAYS_ELAPSED" ]; then echo "$DAYS_AGO > $DAYS_ELAPSED -> Keep alive" if [ $USE_API = true ]; then #Required permission #permissions: # actions: write #Also set env for the step #env: # GH_TOKEN: ${{ github.token }} echo "Using API to keep alive" case "${GITHUB_WORKFLOW_REF:?}" in "${GITHUB_REPOSITORY:?}"/.github/workflows/*.y*ml@*) ;; *) false ;; esac if [ -z "$WORKFLOWS" ]; then WORKFLOWS="${GITHUB_WORKFLOW_REF%%@*}" WORKFLOWS="${WORKFLOWS#${GITHUB_REPOSITORY}/.github/workflows/}" fi for workflow in $WORKFLOWS; do gh api -X PUT "repos/${GITHUB_REPOSITORY}/actions/workflows/${workflow}/enable" done else #Required permission #permissions: # contents: write echo "Adding empty commit" git config user.name "$COMMIT_USERNAME" git config user.email "$COMMIT_EMAIL" git commit --allow-empty -m "$COMMIT_MESSAGE" git push fi echo "done" else echo "$DAYS_AGO <= $DAYS_ELAPSED -> nothing to do" fi