Yashmerino
Yashmerino

Reputation: 131

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?

Upvotes: 3

Views: 3520

Answers (2)

Chetan Talwar
Chetan Talwar

Reputation: 131

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

ActionExample

Reference -

Upvotes: 6

GuiFalourd
GuiFalourd

Reputation: 23040

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.

Upvotes: 4

Related Questions