Use Variables and Secrets Variables with Github Action’s Workflow

Introduction

GitHub Actions is an automation feature provided by GitHub that allows you to define workflows to automate tasks in your software development projects. It helps you build, test, and deploy your code by creating a series of customizable steps, known as a workflow, that are triggered by various events, such as code pushes, pull requests, or repository updates.

In GitHub Actions, you can use variables and secrets in your workflow to parameterize and secure your workflows. Here’s a basic guide on how to use variables and secrets in GitHub Actions workflows:

Set Variables in Workflow

You can define variables directly in your workflow file or set them at the job or step level. Here’s an example of setting a variable at the job level:

jobs:
  build:
    runs-on: ubuntu-latest
    env:
      MY_VARIABLE: "some_value"
    steps:
      - name: Use Variable
        run: echo $MY_VARIABLE

Accessing Variables

You can access variables using the env context. In the example above, $MY_VARIABLE is used to access the variable in the run step.

Using Secrets


Store Secrets in Repository: Navigate to your GitHub repository, go to “Settings” > “Secrets” > “New repository secret,” and add your secret.

Using Secrets in Workflow: You can use secrets in your workflow by referencing them with the secrets context. For example:

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Deploy to Production
        run: ./deploy.sh
        env:
          API_KEY: ${{ secrets.PRODUCTION_API_KEY }}

Combining Variables and Secrets

You can combine variables and secrets as needed in your workflow:

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v2

      - name: Deploy to Production
        run: ./deploy.sh
        env:
          API_KEY: ${{ secrets.PRODUCTION_API_KEY }}
          ENVIRONMENT: "production"

In this example, both the API_KEY (using a secret) and ENVIRONMENT (using a variable) are passed as environment variables to the deploy script.

Remember that secrets are encrypted and should not be exposed in logs. Be cautious about how you use and share them in your workflows.

Conclusion

We have show the use case of variable and secrets variables in GitHub Actions workflow, If you still have questions, please post them in the comments section below.

Author

Use Variables and Secrets Variables with Github Action’s Workflow

Leave a Reply

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

Scroll to top