Git

How to Use Environment Variables in GitHub Actions?

As a professional programmer, GitHub actions are the most commonly used feature in GitHub as it allows you to build, test, and deploy the project automatically. The environment variable is a feature inside the GitHub actions that increases the reusability and is helpful for achieving workflow tasks.

This hands-on tutorial will provide the procedure for using environment variables in GitHub actions.

How to Use Environment Variables in GitHub Actions?

Environment variables are very commonly used features in GitHub actions to store and use sensitive/non-sensitive data within the workflow. To initialize it in the GitHub actions, we use the β€œenv” keyword.

Let’s use environment variables in GitHub actions practically.

Step 1: Open GitHub Repository

Access your GitHub, select and open the repository desired repository in which you want to use the environment variables:


Step 2: Access Actions Tab

Afterward, access the β€œActions” tab of the repository by clicking on it:


Step 3: Create a Workflow File and Use the Environment Variable

As we know the workflow is created in the .yml file in the directory β€œ.github/workflow/” inside the main repository. We have created and committed the β€œAction-variable.yml” file with the following workflow code:

name: Greeting on variable day

on:
  workflow_dispatch

env:
  Name: Mateen

jobs:
  greeting_job:
    runs-on: ubuntu-latest
    env:
       blog: LinuxHint
    steps:
      - name: "LinuxHint Tutorial."
        run: echo "My name is $Name, Welcome to $blog"

 
The above-mentioned code is defined as:

    • The β€œname” attribute represents the workflow name.
    • The β€œon” specifies the event in which the workflow will be called.
    • The β€œenv” is listing the environment variables.
    • The β€œjobs” are the actions to be performed.
    • The β€œsteps” are using and printing the defined variables.

 

Step 4: Open Created Workflow

Once the workflow is created, navigate to the particular workflow inside the β€œActions” tab:


Step 5: Run GitHub Actions Workflow

Finally, run the GitHub actions workflow to check the results:


Step 6: Check Results

Upon running the workflow, you can see that our defined environment variables are successfully running:


By following these steps, you can create the environment variables inside your GitHub actions.

Conclusion

To use the environment variables in GitHub actions, open the particular repository, create the workflow, and use the β€œenv” attribute to define the environment variables. For printing it, use the β€œecho” command. After that, run the workflow and check the results. This write-up has lightened up the steps for utilizing environment variables in GitHub actions.

About the author

Abdul Mateen

I hold a bachelor's degree in Computer Science and have a good technical background. As an author, I break down technical concepts in a clear and concise manner. Learning new skills and exploring new technologies is my passion.