workflow_run is never trigged by pull_request - github

I have two very simple GitHub workflows for testing proposes. deploy-test-first which runs always on pull_request and deploy-test-second which should run on completion of deploy-test-first
The deploy-test-second however never runs. What Am I missing? How do I make it work?
name: deploy-test-first
on:
push:
branches: [main]
pull_request:
branches: [main]
jobs:
deploy-test-first:
runs-on: ubuntu-20.04
steps:
- name: deploy-test-first
run: echo 'deploy-test-first'
name: deploy-test-second
on:
workflow_run:
workflows: [deploy-test-first]
types: [completed]
branches: [develop]
jobs:
test-deploy-second:
if: ${{ github.event.workflow_run.conclusion == 'success' }}
runs-on: ubuntu-20.04
steps:
- name: test-deploy-second
run: echo 'deploy-test-second'
Both workflows are suppose to run on non-default branch develop in PR.

Related

How to trigger a github workflow job only for specific branch using patterns

I would like to trigger a github action wf for a specific branch.
My branch has the name
refs/heads/release/rc22-15.0.0
I would like to trigger the wf or a specific step for all releases refs/heads/release/**
The execution of the wf triggers both steps.
name: wf_logic_test_releasebranch
on:
workflow_dispatch:
push:
branches:
- 'release/**'
- 'feature/**'
- 'main'
jobs:
test_condition:
name: job_test_condition
runs-on: [atc-ubuntu-20.04]
environment: sandbox
steps:
- name: Branch name
if: github.ref == 'refs/heads/release/rc22-15.0.0'
run: echo running on branch ${GITHUB_REF##*/}
- run: echo ${{github.ref}}
By changing the code to:
name: wf_logic_test_releasebranch
on:
workflow_dispatch:
push:
branches:
- 'release/**'
- 'feature/**'
- 'main'
jobs:
test_condition:
name: job_test_condition
runs-on: [atc-ubuntu-20.04]
environment: sandbox
steps:
- name: Branch name
if: github.ref == 'refs/heads/release/**'
run: echo running on branch ${GITHUB_REF##*/}
- run: echo ${{github.ref}}
The execution of the above wf triggers only the second step.
This is what worked for me:
name: wf_logic_test_releasebranch
on:
workflow_dispatch:
push:
branches:
- 'release/**'
- 'feature/**'
- 'main'
jobs:
test_condition:
name: job_test_condition
runs-on: [atc-ubuntu-20.04]
environment: sandbox
steps:
- name: Always call branch name
run: echo running on branch ${GITHUB_REF##*/}
#WF executes step only if branch name is main
- name: Determine IP of runner only for release branch
if:
contains(github.ref, 'release')
run: curl https://api.ipify.org
- name: Show branch name for main and feature branch only
if:
contains(github.ref, 'main') || contains(github.ref, 'feature')
run: echo running on branch ${GITHUB_REF##*/}
job_only_for_main:
name: job_test_only_for_main
runs-on: [atc-ubuntu-20.04]
environment: sandbox
if:
contains(github.ref, 'main')
steps:
- name: Always call branch name
run: echo running on branch ${GITHUB_REF##*/}

How to run a github action step if it has a label

How would I check for a label added on a pull request when I merge to main branch from a pull request.
I tried using the below but didn't work:
name: Publish
on:
push:
branches:
- main
jobs:
Publish:
runs-on: ubuntu-latest
steps:
if: ${{github.event.label.name == 'release'}}
.......
How about like this?
name: Create Release
on:
pull_request:
branches:
- main
types:
- closed
jobs:
build:
runs-on: ubuntu-latest
timeout-minutes: 5
if: github.event.pull_request.merged == true && contains(github.event.pull_request.labels.*.name, 'release') # detect when pull request is merged since there's no merged event.
steps:
- name: Create release
...

Dependencies Between Workflows on Github Actions not working

I have two workflows and i do from this manual - https://docs.github.com/en/github-ae#latest/actions/using-workflows/workflow-syntax-for-github-actions#onworkflow_runbranchesbranches-ignore but dependent workflow not starting..
name: Tests
on:
push:
branches:
- '**' # matches every branch
- '!main' # excludes master
jobs:
setup-tests-job:
runs-on: ubuntu-latest
and dependent workflow
name: Build
on:
workflow_run:
workflows:
- 'Tests'
branches:
- '**'
types:
- completed
jobs:
setup-tests-job:
runs-on: ubuntu-latest
Maybe i do smthg wrong?

How do i run GithHub actions .yaml files in certain order?

I have two .yaml files for my GitHub actions. I need the second file to be executed only after first. How can I achieve this if the jobs are both in other files?
You could use the workflow_run syntax for Github Actions workflows.
In the example below, a workflow with the following trigger will only run when the workflow named Workflow Tester is completed (you could also started them in sequence using the requested type).
on:
workflow_run:
workflows: ["Workflow Tester"]
types: [completed] #requested
Note that when using the trigger that way (with the completed type) you can also check the previous workflow, and perform different jobs depending on the workflow conclusion.
Example
jobs:
on-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
[...]
on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
[...]
I've tested this syntax in this workflow if you want to have a look and check the workflow runs in the repo Actions tab.
There is a feature called Reusing Workflows which can be used.
Example:
workflow1.yaml
name: Job1
on:
workflow_call:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Run a one-line script
run: echo Job1 Executed!
workflow2.yaml
name: Job2
on:
workflow_call:
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Run a one-line script
run: echo Job2 Executed!
demo1.yaml(Calling Workflow)
name: Demo1
on:
push:
branches: [ "main" ]
workflow_dispatch:
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
call-workflow1:
uses: ./.github/workflows/workflow1.yaml
call-workflow2:
if: ${{ always() }} #This will make your workflow2 executed even if workflow1 fails, remove this, if you want to run this only on success of workflow1
needs: call-workflow1
uses: ./.github/workflows/workflow2.yaml
Sample
Reference -
Job Link
Repo

Github Action triggered by success of a different action

I am trying to trigger a Github action to run after a successful run of a different action.
the 2 workflows are:
Unit Test Action (Which runs first, and should trigger the Follow on Test action below
name: unit-tests
on:
push:
branches:
- '**'
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout#v2
- name: Setup .NET Core
uses: actions/setup-dotnet#v1
with:
dotnet-version: "3.1.x"
- name: Test
run: dotnet test src/XXXXXXXXXX
Follow on Test Action (This is just a test action)
name: Test action triggered by previous action success
on:
workflow_run:
workflows:
- unit-tests
types:
- completed
jobs:
test-job:
name: Test Step
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v2
with:
ref: ${{ github.event.workflow_run.head_branch }}
- run: echo "The follow on works!!"
The issue is that when this is triggered on a feature branch and not the default branch (as it should be because I want the actions to run all all branches) it doest work?
Any ideas?
As discussed in the comments:
First: It is necessary to have both workflows on the branch and to first merge the branch into your default branch, then onwards it will work.
Second: It is possible to use if: ${{ github.event.workflow_run.conclusion == 'success' }} to only run the jobs if the previous workflow was successful.
Example:
on:
workflow_run:
workflows: ["Other Workflow Name"]
types: [completed] #requested
jobs:
on-success:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'success' }}
steps:
- run: echo "First workflow was a success"
on-failure:
runs-on: ubuntu-latest
if: ${{ github.event.workflow_run.conclusion == 'failure' }}
steps:
- run: echo "First workflow was a failure"