Assuming I have two jobs and the second depends on the first one:
jobs:
job-1:
runs-on: ubuntu-latest
steps:
- uses: somerepo/someaction#v1
- uses: anotherrepo/anotheraction#v1
- bash: shell
run: |
do-a-lot-of-stuff
job-2:
runs-on: ununtu-latest
needs: job-1
if: failure()
steps:
- shel: bash
run: echo ${{ needs.job-1.fail.message }}
Is there a way to capture the failure message from job-1?
(from whichever step failed of course)
Related
I have a workflow with two jobs, tests and build. tests job I want to run when any branch gets pushed into but it should not get run when I do merge from any branch to another. Aim is to prevent long running tests job during merge because it already gets executed when we push code.
name: PR test
on:
push:
branches:
- '**'
tags-ignore:
- '**'
jobs:
tests:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Run tests
run: |
echo "tests run"
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Build
run: |
echo "Build"
When I add if condition under job tests:
github.ref != 'refs/heads/develop' && github.event.pull_request.merged == false
...it helps to run tests job when I push in any branch and prevents tests job when I merge into develop but it also prevents when I do direct change and push in develop branch while I want it to run on direct push.
Note: Answer should be with native github workflow job definition condition, it should not be under steps. Also answer shouldn't based on commiter name. You can use this repo for testing if you wish https://github.com/hk1313/prtest
It's possible to add an if statement that would check if it's a push or not, for example:
name: PR test
on:
push:
branches:
- '**'
tags-ignore:
- '**'
jobs:
tests:
if: |
!(github.event_name == 'pull_request' && github.event.pull_request.merged == true)
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Run tests
run: |
echo "tests run"
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout#v3
- name: Build
run: |
echo "Build"
I would like to get the success/failure output of this action aws-actions/configure-aws-credentials#v1 in my next job. As per the action-output the output is aws-account-id.
hence I created below workflow, doesn't matter what I do, I don't seems to catch the output. My question is how to catch the error/success message for this action.
Right now I'm getting empty even if the job success/failed. Is there any way I can get the output of the steps?
jobs:
deploy_infra:
name: Deploy
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
outputs:
output1: ${{ steps.cac.outputs.aws-account-id }}
steps:
- name: Git Checkout
uses: actions/checkout#v3
- name: AWS Role
id: cac
uses: aws-actions/configure-aws-credentials#v1
with:
role-to-assume: arn:aws:iam::${{ github.event.inputs.account_id }}:role/assume-role
aws-region: ${{ github.event.inputs.aws_region }}
mask-aws-account-id: false
alert_job:
if: always()
needs: deploy_infra
name: Testing the status
runs-on: ubuntu-latest
steps:
- run: echo ${{needs.job1.outputs.output1}}
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
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"
My use case is, to trigger docs build when there is a trigger word in Pull Request Comments.
I am using pull-request-comment-trigger to know if a Trigger word is present in the code.
After knowing Action is triggered, I want to run some commands inside the repo. So, I have to use actions/checkout for that.
My doubt is, inside the run command, Only Shell Commands are valid, Right? I want to run another job if above condition satisfies.
My Current Yaml File
name: Testing GH Actions
on:
pull_request:
types: [opened]
jobs:
deploy:
runs-on: ubuntu-latest
steps:
- uses: khan/pull-request-comment-trigger#master
id: check
with:
trigger: "AppajiC"
reaction: rocket
env:
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
- run:
# Generally Anything here runs if below condition satisfies.
# I want to run another job here, which uses actions/checkout#v2 action
if: steps.check.outputs.triggered == 'true'
How can I achieve this ?
You can use the condition for your checkout step and the following steps:
- name: Checkout
uses: actions/checkout#v2
if: steps.check.outputs.triggered == 'true'
- name: Following step1
if: steps.check.outputs.triggered == 'true'
...
Alternatively, you can create a new job and use that if condition once:
jobs:
deploy:
runs-on: ubuntu-latest
outputs:
deploy-status: ${{ steps.check.outputs.triggered }}
steps:
- uses: khan/pull-request-comment-trigger#master
id: check
with:
trigger: 'AppajiC'
reaction: rocket
env:
GITHUB_TOKEN: ${{ github.token }}
# this job will only run if steps.check.outputs.triggered == 'true'
# you just need to write the if once
after-deploy:
runs-on: ubuntu-latest
needs: [deploy]
if: needs.deploy.outputs.deploy-status == 'true'
steps:
- name: Checkout
uses: actions/checkout#v2
...