Tomas Sirio

Sign in

If statements on Github Actions

Hey there!

Last week I faced the need to have different steps to be run when Github Actions was executed. Today I brought you a simple solution to how I managed to achieve this. Be warned, the important content of this post will be the Github Actions Workflow.

Step by step:

We define a workflow that will be run on a ‘push’ event. We name this job ‘project-a-or-project-b’ and we assign it to a ubuntu container with the latest version possible.

Our main program only calculates a random number between 0 and 1. We could say it’s mostly a flip-coin program. If we get 0 we will run our ‘projectA’ program and if we get 1, we run ‘projectB’

Inside ‘projectA’ and ‘projectB’ there’s just a Print indicating which routine is being run

Our first step on the workflow is going to check-out the main repository in the ./mainRepository folder and then it will install go 1.9.3 in order to run the main program.

Once we get to the ‘Run Main Repository’ step, we assign it the id ‘main’ which will allow us later to get it’s outputs. So we define an output of the program:

and then we indicate Github Actions that the OUPUT variable is going to be an output on that step:

Now it’s time to know which side our coin landed on:

We can define an if statement on the steps that will depend on our OUTPUT from the last step. You can get your outputs concatenating the string ‘steps.{step_name}.outputs.{step_output}

That’s it for today. I’ll leave you the repo so you can check if you are missing something while trying it

Originally published at https://dev.to on November 11, 2020.