How to build a custom GitHub Workflow with variables and scripts

How to build a custom GitHub Workflow with variables and scripts

github_custom_workflow_vars_scripts

Here in this article we will try to create GitHub Workflow with user defined variables and script. We will see how we can use the user defined variable as input arguments to a custom script which gets executed as a part of the GitHub Workflow.

Test Environment

Ubuntu 22.04

If you are interested in watching the video. Here is the YouTube video on the same step by step procedure outlined below.

Procedure

Step1: Clone Repository

As a first we are cloning a sample GitHub repository from my GitHub Account. You can fork this repository or create your own repository and use it for this activity.

[admin@fedser github_space]$ git clone https://github.com/novicejava1/learngit.git

Step2: Create Bash script

Here is a basic bash script which we will introduce into our repository. This script will capture the input arguments to the script and print the values for the same.

[admin@fedser learngit]$ cat hello-runner.sh 
#!/bin/bash

echo "This script captures the custom env variables and prints their value"

echo "language: '$1'"
echo "message: '$2'"

We also need to provide the execute permissions for the script as shown below.

[admin@fedser learngit]$ chmod 755 hello-runner.sh 

Step3: Create Workflow

Now its time to create our workflow. This workflow will checkout the repository and try to run the “hello-runner.sh” script with input arguments. The input arguments are fetched by defining them as environment variables at the step level as shown in the workflow. The environment variables that we have defined are “language” and “message” under the “env” key. These environment variables can be used for a single workflow

Variables provide a way to store and reuse non-sensitive configuration information. Environment variables can be defined at the workflow, job or step as details in “Defining environment variables for a single workflow“.

[admin@fedser learngit]$ cat .github/workflows/customworkflow.yml
name: GitHub Actions Custom Workflow Demo
run-name: ${{ github.actor }} is testing out GitHub Actiions Custom Workflow 🚀
on: [push]
jobs:
  Explore-GitHub-Actions-Custom-Workflow:
    runs-on: ubuntu-latest
    steps:
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: execute bash script
        run: ${{ github.workspace }}/hello-runner.sh $language $message
        env:
          language: "bash"
          message: "Hello GitHub Actions!!"

Step4: Push Changes

Now its time push our changes related to Workflow and the script that we have created.

[admin@fedser learngit]$ git add .

[admin@fedser learngit]$ git commit -m "create and update custom workflow"

[admin@fedser learngit]$ git push -u origin main

Step5: Validate Workflow

As soon as the push event is triggered on our GitHub repository, It will execute the respective GitHub Workflow’s that are defined under ./github/workflows folder. Here is the screenshot of the custom workflow status as shown below.

github_workflow_status_vars_script

Hope you enjoyed reading this article. Thank you..