'If' Statements on Github Actions

'If' Statements on Github Actions
Techiio-author
Written by Abhishek JalanOctober 20, 2021
12 min read
Github
8 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 8 VIEWS SHARE
Techiio-author
Abhishek Jalan

DevSecOps Engineer

In this post we can talk about steps to run the Github Actions Workflow,

It takes time to master highly specialized and complex IT domains like software development and automation. But with time at a premium, you would often be required to deliver projects quickly. So, what do you do in that case? You look for simple solutions, right.

If you are tired of searching for easy-to-execute and straightforward steps to run the Github Actions Workflow, this post is for you. Because time is at a premium, we won't waste any more of it. Let's dive straight into the discussion.

GitHub Actions Workflow

Before we start, let's quickly look at a hypothetical Github Actions Workflow command screen that we will use as an illustration to help you better understand the steps involved.

GitHub Actions Workflow

Before we start, let's quickly look at a hypothetical Github Actions Workflow command screen that we will use as an illustration to help you better understand the steps involved.

on: push
jobs:
   project-a-or-project-b:
       runs-on: ubuntu-latest
       name: Project A or Project B
       steps:
       - name: Checkout Main repository
         uses: actions/checkout@v2
         with:
           path: ./mainRepository
       - uses: actions/setup-go@v1
         with:
           go-version: '1.9.3' # The Go version to download (if necessary) and use.
       - name: Run Main Repository 
         id: main
         run: |
           cd ./mainRepository &&
           OUTPUT=$(go run main.go | tail -1) &&
           echo "::set-output name=OUTPUT::$OUTPUT"
       - name: Run Project A
         if: steps.main.outputs.OUTPUT == 0
         run: |
           cd ./mainRepository/projectA &&
           go run main.go
       - name: Run Project B
         if: steps.main.outputs.OUTPUT == 1
         run: |
           cd ./mainRepository/projectB &&
           go run main.go

GitHub Actions Workflow - Steps Involved

First things first. Just like there must be a car to learn how to drive a car, there must be a workflow to learn how to run a workflow.

In the example above, we have first defined a workflow that runs on a 'push' event. We have divided it into Project A and Project B and assigned it to the latest version of an Ubuntu container. Because our main program only calculates a value equal to 0 or 1, we can also call it a flip-coin program. If the toss of the coin yields 0, we run Project A. If the coin yields 1, we run Project B. Simple, isn't it?

on: push
jobs:
   project-a-or-project-b:
       runs-on: ubuntu-latest
       name: Project A or Project B
package main
import (
   "fmt"
   "math/rand"
   "time"
)
func main() {
   s1 := rand.NewSource(time.Now().UnixNano())
   r1 := rand.New(s1)
   result := r1.Intn(2)
   fmt.Println(result)
}

The only command inside Project A and B is the 'Print' command, indicating which routine is currently being run.

package main
import "fmt"
func main() {
   fmt.Println("I'm running Project A")
}

Alright. We now have the car. It is time now to understand the steps involved in driving it.

Step 1

The first step is to check the main repository in the ./mainRepository folder and then install the ‘Go-version 1.9.3’ to run the main program.

  - name: Checkout Main repository
         uses: actions/checkout@v2
         with:
           path: ./mainRepository
       - uses: actions/setup-go@v1
         with:
           go-version: '1.9.3' # The Go version to download (if necessary) and use.

Step 2

The next step is to run the main repository. To do so, we first assign it the ID 'main' that will help us retrieve the output later. The other half of this step involves defining the output of the program. Once done, our next action is to let Github Actions know the OUTPUT variable will be the output obtained by running the main repository.

- name: Run Main Repository 
         id: main
         run: |
           cd ./mainRepository &&
           OUTPUT=$(go run main.go | tail -1) &&
           echo "::set-output name=OUTPUT::$OUTPUT"

Step 3

The last step is to define an ‘if’ statement to determine which project to run based on the value or output obtained from our flip-coin program. You can do so by linking the string 'steps. {step_name}.outputs.{step_output}'.

   - name: Run Project A
         if: steps.main.outputs.OUTPUT == 0
         run: |
           cd ./mainRepository/projectA &&
           go run main.go

That's it for today, folks!! We hope this post helped you learn something new and helpful today. Stay tuned in for more such exciting and value-adding updates. Till the next time, it's goodbye from us.


software development
Github Actions
flip coin program
GitHubActions
Workflow
8 VIEWS 1 LIKES 0 DISLIKES SHARE
1 LIKES 0 DISLIKES 8 VIEWS SHARE
Was this blog helpful?
techiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plantechiio-price-plan
You must be Logged in to comment
Code Block
Techiio-author
Abhishek Jalan
DevSecOps Engineer
Techiio-followerTechiio-followerTechiio-followerTechiio-followerTechiio-follower
+59 more
19 Blog Posts
9 Discussion Threads
Trending Technologies
15
Software40
DevOps46
Frontend Development24
Backend Development20
Server Administration17
Linux Administration26
Data Center24
Sentry24
Terraform23
Ansible83
Docker70
Penetration Testing16
Kubernetes21
NGINX20
JenkinsX17
Techiio-logo

Techiio is on the journey to build an ocean of technical knowledge, scouring the emerging stars in process and proffering them to the corporate world.

Follow us on:

Subscribe to get latest updates

You can unsubscribe anytime from getting updates from us
Developed and maintained by Wikiance
Developed and maintained by Wikiance