ROHIT BANSAL
ROHIT BANSAL

Reputation: 327

How to trigger a workflow from another workflow using GitHub Actions?

I want to read version from a file and create tag as v11.0.5.1.aws using the workflow . Then I want to use that tag in the docker image. For that, I have created a branch as devops.

First created a VERSION file as

1.1.3 20 Apr, 2022

Created a workflow as release-version.yml

name: Release Version
on:
  push:
    branches:
      - devops
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Bump version and push tag
    uses: melheffe/version_release_composer@master
    env:
      PREPEND: 'v'
      APPEND: '.aws' # must include '.' or it will append without separation
      DRAFT: 'false'
      PRERELEASE: 'true'
      TOKEN: ${{ secrets.AUTH_TOKEN }}
      TRIGGER: ${{ github.event.pull_request.base.ref }} # can use the triggering branch or define a     fixed one like this: 'master'
      REPO_OWNER: rohit
      VERSION_FILE_NAME: 'VERSION'

Then created another workflow as ci.yml which will get tag from release-version workflow

name: CI

# Only trigger, when the build workflow succeeded
on:
  workflow_run:
    workflows: ["Release Version"]
    types:
      - completed

jobs:
  # This workflow contains a single job called "build"
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2

  DeployDev:
    # Steps represent a sequence of tasks that will be executed as part of the job
    name: Deploy to Dev
    needs: [Build]
    runs-on: ubuntu-latest
    environment:
      name: Dev

    steps:
      - uses: actions/checkout@v2
        with:
          token: ${{ secrets.AUTH_TOKEN }}

      - name: Build, tag, and push image to Amazon ECR
        id: build-image
        #env:
        #  IMAGE_TAG: ${{ github.sha }}
        run: |
          # Build a docker container and push it to ECR so that it can
          # be deployed to ECS.
          echo "$GITHUB_REF_NAME"
          docker build -t ${{secrets.ECR_REPO_URI}}/${{secrets.REPO_NAME}}:$GITHUB_REF_NAME .
          docker push ${{secrets.ECR_REPO_URI}}/${{secrets.REPO_NAME}}:$GITHUB_REF_NAME
          

I'm able to trigger release version workflow after making changes on devops branch but ci workflow is not getting triggered after triggering the release-version. Any suggestion will be helpful for me.

Upvotes: 19

Views: 46757

Answers (2)

bolinfest
bolinfest

Reputation: 3768

There is an important Note in GitHub's documentation on workflow_run:

You can't use workflow_run to chain together more than three levels of workflows. For example, if you attempt to trigger five workflows (named B to F) to run sequentially after an initial workflow A has run (that is: ABCDEF), workflows E and F will not be run.

Perhaps you have a chain with more than three levels of workflows?

Upvotes: 3

GuiFalourd
GuiFalourd

Reputation: 23290

If the workflow_run trigger isn't working as expected, there are two other ways to achieve what you want (triggering a workflow from another workflow, sending an input parameter from the first one to use in the second one).

The documentation is very good about how to use them, but I'll add here some references that can help as well:

As you can see, you can trigger those events using directly the Github API in a step (with a CURL request) or using some actions from the Github Marketplace that perform the same operation.

The answer below also explains the difference between both events (as they are similar, and CURL payload differences may be confusing)

I'll also add here an example that can be useful to understand how to use the repository_dispatch event to receive a callback from the other workflow to the first one:

Note that you will also need to use a PAT to trigger a workflow using a dispatch event.

Upvotes: 8

Related Questions