Home > database >  Run a GitHub Action Last
Run a GitHub Action Last

Time:02-13

I have a bunch of GitHub actions for services that each build a container if something changes in their corresponding directories. Then I have a final GitHub actions that sets up a cloud environment with Terraform and uses the containers created by the other actions to deploy the microservices and API for my project.

Currently I'm running that final action manually after all the other actions complete. However, I was wondering if there is any way to automate this. I realize I can chain actions but I'm unsure how to handle this since any number of actions might be running and might finish in any order.

CodePudding user response:

You can turn each GitHub actions you have into a composite action for more reusability and readability.

But you can just combine all existing actions into 1 workflow - each in separate job if they need different runners.

Then you can set dependencies between those jobs them using needs syntax. At the end when they all complete, you can have 1 job that has needs for each of those combined and check if they succeeded and based on that execute what you need.

As an example:

jobs:
  job1:
  job2:
    needs: job1 //if you want to queue jobs then set dependency, if you want to run job1, job2 in parallel, remove this dependency
  final_action:
    needs: [job1, job2]
    if: ${{ needs.job1.result == 'success' && needs.job2.result == 'success' }}
    run: //PUBLISH STUFF
  notify_error:
    needs: [job1, job2]
    if: ${{ always() && !cancelled() && (needs.job1.result != 'success' || needs.job2.result != 'success') }}
    run: //NOTIFY FAILURE

CodePudding user response:

In one workflow? You can run all actions in parallel in their own job. Then use needs: for the final job to depend on all the other jobs and deploy the application in that job.

  • Related