Git Product home page Git Product logo

keepalive-workflow's Introduction

Keepalive Workflow npm version

GitHub action to prevent GitHub from suspending your cronjob based triggers due to repository inactivity

Why

GitHub will suspend the scheduled trigger for GitHub action workflows if there is no commit in the repository for the past 60 days. The cron based triggers won't run unless a new commit is made. It shows the message "This scheduled workflow is disabled because there hasn't been activity in this repository for at least 60 days" under the cronjob triggered action.

preview

What

This workflow will automatically use the GitHub API (or create a dummy commit) in your repo if the last commit in your repo is 50 days (default) ago. This will keep the cronjob trigger active so that it will run indefinitely without getting suspended by GitHub for inactivity.

How to use

There are three ways you can consume this library in your GitHub actions

GitHub API Keepalive Workflow - Default (For GitHub Actions users)

You can just include the library as a step after one of your favorite GitHub actions. Your workflow file should have the checkout action defined in one of your steps since this library needs git CLI to work.

name: Github Action with a cronjob trigger
on:
  schedule:
    - cron: "0 0 * * *"
permissions:
  actions: write
jobs:
  cronjob-based-github-action:
    name: Cronjob based github action
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      # - step 2
      # - step n, use it as the last step
      - uses: gautamkrishnar/keepalive-workflow@v2 # using the workflow with default settings

Moving the keepalive workflow into its own distinct job is strongly recommended for better security. For example:

name: Github Action with a cronjob trigger
on:
  schedule:
    - cron: "0 0 * * *"
jobs:
  main-job:
    name: Main Job
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      # - step1
      # - step 2
      # - Step N
  keepalive-job:
    name: Keepalive Workflow
    runs-on: ubuntu-latest
    permissions:
      actions: write
    steps:
      - uses: actions/checkout@v4
      - uses: gautamkrishnar/keepalive-workflow@v2
Advanced use cases
Keeping another workflow file active using keepalive workflow

Lets assume that you have some build workflows:

  • .github/workflows/build1.yml
name: Build 20

on:
  schedule:
    - cron: "0 0 * * *"

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: "20"
          cache: "yarn"
      - run: yarn install --frozen-lockfile
      - run: yarn build
  • .github/workflows/build2.yml
name: Build 19

on:
  schedule:
    - cron: "0 0 * * *"

jobs:
  publish-npm:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3
      - uses: actions/setup-node@v3
        with:
          node-version: "19"
          cache: "yarn"
      - run: yarn install --frozen-lockfile
      - run: yarn build

You can keep both of these workflows active using the following keepalive workflow code:

name: Keepalive Workflow
on:
  schedule:
    - cron: "0 0 * * *"
permissions:
  actions: write
jobs:
  cronjob-based-github-action:
    name: Keepalive Workflow
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: gautamkrishnar/keepalive-workflow@v2
        with:
          workflow_files: "build1.yml, build2.yml"
          time_elapsed: "0"

Dummy Commit Keepalive Workflow (For GitHub Actions users)

To use the workflow in auto commit mode you can use the following code, Please note that this will create empty commits in your repository every 50 days to keep it active. Use the default API based option instead for a clean Git commit history.

name: Github Action with a cronjob trigger
on:
  schedule:
    - cron: "0 0 * * *"
permissions:
  contents: write
jobs:
  cronjob-based-github-action:
    name: Cronjob based github action
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      # - step1
      # - step 2
      # - step n, use it as the last step
      - uses: gautamkrishnar/keepalive-workflow@v2
        with:
          use_api: false

Moving the keepalive workflow into its own distinct job is strongly recommended here as well, for better security:

name: Github Action with a cronjob trigger
on:
  schedule:
    - cron: "0 0 * * *"
jobs:
  main-job:
    name: Main Job
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      # - step1
      # - step 2
      # - Step N
  keepalive-job:
    name: Keepalive Workflow
    if: ${{ always() }}
    needs: main-job
    runs-on: ubuntu-latest
    permissions:
      contents: write
    steps:
      - uses: actions/checkout@v4
      - uses: gautamkrishnar/keepalive-workflow@v2
        with:
          use_api: false
Let's take an example of [Waka Readme](https://github.com/athul/waka-readme)
name: My awesome readme
on:
  workflow_dispatch:
  schedule:
    # Runs at 12 am UTC
    - cron: "0 0 * * *"

jobs:
  update-readme:
    name: Update this repo's README
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - uses: athul/waka-readme@master
        with:
          WAKATIME_API_KEY: ${{ secrets.WAKATIME_API_KEY }}
      - uses: gautamkrishnar/keepalive-workflow@v2 # using the workflow

Using via NPM (For GitHub Actions developers)

For developers creating GitHub actions, you can consume the library in your javascript-based GitHub action by installing it from NPM. Make sure that your GitHub action uses checkout action since this library needs it as a dependency. You can also ask your users to include it as an additional step as mentioned in the first part.

Install the package

Install via NPM:

npm i keepalive-workflow

Install via Yarn:

yarn add keepalive-workflow

Use it in your own GitHub action source code

const core = require('@actions/core');
const { KeepAliveWorkflow, APIKeepAliveWorkflow } = require('keepalive-workflow');

// Using the lib in Dummy commits mode
KeepAliveWorkflow(githubToken, committerUsername, committerEmail, commitMessage, timeElapsed)
  .then((message) => {
    core.info(message);
    process.exit(0);
  })
  .catch((error) => {
    core.error(error);
    process.exit(1);
  });

// Using the lib in GitHub API mode
APIKeepAliveWorkflow(githubToken, {
  timeElapsed
}).then((message) => {
    core.info(message);
    process.exit(0);
  })
  .catch((error) => {
    core.error(error);
    process.exit(1);
  });

Options

For GitHub Action

If you use the workflow as mentioned via GitHub actions following are the options available to you to customize its behavior.

Option Default Value Description Required
gh_token your default GitHub token with repo scope GitHub access token with Repo scope No
commit_message Automated commit by Keepalive Workflow to keep the repository active Commit message used while committing to the repo No
committer_username gkr-bot Username used while committing to the repo No
committer_email [email protected] Email id used while committing to the repo No
time_elapsed 50 Time elapsed from the previous commit to trigger a new automated commit (in days) No
auto_push true Defines if the workflow pushes the changes automatically No
auto_write_check false Specifies whether the workflow will verify the repository's write access privilege for the token before executing No
use_api true Instead of using dummy commits, workflow uses GitHub API to keep the repository active No
workflow_files "" Comma separated list of workflow files. You can use this to keepalive another workflow that's not a part of keepalive workflow's file. See example for more info. No

For Javascript Library

If you are using the JS Library version of the project, please consult the function's DocStrings in library.js to see the list of available parameters.

Migrating from v1 to v2

If you are an existing user which used this workflow's v1 version, you can easily migrate to v2 by simply updating the permissions key in your workflow:

Change:

permissions:
  contents: write

to

permissions:
  actions: write

And change the workflow's version from gautamkrishnar/keepalive-workflow@v1 or gautamkrishnar/keepalive-workflow@master to gautamkrishnar/keepalive-workflow@v2. This will automatically start using the workflow's API based method. No more dummy commits 🕺 .

Workflow Versions

  • v1 version of project used dummy commit by default to keep the repository active, It will no longer be maintained except for security patches and bug fixes. You can view the source coe of v1 version at the master branch of this repository. This branch is kept intact for people who are using gautamkrishnar/keepalive-workflow@master.
  • v2 version will be developed and maintained by keeping the version2 branch as the source. Going forward, This will be the main branch.

FAQs and Common issues

Contributors ✨

Thanks goes to these wonderful people (emoji key):

Abit
Abit

💻
Guillaume NICOLAS
Guillaume NICOLAS

📖
Daniel Maticzka
Daniel Maticzka

💻
iTrooz
iTrooz

💻
Louis-Guillaume MORAND
Louis-Guillaume MORAND

💻
Tiger Kaovilai
Tiger Kaovilai

📖
Howard Wu
Howard Wu

💻
Tomáš Janoušek
Tomáš Janoušek

🤔
Shihyu
Shihyu

📖

This project follows the all-contributors specification. Contributions of any kind welcome!

License

This project uses GNU GENERAL PUBLIC LICENSE

Liked it?

Hope you liked this project, don't forget to give it a star ⭐.

keepalive-workflow's People

Contributors

abitmore avatar allcontributors[bot] avatar dependabot[bot] avatar dmaticzka avatar gautamkrishnar avatar gkr-bot avatar howard20181 avatar itrooz avatar kaovilai avatar lgmorand avatar nigui avatar rfay avatar shihyuho avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

keepalive-workflow's Issues

Documentation on the impact of a "Dummy commit"

Hi, this could be what a lot of people are looking for!

I think it would be really beneficial to add some documentation on the pros and cons of the dummy commit. There may not be any cons but always good to reassure your users. 😊

My workflow has been disabled even if I use this action

Hi,

Am I doing something wrong or is this a bug ?

Every week, I run the v2 version of this action using a simple "Keep Alive" action: https://github.com/julienloizelet/ddev-tools/actions/workflows/keepalive.yml

I use it so that another "test" action is not disabled:

https://github.com/julienloizelet/ddev-tools/actions/workflows/tests.yml

But, it has been disabled today:


Screenshot 2024-05-14 at 16 36 32

I don't see any error in the Keep Alive workflow:


Screenshot 2024-05-14 at 16 39 28

But despite this, the "test" action has been deactivated.

Do you have an idea of what could be the reason of this behavior ?

Thanks !

Unexpected input(s) 'use_api' during action run

I followed the documentation and here is the link to my YAML file. For some reason, the step is skipped and it seems like it doesn't do anything. Assistance would be greatly appreciated. I would prefer not to create any new commits.

image

My PAT has the following access:
Github Action - Keep Alive — admin:repo_hook, repo, workflow

Any assistance / directions would be greatly appreciated. Thanks in advance!

Add keepalive using GitHub actions API

Now GitHub supports re-enabling the workflows using GitHub API.

gh api -X PUT repos/{ repo }/actions/workflows/{ actionFile }/enable

ToDo

  • Provide API for action developers to use this approach
  • Add this as an option to the keepalive workflow

ref: #1

Please update Release 2.0 to say explicitly what needs to change

in https://github.com/gautamkrishnar/keepalive-workflow/releases/tag/2.0.0 you say

People who are using v1 and master tags of this workflow will continue using the dummy commit method since the newly added API-based method requires explicit permission for the API Keys for it to function.

but you don't mention what permission has to be changed.

It looks to me like you're saying that

permissions:
  contents: write

must be changed to:

permissions:
  actions: write

Could you please just show the simple change of an action in the release notes (and probably in the README?)

Thanks for all the work on this!

"unable to access" issue in multiple projects.

Thank you for the creating this action.
I have several open-source projects that use this action to test against an upstream stable/head builds.

About a week ago, I started recieving email notifications of tests failing:

{
    "command": "git push origin HEAD",
    "exitCode": 128,
    "outputData": "",
    "errorData": "remote: Permission to tyler[3](https://github.com/tyler36/ddev-tinker/actions/runs/5162752635/jobs/9315661265#step:12:3)6/ddev-tinker.git denied to github-actions[bot].\nfatal: unable to access 'https://github.com/tyler36/ddev-tinker.git/': The requested URL returned error: [4](https://github.com/tyler36/ddev-tinker/actions/runs/5162752635/jobs/9315661265#step:12:4)03\n"
}
with:
    gh_token: ***
    commit_message: Automated commit by Keepalive Workflow to keep the repository active
    committer_username: gkr-bot
    committer_email: [email protected]
    time_elapsed: 50
    auto_push: true
  env:
    NIGHTLY_DDEV_PR_URL: https://nightly.link/ddev/ddev/actions/runs/1720215802/ddev-linux-amd64.zip
    DDEV_GITHUB_TOKEN: ***
    HOMEBREW_NO_INSTALL_FROM_API: 1

Link to workflow.yml

denied by branch protections

Wondering what is the best way to work around branch protections?

Error: {"command":"git push origin HEAD","exitCode":1,"outputData":"","errorData":"remote: error: GH006: Protected branch update failed for refs/heads/dev.        \nremote: error: At least 1 approving review is required by reviewers with write access. 2 of 2 required status checks are expected.

Workflow being disabled even though v2 is in use

This is apparently different from @julienloizelet 's

I just got the warning from GitHub that my workflows on https://github.com/rfay/ddev-drushonhost are about to be disabled.

As you can see there the scheduled tests have been running; there's only one workflow, and it's using v2 of this action.

This is using https://github.com/ddev/github-action-add-on-test v2, which uses gautamkrishnar/keepalive-workflow v2

I'd love to know what you think is going on.

I do have other projects with identical single workflow (like https://github.com/ddev/signing_tools/ and https://github.com/ddev/ddev-redis-commander ) that are now more than 2 months out and not disabled yet.

Use branch "keep-alive"

The action should not use the current main branch, but some other branch.

I know that with additional steps, this should be possible. Maybe, this should be documented in the README.md. Maybe, it is as simple as

- run: git checkout -b keep-alive

[Question] Write Permission check

Hi,

First, thanks for this very useful action.

Then, a few remarks:

since the release 1.2.0 (including this commit ), almost all my Keep Alive workflows no longer work because I protect the main branch on almost all my projects.

I guess I can solve it by setting auto_write_check to false (did no test yet, am I right ?).

Setting the default value of auto_write_check to true is a kind of breaking change for me, and I would have prefer a @v2 maybe (or still a @v1 if the default value is false).

Finally, my question is:

Is it normal that, even if we set explicitly the write permission in the workflow, the Keep alive step will throw an error ?

For example,

I'm using this generic action :

name: Keep Alive
on:

 schedule:
    - cron: '0 0 * * *'

permissions:
  contents: write

jobs:
  keep-alive:

    name: Keep Alive
    runs-on: ubuntu-latest

    steps:

      - name: Clone project files
        uses: actions/checkout@v3

      # keepalive-workflow adds a dummy commit if there's no other action here, keeps
      # GitHub from turning off tests after 60 days
      - uses: gautamkrishnar/keepalive-workflow@v1
        with:
          commit_message: "chore(*): Automated commit to keep the repository active"
          time_elapsed: 55

And it fails (because I protected the main branch) even if there is the permissions:contents: write directive.

Thanks

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.