Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I reuse a GitHub Actions workflow from the branch I'm currently working on?

I'd like to abstract some of my GitHub Actions with a reusable workflow.

In order to do this, I need to call my newly defined callable workflow in the format {owner}/{repo}/{path}/{filename}@{ref}

e.g. (from the docs)

jobs:
  call-workflow-1:
    uses: octo-org/this-repo/.github/workflows/workflow-1.yml@172239021f7ba04fe7327647b213799853a9eb89
  call-workflow-2:
    uses: octo-org/another-repo/.github/workflows/workflow-2.yml@v1

However, in practice, I'm working on my branch, some-branch-name, where I'm working on my workflow-1.yml file and I'd like to be able to run my actions as defined in my current branch, Given

`{ref} can be a SHA, a release tag, or a branch name.

It seems like I'd need to use

jobs:
  call-workflow-1:
    uses: octo-org/this-repo/.github/workflows/workflow-1.yml@some-branch-name

But, I'd like this to work for any branch, so

jobs:
  call-workflow-1:
    uses: octo-org/this-repo/.github/workflows/workflow-1.yml@${{ github.ref }}

But, it turns out this isn't possible as expressions can't be used in the uses attributes.

How can I achieve this?

like image 585
Joshua Avatar asked Oct 11 '21 21:10

Joshua


People also ask

How do I rerun a workflow in GitHub?

To re-run a failed workflow run, use the run rerun subcommand. Replace run-id with the ID of the failed run that you want to re-run. If you don't specify a run-id , GitHub CLI returns an interactive menu for you to choose a recent failed run.

How do I call one workflow from another workflow in GitHub Actions?

You call a reusable workflow by using the uses keyword. Unlike when you are using actions within a workflow, you call reusable workflows directly within a job, and not from within job steps.

Do GitHub action jobs run on the same machine?

And why you can't simply run it once for all jobs, because each job may run on a different machine. A job is a set of steps that execute on the same runner. By default, a workflow with multiple jobs will run those jobs in parallel. You can also configure a workflow to run jobs sequentially.

How to create a reusable workflow in GitHub?

You can only use reusable workflow if they are public, inside same repo or same organization To begin with, you can create a file inside .github/workflows/sample-test.yml or similar // your steps... Here you can create a workflow by using the key workflow_call and pass the variable using input and encrypted input as secrets

Is it possible to reuse code in GitHub actions?

While code reuse in Github Actions is (still) not available, you may want to consider this workaround to deploy to multiple targets from a single workflow file.

What happens when you reuse a workflow from another repository?

When you reuse a workflow, the entire called workflow is used, just as if it was part of the caller workflow. If you reuse a workflow from a different repository, any actions in the called workflow run as if they were part of the caller workflow.

Where can I get help with GitHub actions?

And if you have questions or comments about GitHub Actions overall (including template or anchor support), then the Product and Support teams are happy to help at the GitHub Community Forum. Sorry, something went wrong. Hmm. Why is this closed?


Video Answer


1 Answers

It's as you said: It can't be done at the moment as Github Actions doesn't support expressions with uses attributes.

There is no workaround (yet?) because the workflow interpreter (that also checks the workflow syntax when you push the workflow to the repository) can't get the value from the expression at that moment.

It could maybe work if the workflow was recognized by the interpreter, but it doesn't event appear on the Actions tab as it's considered invalid.

For the moment, you can only use tag, branch ref or commit hash after the @ symbol, the same way you use any action.

Note that someone else had the same issue here

like image 89
GuiFalourd Avatar answered Sep 19 '22 05:09

GuiFalourd