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:
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.