62

I have a repository in GitHub and I want to create an Action to build a Docker image and push it to the DockerHub. I know how to do it but if I create the action in a branch other than master, GitHub does not run it.

This is a known problem (Workflow files only picked up from master?).

Any ideas to fix it?

1

3 Answers 3

34

According to the official GitHub Actions documentation (About workflow events):

The following steps occur to trigger a workflow run:

  1. An event occurs on your repository, and the resulting event webhook has an associated commit SHA and Git ref.

  2. The .github/workflows directory in your repository is searched for workflow files at the associated commit SHA or Git ref. The workflow files must be present in that commit SHA or Git ref to be considered.

    For example, if the event occurred on a particular repository branch, then the workflow files must be present in the repository on that branch.

  3. The workflow files for that commit SHA and Git ref are inspected, and a new workflow run is triggered for any workflows that have on: values that match the triggering event.

    The workflow runs on your repository's code at the same commit SHA and Git ref that triggered the event. When a workflow runs, GitHub sets the GITHUB_SHA (commit SHA) and GITHUB_REF (Git ref) environment variables in the runner environment. For more information, see "Using environment variables."

Because of this, in order to test the workflows we need to perform a git action (ie. do push) in the created branch.

1
  • 12
    "if the event occurred on a particular repository branch, then the workflow files must be present in the repository on that branch." What I was looking for, thanks
    – lenz
    Jun 7, 2021 at 8:50
4

What has worked for me (through trial and error)

  1. Create an empty YAML file in the .github/workflows folder
  2. Create a PR to move that file to your branch
  3. In your branch, you can now do the necessary edits to get your GH Action up & running. NOTE: next to updating your YAML, you also need to make a change that actually triggers the workflow (I am using the below trigger, note the absence of the '.github' path trigger).
on:
  push:
    paths:
      - 'path/to/your/code/**'
2
  • 1. create an empty YAML file in the .github/workflows folder . This will not work for some one not having rights to directly check into default branch(when default branch is protected , which mostly is the case).
    – sjethvani
    Jan 12, 2022 at 8:07
  • 8
    "Create a PR to move that file to your branch", this is hard to understand. You can't move files between branches (with or without PRs).
    – MEMark
    Feb 22, 2022 at 8:41
3
on:
  push:
    branches:
      - "YOUR-TEST-BRANCH"
  pull_request:
    branches:
      - "main" 
    paths:
      - ".github/workflows/test.yaml"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.