Github-Actions Introduction
Contents covered:
jobs -- what are jobs in github workflow
runs-on -- what is this keyword mean
steps -- what are steps in github actions
Environment Variables -- what are env values, and how to use and reuse them
Defining environment variables for a single workflow
Defining configuration variables for multiple workflows
Writing values into GITHUB_ENV
JOBS_OUTPUT -- using one job output by another job
You can create a workflow file configured to run on specific events
Official Docs -> Configuring a workflow and Workflow syntax for GitHub Actions and GitHub Actions
Assuming you know the basics of actions like what are they I am continuing the explanation
Now the basic thing is that the actions must include jobs keyword and its actions
jobs
A job
is defined task made up of steps
. Each job is run in a fresh instance of virtual environment.
You can define the dependency rules for how jobs run in a workflow file. Jobs can run at the same time in parallel to be dependent on the status of a previous job and run sequentially
Example case: Workflow can have two sequential jobs that build and test code, where the test job is dependent on the status of the build job. If the build job fails, then test job will not run
runs-on
Github hosts virtual machine to run these jobs as mentioned above, we can mention which OS to run these jobs on
All the mentioned jobs execute in the same instance of the virtual environment by default but you can change it, allowing the actions in that job to share information using the file system
steps
A step is a set of tasks performed by a job. Each step in a job executes in the save environment, allowing the actions in that job to share information using the filesystem if necessary.
These steps can run system commands or actions that user require
Environment Variables
Now some command or actions might require some env variables that we cannot hardcode these values in our code, they might be some username passwords or api keys etc.
You can set a custom variables in two ways
To define an environment variable for use in a single workflow, you can use the
env
key in the workflow file.To define a configuration variable across multiple workflows, you can define it at the organization, repository, or environment level.
Now how can we mention them in our actions file
Now github by default gives access to some environment variables which depends on github user and varies from user to user
The complete list of github env variables that github provides by default are here default-environment-variables
Defining environment variables for a single workflow
To set a custom env variable for a single workflow, you can define it using the env
key in the workflow file
The scope of custom variable is set by this method is limited to the element in which it is defined.
You can define variables that are scoped
the entire workflow, by using
env
at the top level of the workflowthe contents of a job within a workflow, by defining the key value pair inside a job
a specific step within a job, by defining that key value pair inside that particular step
The commands in the run
steps of a workflow, or referenced actions, are processed by the shell you are using on the runner. The instructions in the other parts of a workflow are processed by Github Actions and are not sent to the runner.
Because runner environment variable interpolation is done after a workflow job is sent to a runner machine, you must use the appropriate syntax for the shell that's used on the runner.
Example: when you use ubuntu
these use bash shell, so you must use the syntax $KEY
, and for windows env use PowerShell, so you would use the syntax $env:KEY
Now you cant just name these env variables as you want, there are some default env variables that github gives you to use them and you cant name your env keys to be these names
Example in the above code snipper where we used, GITHUB_ACTOR as env value into run command directly you can create env value with key as GITHUB_ACTOR
If you try to override the value of one of these default variables, the assignment is ignored by github runners
Defining configuration variables for multiple workflows
You can create configuration variables for use across multiple workflows, and can define them at either the organization, repository or environment level
Example, you can use configuration variables to set default values for parameters passed to build tools at an organization level, but then allow repository owners to override these parameters on case-by-case basis
When you define configuration variables, they are automatically available in the vars
context,
If a configuration variable has not been set, the return value of a context referencing the variable will be empty string
The if conditionals are processed by Github Actions and only the steps where the check resolves as true are sent to the runner.
Writing values into GITHUB_ENV
If you generate a value in one step of a job, you can use the value in subsequent steps of the same job by assigning the value to an existing or new environment variable and then writing this to the GITHUB_ENV
environment file.
The environment file can be used directly by an action, or from a shell command in the workflow file by using the run
keyword.
JOBS_OUTPUT
If you want to pass a value from a step in one job in a workflow to a step in another job in the workflow, you can define the value as a job output Then you can then reference this job output from a step in another job
These outputs are "UNICODE" string, and can be a maximum of 1MB. The total of all outputs in a workflow can be a max of 50MB
Jobs outputs containing expressions are evaluated on the runner at the end of each job. Outputs containing secrets are redacted on the runner and not sent to the github actions
Last updated