3. Variables
In any platform or tool facilitating the implementation of CI/CD, it is essential to have a mechanism to configure variables in the pipelines, depending on the different scopes of the pipeline implementation. This chapter explores the options for setting up GitHub Actions variables, how to scope them, naming conventions for variables, and the default variables in workflows. In GitHub Actions, you can define custom variables in the scope of a workflow, job, or step. Variables can be created or modified using commands in a workflow’s steps or actions. This section identified the options to define custom environment variables in a GitHub Actions workflow with syntax references. It explained how to use the variables in the workflow steps or actions. Additionally, it looked at how to change a variable value or create a variable via an action using the set-env command. CI: This variable value is always set to true. HOME: The home directory in the runner storing user data in the workflow. GITHUB_WORKFLOW: GitHub workflow name. GITHUB_RUN_ID: In a repo, each workflow run has a unique number. When rerunning an existing run, it does not change the run ID. GITHUB_RUN_NUMBER: The number for each run of the given workflows. If a repo has more than one workflow, the second or any other workflow’s first run begins with the number 1. If you re-run an existing workflow run, this number does not change. GITHUB_ACTION: The action’s identification. GITHUB_ACTIONS: This variable value is true if an action is running in a job. It identifies whether an action is running or not. GITHUB_ACTOR: The name of the person or app that initiated the workflow. GITHUB_REPOSITORY: The repository name and the owner. For example, chamindac/variabledemo. GITHUB_EVENT_NAME: The name of the webhook event that triggers the workflow. GITHUB_EVENT_PATH: The path of the file containing the payload of the webhook event which has triggered the workflow. GITHUB_WORKSPACE: This is the work directory in the job runner machine of the workflow. When actions/checkout action is used, a folder is created with the repo content inside the workspace folder. If the actions/checkout action is not used, the folder would be empty. GITHUB_SHA: The commit SHA that triggers the workflow. GITHUB_REF: The branch or tag ref that triggers the workflow. This variable is not available if the event triggering the workflow does not have a branch or tag. GITHUB_HEAD_REF: When a workflow is based on a forked repo, this variable contains the branch of the head repository. GITHUB_BASE_REF: When a workflow is based on a forked repo, this variable contains the branch of the base repository. GITHUB_SERVER_URL: The URL of the GitHub server (https://github.com). GITHUB_API_URL: The API URL (https://api.github.com). GITHUB_GRAPHQL_URL: The GraphQL API URL (https://api.github.com/graphql). We identified the predefined variables available in a GitHub Actions workflow and saw how to get additional variables according to the language/framework. GitHub Actions workflows allow you to define custom environment variables in a scoped workflow, job, or step. However, when defining your custom variables, there are a couple of things you must consider.
The GITHUB_ prefix is reserved for GitHub. You cannot use it in naming custom environment variables. If you try to use GITHUB_, it results in an error in the workflow. GitHub variables are case sensitive. Hence, a variable name and its usage should use the same case, or else the variable value cannot be retrieved in the usage location of the workflow. The variables you define to point to a filesystem location should contain the _PATH suffix. However, the HOME and GITHUB_WORKSPACE default variables do not use this convention because the words home and workspace imply a location. Even though there are no syntactical errors caused by using special characters in the middle of a variable name, it is better to avoid them at all costs because such variables cannot be properly retrieved when used in workflow steps/actions. Using an underscore (_) to separate parts of a variable name is acceptable. Variable names must begin with an alphabetical character and may contain numbers in the middle or at the end of the name. However, the variable name should not begin with a number. Special characters other than _ should be avoided. In this section we have looked at considerations in creating custom variables in GitHub Actions workflows. This chapter discussed using custom environment variables and the default variables available in GitHub Actions workflows and used a .NET Core example. It also discussed naming conventions for variables. The next chapter explores the use of secrets and tokens in GitHub Actions workflows.Defining and Using Variables
Variables in the Entire Workflow Scope
Variables in Job Scope
Variables in Step Scope
Using the set-env Command
Default Variables
Naming Considerations for Variables
GITHUB_ Prefix
Case Sensitivity
_PATH Suffix
Special Characters
Summary