Search code examples
workflowgithub-actionsgithub-cli

Ensure that a workflow in Github Actions is only ever triggered once


I have workflow that is triggered when a specific file is changed. Is it possible to ensure that this workflow is only triggered the first time that file is changed?

The use case is:

  • a repository is created from a template repository
  • to initialize the README and other things in the repo, some variables can be set in a JSON config file
  • once that file is committed, the workflow runs, creates the README from a template etc.

I have tried to do let the workflow delete itself before it commits and pushes the changed files. That doesn't work: fatal: not in a git directory and fatal: unsafe repository ('/github/workspace' is owned by someone else).

What might work is adding a topic like initialized to the repo at the end of the workflow and check for the presence of this topic at the beginning of the workflow. However, this is feels like a hack in that I'm abusing topics for something they're probably not meant to do.

So my question remains: is there a good way to only run the workflow once?


Solution

  • With the help of the comment by frennky I managed to do solve this by using the GitHub CLI to disable the workflow.

    Here is the step at the end of the workflow that does this:

    - name: Disable this workflow
      shell: bash
      run: |
        gh workflow disable -R $GITHUB_REPOSITORY "${{ github.workflow }}"
      env:
        GITHUB_TOKEN: ${{secrets.GITHUB_TOKEN}}