10

Can I trigger a new workflow from another workflow?

I'm trying to run a workflow after the first workflow has pushed a new release and it seems to ignore it.

bArmageddon
  • 8,088
  • 6
  • 22
  • 40

4 Answers4

11

As described here, you can trigger another workflow using the workflow_run event.

For example we could think of two workflow definitions like this (the only prerequisite is, that both reside in the same repository - but I am sure, there's also an event for other repos as well):

release.yml

name: CI release

on: [push]

jobs:
  build:
    runs-on: ubuntu-latest

    steps:
    - uses: actions/checkout@v2

    - name: Release artifact
      run: ...

do-something-different.yml

name: Do anything after the release of the first workflow

on:
  workflow_run:
    workflows: ["CI release"]
    types:
      - completed

jobs:
  notify:
    runs-on: ubuntu-latest

    steps:
      - uses: actions/checkout@v2

      - name: Do something
        run: ...

A crucial point here is that the name: CI release definition of the first yaml file must exactly match the workflow_run: workflows: ["CI release"] definition in the second yaml file. Another point is that this approach needs to be done on the default branch (which is mostly main or master) as the docs state:

Note: This event will only trigger a workflow run if the workflow file is on the default branch.

Jonathan Hall
  • 75,165
  • 16
  • 143
  • 189
jonashackt
  • 12,022
  • 5
  • 67
  • 124
  • 7
    Sadly i think `workflow_run` is pretty much useless in most situations: 1. It runs on default branch which is problematic sometimes 2. When triggered from a pull_request workflow it looses the pull_request context and "just runs" and is unaware of the PR – Niton Nov 09 '21 at 19:29
8

Found the answer here:

An action in a workflow run can't trigger a new workflow run. For example, if an action pushes code using the repository's GITHUB_TOKEN, a new workflow will not run even when the repository contains a workflow configured to run when push events occur.

EDIT: The quote above might be confusing. When I add a Personal Access Token (PAT) to the checkout action with repo permissions granted (and not repository's GITHUB_TOKEN), the following commands DO trigger other workflows:

        - name: Checkout Repo
          uses: actions/checkout@v2
          with:
              token: ${{ secrets.PAT_TOKEN }}

(In my case, running semnatic-release after this checkout, which creates a new release with a new tag - did trigger another workflow that runs only if a tag was created)

bArmageddon
  • 8,088
  • 6
  • 22
  • 40
  • 2
    The `GITHUB_TOKEN` that the documentation mentions is the default token scoped to just that repository. What you are using is not *the* `GITHUB_TOKEN`, it's a Personal Access Token (PAT), which has much wider scope. It is intentional that PATs allow further workflows to trigger, while the `GITHUB_TOKEN` does not. – peterevans Feb 27 '20 at 08:50
  • Thanks @peterevans! yes I used the PAT. I'll fix to 'Personal Access Token (PAT)' – bArmageddon Feb 27 '20 at 09:37
  • 1
    I have that same scenario and use PAT tokens, but my tags workflow will not run. – secondman Jan 11 '22 at 13:50
  • @peterevans to your knowledge is to possible to reduce the PAT scope so that further workflows are not triggered from one workflow? – n1nsa1d00 Feb 22 '23 at 09:20
  • @n1nsa1d00 I'm not aware of that being possible. It's not configurable with a scope. If you don't want further workflows triggered then just use the default `GITHUB_TOKEN`. – peterevans Feb 24 '23 at 02:11
4

If you don't want to use a general Personal Access Token (which has access to all of your repos), you can generate a dedicated SSH keypair for this purpose and add it to the repository as a Deploy Key. This is done as follows:

  1. Generate an SSH keypair:

    ssh-keygen -N "" -f deploy_key -C "github-actions"
    
  2. Add the private key (generated file deploy_key) as an encryped secret, e.g. COMMIT_KEY to the GitHub project.

    GitHub secret

  3. Add the public key (generated file deploy_key.pub) as a deploy key with write access to the GitHub project. Tick the Allow write access checkbox.

    Deploy key

  4. When checking out the source code in your workflow, add the SSH key:

    - name: Checkout
      uses: actions/checkout@v3
      with:
        ssh-key: "${{secrets.COMMIT_KEY}}"
    

Subsequent push actions in the same workflow will then trigger any configured GitHub workflow as if they were pushed manually.

carlfriedrich
  • 2,919
  • 1
  • 15
  • 29
0

My Case:

Frontend Branch( 1st Workflow) Backend Branch (2nd Workflow)

I Wanted to trigger 2nd Workflow from 1st Workflow(here i was pushing some files to backend branch)

Intially used github token to achieve but found it will not work as it doesn't have priviledges.

I used PAT or Deploy token to acheive this

If using PAT token, then in WORKflow 1

 - uses: actions/checkout@v2
      with:
        persist-credentials: false

...... .....

..

- name: Push changes
      uses: ad-m/github-push-action@master
      with:
        github_token: ${{ secrets.GITHUB_TOKEN }}
        branch: ${{ github.ref }}

If using Deploy (first create SSh keys)

- uses: actions/checkout@v3
  with:
    ssh-key: ${{ secrets.SSH_PRIVATE_KEY }}
    persist-credentials: true

..... .. .....

- name: Push changes
  uses: ad-m/github-push-action@master
  with:
    ssh: true
    branch: ${{ github.ref }}

For More REF HERE

Ansh
  • 1
  • 2