If condition in Github Actions for another Job - github

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
...

Related

git steps command to get output from action

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}}

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 workflow only on deployment_status AND specific branch

Similar tho this question How to run Github Actions with the 'deployment_status' kitty and only on the QAS branch?
I want to execute a workflow only on a specific branch but combined with the deployment_status trigger.
So I want to execute my end to end test only if the deployment is done and we are on the develop branch.
The problem here is: On the trigger deployment_status the github.ref variable is not set. It is also written in docs. So I can not do a manual check if I am on the right branch.
name: E2E
on:
deployment_status:
# does not work because it is ignored on deployment_status
branches:
- develop
- stage
- master
- main
jobs:
chrome:
if: github.event.deployment_status.state == 'success'
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Run Cypress
uses: cypress-io/github-action#v2
with:
browser: chrome
cache-key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
env:
CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}
On the other way: If i use the deployment_status trigger I can not use branches or branches-ignore in combination. This is not working at all.
name: E2E
on:
deployment_status:
jobs:
chrome:
# does not work because github.ref is not defined
if: github.event.deployment_status.state == 'success' && github.ref == 'refs/heads/develop' /
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout#v2
- name: Run Cypress
uses: cypress-io/github-action#v2
with:
browser: chrome
cache-key: ${{ runner.os }}-yarn-${{ hashFiles('**/yarn.lock') }}
env:
CYPRESS_BASE_URL: ${{ github.event.deployment_status.target_url }}
Any ideas how to solve this?

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"

A workflow is not triggering a second workflow

The workflow in file inrisk.packages.ci.yml generates a tag and a realise of the code when a push is done in the develop branch. The below works as expected.
name: Code Int
on:
push:
paths:
- 'infra/**'
jobs:
ci:
runs-on: ubuntu-latest
steps:
# Checks-out to $GITHUB_WORKSPACE
- uses: actions/checkout#v2
- name: Basic Checks
run: |
whoami
ls -lah
pwd
- uses: actions/setup-node#v1
# Create a new release when on develop which triggers the deployment
- name: Bump version and push tag
if: github.ref == 'refs/heads/develop'
uses: mathieudutour/github-tag-action#v4.5
id: tag_version
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
- name: Create Release
if: github.ref == 'refs/heads/develop'
id: create_release
uses: actions/create-release#v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
tag_name: ${{ steps.tag_version.outputs.new_tag }}
release_name: Release ${{ steps.tag_version.outputs.new_tag }}
draft: false
prerelease: false
The below workflow in file inrisk.packages.cd.yml and is suppose to be triggered when ever a tag/realise is created/published.
name: Code Deploy
on:
push:
tags:
- 'v*'
release:
types:
- published
- created
- released
jobs:
build-and-deploy:
runs-on: ubuntu-latest
steps:
# Checks-out to $GITHUB_WORKSPACE
- uses: actions/checkout#v2
- uses: actions/setup-node#v1
- name: Install Yarn
run: npm install -g yarn
- uses: chrislennon/action-aws-cli#v1.1
- name: Install, Build and Deploy
run: |
whoami
ls -lah
pwd
The second workflow Code Deploy dose not get trigger after Code Int publishes/created a tag/realise
However when I manually create a realise/tag the second workflow Code Deploy get triggered
This seems to be by design as stated here .This is to stop recursive workflow runs.
I used this article to get around the problem