Categories
Automation

GitHub Actions Tutorial Part-2: Environment Variables and Accessing Repository

In this part, we will focus on how to add variables in GitHub actions and accessing repository code inside GitHub actions. Also as a bonus, we will see how to use GitHub secrets in Actions.

First let’s create all the files required.

Remove or put the following content in the hello_world.yml. For the sake of this tutorial, I will be creating a new yml file at workflows directory.

action-demo/.github/workflows/variables_and_repository.yml

on:
  push:
    branches:
      - master

name: Variables and Repository
jobs:
  variables-and-repository:
    name: print variables and list repository content
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: Printing variables and repository content
      uses: './'
      env: # Define environment vars
        # variable_name: value
        user_name: Devarshi Sathiya

As you can see in the above file I just added env and user_name part and updated naming related content.

The environment variables will be passed to your Docker environment so that you can access them in the entrypoint.sh

Update entrypoint.sh with below content

action-demo/entrypoint.sh

#!/bin/bash

echo "##### Listing Github repository content #####"
ls $GITHUB_WORKSPACE

echo "##### cat fact.txt from GitHub repository #####"
cat $GITHUB_WORKSPACE/fact.txt

echo "##### Example of environment variables #####"
echo "Hey I am $user_name"

As you can see I have used a variable `GITHUB_WORKSPACE` which is not in the workflow file. It is one of the GitHub Actions default variables. It basically contents the GitHub workspace directory path.

Also, I have added a file named fact.txt at the root of the repository which contents a fact about Linux.

action-demo/fact.txt

First Linux Kernel, version 0.01, had just 10,239 lines of code (Source: Wikipedia)

Push the updated code in the repository and go to actions run or debug page and click on Printing variables and repository content

Final output of the Variables and repository access part.

As you can see in the above image we have got the value of the environment variable declared in the workflow, listed repository content and echoed a file.

Once you can access the content of repository in Actions you can do a lots of interesting stuff and do automation.

GitHub secrets in Actions

First, let’s create a secret named `SECRET_NAME` ( yeah it is not an appropriate name but works for our demo) with some secret value.

Create a GitHub secret

Add following line in env section of action-demo/.github/workflows/variables_and_repository.yml.

GH_secret: ${{ secrets.SECRET_NAME }}

Here GH_secret is the variable name which we can access in the Docker environment with the value of our secret.

Also, append the following lines to action-demo/entrypoint.sh

echo "##### Secret Value #####"
echo $GH_secret

Push the code and see the results in the Actions window.

GitHub action run for secrets

As you can see instead of printing the actual secret value it printed ***. It is for security reasons so that we don’t accidentally print the value of a secret.

GitHub by default adds GITHUB_TOKEN as a secret which can be used for various operations in Actions. With this token, you can get various permission to perform other actions.

To read more about GITHUB_TOKEN : https://help.github.com/en/actions/configuring-and-managing-workflows/authenticating-with-the-github_token

You can use GITHUB_TOKEN as any other secret.

So in the next part, we will edit and push code in a branch with GitHub Actions.’

Leave a Reply

Your email address will not be published. Required fields are marked *