0

I am trying to trigger different reusable workflows according to target branch name of a created tag. For example if I create a tag from main branch, I want to trigger run-on-prod-tags.yml workflow and if the branch name starts with feature, I want to trigger run-on-stage-tags.yml. Should I make something like that in order to do that?

name: Tag Workflow

on:
    push:
        tags:
            - '*'

jobs:
    check-tag:
        runs-on: docker-runner
        steps:
            -   name: checkout Source Code
                uses: actions/checkout@v3
                with:
                    # Disabling shallow clone is recommended for improving relevancy of reporting
                    fetch-depth: 0

            -   name: Get Branch
                run: |
                    echo ${{ github.ref }}
                    raw=$(git branch -r --contains ${{ github.ref }})
                    branch=${raw##*/}
                    echo "BRANCH=$branch" >> $GITHUB_ENV
            - run: echo ${{env.BRANCH}}
    build-and-deploy:
        needs: [check-tag]
        uses: ./.github/workflows/run-on-${{env.BRANCH}}-tags.yml

If I use something like that, when we change file name of reusable workflows', this code will fail. I should use something more robust. Do you have any idea to do that?

Thanks

P.S. Output Of The Script

enter image description here

Berkin
  • 1,565
  • 5
  • 22
  • 48
  • The above workflow is [invalid](https://rhysd.github.io/actionlint/#eJxlUE1rAyEQvfsr5rCQtqDehT00gbanHEqhl15cmazbJBrU7VK2/vf6kbAsGRB05r3nm2fkGQXsHMqA8CF7+LTueDjZiRBrBIFUl9HressVZO+XVy4Km6cNId+2uw6URnWkCbjgaDqmfPWKAbZOGqVXIm40Av5WrdKWU9s89EOArnCAOqBUWRPkYDw08wxpqMeOOTxAjI93CpXXNnOS4tYN/WC+eLyDodIWhPAYKJqf4rXdvj/vd29CNN3aL735LaTsIVFYRScPpABzhsv+dfeXnOutNXr0AhhndQE+XYP3PGlTa2gSXnRjzIF69ns+/QMnu3UU). You have different reusable workflows for each branch? Have you tested above strategy? Also, what would this (`git branch -r --contains ${{ github.ref }}`) return? – Azeem Mar 10 '23 at 09:55
  • The code above is kinda psuedo. check-tag job is focused on finding the branch name of created the tag. – Berkin Mar 10 '23 at 10:06
  • Regarding "**finding the branch name of created the tag**", see https://stackoverflow.com/a/14613605/7670262. Also. in your case, `github.ref` would contain the tag, not branch. And, [`--contains` requires a commit](https://git-scm.com/docs/git-branch#Documentation/git-branch.txt---containsltcommitgt). Did you mean to use [`github.sha`](https://docs.github.com/en/actions/learn-github-actions/contexts#github-context) instead? – Azeem Mar 10 '23 at 10:13
  • I updated my code. I am going to share the output of that script. But I want to emphasize the problem of dynamically calling workflow. – Berkin Mar 10 '23 at 10:40
  • Apart from that, what kind of issues are you facing with that dynamic configuration? – Azeem Mar 10 '23 at 10:53
  • 1
    github_ref = refs/tags/17 and github_ref_name = 17. But my goal is to find branch name from tag. I can't find branch name from 17 (ref_name). I used tihs as reference. https://stackoverflow.com/questions/63745613/how-to-get-a-branch-name-on-github-action-when-push-on-a-tag – Berkin Mar 10 '23 at 10:57
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/252442/discussion-between-berkin-and-azeem). – Berkin Mar 10 '23 at 10:58

1 Answers1

0

As those are two separate jobs, env won't work like that. And, if you resort to set the output parameter, it still won't work because according to context availability, uses is not listed i.e. no context is available under uses.

So, as of now, you may use if and its negation as you're currently doing to switch between workflows with hardcoded full paths of the reusable workflows.

Alternatively, you may write a wrapper reusable workflow with inputs to call the respective workflow internally to hide the details in the main workflow.

Azeem
  • 11,148
  • 4
  • 27
  • 40