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.