Action trigger another Action

May 13, 2023

Use your action to trigger another

Context

You want to enable automatic tag version bump when desired, and then automatically build the versioned container image and push it to the registry.

This happen for this website repository web, hosted on Github, which periodically receives library update pull requests (PRs) from dependabot. Sometimes, at night, I check for these notifications on my phone using Github App and merged them. So, if I want to deploy the changes, I need following actions to be automatically triggered: [bump version] -> [build and push].

Limitation

Sure, there is a Github Action available for bumping the tag version.

    - name: Bump version and push tag
      id: bump_version
      uses: anothrNick/[email protected]
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        WITH_V: true

But there is a problem. By design, from a security perspective, pushing tags does not trigger a new workflow run. Alternatively you can use a Personal Access Token (PAT) instead of of GITHUB_TOKEN according to this discussion.

Solution

But there is another way. You can create createWorkflowDispatch with inputs parameter. For example using actions/github-script

    - name: Trigger build
      uses: actions/github-script@v6
      with:
         script: |
            github.rest.actions.createWorkflowDispatch({
              owner: context.repo.owner,
              repo: context.repo.repo,
              workflow_id: 'docker-image.yml',
              ref: 'main',
              inputs: {
                current_version: '${{ steps.bump_version.outputs.new_tag }}'
              },
            })

And then on the receiving workflow. add parameter to accept the inputs:

on:
  push:
    tags:
      - '*'
  workflow_dispatch:
    inputs:
      current_version:
        description: set version on build artifact
        required: false
        default: ''

The result

Return to blog

footer