GitHub Actions Workflow Part 2 – Environment Variables
Hello Everyone
Welcome to CloudAffaire and this is Debjeet.
In today’s blog post, we will discuss env workflow element and environment variables in GitHub. You can use a user defined environment variables or GitHub inbuilt environment variables in your GitHub actions workflow.
User Defined Environment Variables:
env (optional): A map of environment variables that are available to the steps of all jobs in the workflow. You can also set environment variables that are only available to the steps of a single job or to a single step.
When more than one environment variable is defined with the same name, GitHub uses the most specific environment variable. For example, an environment variable defined in a step will override job and workflow variables with the same name, while the step executes. A variable defined for a job will override a workflow variable with the same name, while the job executes.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
name: GitHub Actions Demo on: [push] env: MYVAR1: scope_workflow jobs: job1: name: job1 runs-on: ubuntu-latest env: MYVAR2: scope_jobs steps: - name: step1 run: echo "${{ env.MYVAR1 }} ${{ env.MYVAR2 }} $MYVAR3" env: MYVAR3: scope_steps |
Default environment variables: These variables exist only on the runner that is executing your job. GitLab provides following default environment variables.
- CI: Always set to true.
- GITHUB_WORKFLOW: The name of the workflow.
- GITHUB_RUN_ID: A unique number for each run within a repository. This number does not change if you re-run the workflow run.
- GITHUB_RUN_NUMBER: A unique number for each run of a particular workflow in a repository. This number begins at 1 for the workflow’s first run, and increments with each new run. This number does not change if you re-run the workflow run.
- GITHUB_JOB: The job_id of the current job.
- GITHUB_ACTION: The unique identifier (id) of the action.
- GITHUB_ACTION_PATH: The path where your action is located. You can use this path to access files located in the same repository as your action. This variable is only supported in composite actions.
- GITHUB_ACTIONS: Always set to true when GitHub Actions is running the workflow. You can use this variable to differentiate when tests are being run locally or by GitHub Actions.
- GITHUB_ACTOR: The name of the person or app that initiated the workflow. For example, octocat.
- GITHUB_REPOSITORY: The owner and repository name. For example, octocat/Hello-World.
- GITHUB_EVENT_NAME: The name of the webhook event that triggered the workflow.
- GITHUB_EVENT_PATH: The path of the file with the complete webhook event payload. For example, /github/workflow/event.json.
- GITHUB_WORKSPACE: The GitHub workspace directory path, initially empty. For example, /home/runner/work/my-repo-name/my-repo-name. The actions/checkout action will check out files, by default a copy of your repository, within this directory.
- GITHUB_SHA: The commit SHA that triggered the workflow. For example, ffac537e6cbbf934b08745a378932722df287a53.
- GITHUB_REF: The branch or tag ref that triggered the workflow. For example, refs/heads/feature-branch-1. If neither a branch or tag is available for the event type, the variable will not exist.
- GITHUB_HEAD_REF: Only set for pull request events. The name of the head branch.
- GITHUB_BASE_REF: Only set for pull request events. The name of the base branch.
- GITHUB_SERVER_URL: Returns the URL of the GitHub server. For example: https://github.com.
- GITHUB_API_URL: Returns the API URL. For example: https://api.github.com.
- GITHUB_GRAPHQL_URL: Returns the GraphQL API URL. For example: https://api.github.com/graphql.
- RUNNER_NAME: The name of the runner executing the job.
- RUNNER_OS: The operating system of the runner executing the job. Possible values are Linux, Windows, or macOS.
- RUNNER_TEMP: The path to a temporary directory on the runner. This directory is emptied at the beginning and end of each job. Note that files will not be removed if the runner’s user account does not have permission to delete them.
- RUNNER_TOOL_CACHE: The path to the directory containing preinstalled tools for GitHub-hosted runners.
1 2 3 4 5 6 7 8 9 10 11 |
name: GitHub Actions Demo on: [push] jobs: job1: name: job1 runs-on: ubuntu-latest steps: - name: step1 run: | echo $GITHUB_REPOSITORY echo $GITHUB_REF |
Contexts: You can use most contexts at any point in your workflow, including when default environment variables would be unavailable. For example, you can use contexts with expressions to perform initial processing before the job is routed to a runner for execution; this allows you to use a context with the conditional if keyword to determine whether a step should run. Once the job is running, you can also retrieve context variables from the runner that is executing the job, such as runner.os.
- github: (object) The top-level context available during any job or step in a workflow.
- github.action: (string) The name of the action currently running. GitHub removes special characters or uses the name __run when the current step runs a script. If you use the same action more than once in the same job, the name will include a suffix with the sequence number with underscore before it. For example, the first script you run will have the name __run, and the second script will be named __run_2. Similarly, the second invocation of actions/checkout will be actionscheckout2.
- github.action_path: (string) The path where your action is located. You can use this path to easily access files located in the same repository as your action. This attribute is only supported in composite actions.
- github.actor: (string) The login of the user that initiated the workflow run.
- github.base_ref: (string) The base_ref or target branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target.
- github.event: (object) The full event webhook payload. You can access individual properties of the event using this context.
- github.event_name: (string) The name of the event that triggered the workflow run.
- github.event_path: (string) The path to the full event webhook payload on the runner.
- github.head_ref: (string) The head_ref or source branch of the pull request in a workflow run. This property is only available when the event that triggers a workflow run is either pull_request or pull_request_target.
- github.job: (string) The job_id of the current job.
- github.ref: (string) The branch or tag ref that triggered the workflow run. For branches this is the format refs/heads/<branch_name>, and for tags it is refs/tags/<tag_name>.
- github.repository: (string) The owner and repository name. For example, Codertocat/Hello-World.
- github.repository_owner: (string) The repository owner’s name. For example, Codertocat.
- github.run_id: (string) A unique number for each run within a repository. This number does not change if you re-run the workflow run.
- github.run_number: (string) A unique number for each run of a particular workflow in a repository. This number begins at 1 for the workflow’s first run, and increments with each new run. This number does not change if you re-run the workflow run.
- github.server_url: (string) Returns the URL of the GitHub server. For example: https://github.com.
- github.sha: (string) The commit SHA that triggered the workflow run.
- github.token: (string) A token to authenticate on behalf of the GitHub App installed on your repository. This is functionally equivalent to the GITHUB_TOKEN secret.
- github.workflow: (string) The name of the workflow. If the workflow file doesn’t specify a name, the value of this property is the full path of the workflow file in the repository.
- github.workspace: (string) The default working directory for steps and the default location of your repository when using the checkout action.
- env: (object) This context changes for each step in a job. You can access this context from any step in a job.
- env.<env_name>: (string) The value of a specific environment variable.
- job: (object) This context changes for each job in a workflow run. You can access this context from any step in a job.
- job.container: (object) Information about the job’s container.
- job.container.id: (string) The id of the container.
- job.container.network: (string) The id of the container network. The runner creates the network used by all containers in a job.
- job.services: (object) The service containers created for a job.
- job.services.<service id>.id: (string) The id of the service container.
- job.services.<service id>.network: (string) The id of the service container network. The runner creates the network used by all containers in a job.
- job.services.<service id>.ports: (object) The exposed ports of the service container.
- job.status: (string) The current status of the job. Possible values are success, failure, or cancelled.
- steps: (object) This context changes for each step in a job. You can access this context from any step in a job.
- steps.<step id>.outputs: (object) The set of outputs defined for the step.
- steps.<step id>.conclusion: (string) The result of a completed step after continue-on-error is applied. Possible values are success, failure, cancelled, or skipped. When a continue-on-error step fails, the outcome is failure, but the final conclusion is success.
- steps.<step id>.outcome: (string) The result of a completed step before continue-on-error is applied. Possible values are success, failure, cancelled, or skipped. When a continue-on-error step fails, the outcome is failure, but the final conclusion is success.
- steps.<step id>.outputs.<output name>: (string) The value of a specific output.
- runner.name: (string) The name of the runner executing the job.
- runner.os: (string) The operating system of the runner executing the job. Possible values are Linux, Windows, or macOS.
- runner.temp: (string) The path to a temporary directory on the runner. This directory is emptied at the beginning and end of each job. Note that files will not be removed if the runner’s user account does not have permission to delete them.
- runner.tool_cache: (string) The path to the directory containing preinstalled tools for GitHub-hosted runners.
- needs.<job id>: (object) A single job that the current job depends on.
- needs.<job id>.outputs: (object) The set of outputs of a job that the current job depends on.
- needs.<job id>.outputs.<output name>: (string) The value of a specific output for a job that the current job depends on.
- needs.<job id>.result: (string) The result of a job that the current job depends on. Possible values are success, failure, cancelled, or skipped.
1 2 3 4 5 6 7 8 9 10 11 |
name: GitHub Actions Demo on: [push] jobs: job1: name: job1 runs-on: ubuntu-latest steps: - name: step1 run: | echo "${{ github.ref }}" echo "${{ github.repository }}" |
How To Use Environment Variables In GitHub Actions
Prerequisites:
Step 1: Create a new GitHub repository for this demo.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
## I am using Linux Shell for this demo ## Replace github_personal_access_token ## github_repository_name, github_user_name ## Create a new GitHub repository curl -i -H "Authorization: token -d '{ "name": " "description": "mycicdpipeline", "auto_init": true, "private": true }' \ https://api.github.com/user/repos |
Step 2: Create your GitHub workflow file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
## Create your workflow file cat << 'EOF' > myworkflow.yaml name: GitHub Actions Demo on: [push] jobs: environment_variable_demo: name: environment_variable_demo runs-on: ubuntu-latest steps: - name: user defined env variables run: | echo $MYVAR env: MYVAR: debjeet - name: default system defined env variables run: | echo $GITHUB_REPOSITORY echo $GITHUB_REF - name: context env variables run: | echo "${{ github.ref }}" echo "${{ github.repository }}" EOF ## Create base64 version of workflow CONTENT=$(cat myworkflow.yaml | base64 -w 0) && cat << EOF > content.json { "message":"workflow added", "content":"$CONTENT" } EOF |
Observe, we have defined a workflow in such a way that it prints environment variable of each type in a separate step.
Step 3: Upload the workflow file in your GitHub repository.
1 2 3 4 5 |
## Upload the workflow file in GitHub curl -X PUT \ -H "Authorization: token https://api.github.com/repos/ -d @content.json |
This will trigger a new GitHub Actions which prints each type of environment variable in separate steps.
Step 4: Check GitHub workflow execution details.
1 2 3 4 5 6 7 |
## Get workflow details curl -H "Authorization: token https://api.github.com/repos/ ## Get details on workflow run status curl -H "Authorization: token https://api.github.com/repos/ |
You can also get the workflow execution details from Actions inside your GitHub repository.
Step 5: Delete the GitHub repository.
1 2 3 4 |
## Delete the GitHub repository curl -X DELETE \ -H "Authorization: token https://api.github.com/repos/ |
Hope you have enjoyed this article. To know more about GitHub, please refer below official documentation