Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 591
ROHIT BANSAL Avatar asked Nov 18 '25 06:11

ROHIT BANSAL


1 Answers

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 workflow_dispatch event.
  • The repository_dispatch event.

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

  • Triggering Github Action using a POST request (Github REST API)
  • How to trigger a workflow_dispatch from Github API?
  • Triggering GitHub workflow using gh CLI

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)

  • Correct request with client-payload to run workflow_dispatch in github action

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:

  • workflow A
  • workflow B

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

like image 137
GuiFalourd Avatar answered Nov 20 '25 15:11

GuiFalourd



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!