Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Github Actions: using workflow_run based on new tags

I have two workflows: CI (for continuous integration) and CD (for continuous delivery). Both are working fine individually. My goal is to run the CD workflow only when:

  • A new tag like v1.1.1 is created on the master branch
  • The CI workflow is finished

To achieve my goal I'm using the workflow_run event. These are the snippets of my workflows files:

ci.yml:

name: CI

on:
  push:
    tags: v[1-9]+.[0-9]+.[0-9]+
    
  pull_request:
    branches: [develop, hotfix*]

cd.yml

name: CD

on:
  workflow_run:
    workflows: [CI] 
    branches: [master]
    types:     
      - completed

The current behavior is: when a tag is created in the master branch only the CI workflow run. I've tried putting tags: v[1-9]+.[0-9]+.[0-9]+ in the workflow_run but the behavior is the same.

My question is: how can I achieve my goal? Is it possible?

like image 619
Bruno Peres Avatar asked Mar 19 '26 16:03

Bruno Peres


1 Answers

According to docs you can only use branches option and not tags for workflow_run so I'm afraid that's why your current setting doesn't work.

You have some alternatives though:

  1. You can turn your CD workflow into action and run it as part of your CI with condition: .github/actiond/cd/action.yml:
name: CD
description: Run CD
runs:
  using: composite
  steps:
    - run: echo "Success!"
      shell: bash

CI:

name: CI

on:
  push:
    tags: v[1-9]+.[0-9]+.[0-9]+
  pull_request:
    branches: [develop, hotfix*]

jobs:
  sucess:
    name: Log success
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v2
      - run: echo "Success!"
      - name: Run CD
        if: github.event_name == 'push' && contains(github.event.ref, '/tags/')
        uses: ./.github/actions/cd
  1. Have it as a separate job that is dependant on CI job using needs option

Converting it to action makes for better encapsulation IMO although requires some work.

like image 191
Ondřej Tůma Avatar answered Mar 22 '26 09:03

Ondřej Tůma



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!