How To Use Environment Secrets or Variables In GitHub Actions

No Comments
Published: 07.04.2024

Do you want to use a GitHub environment secret or variable inside your GitHub Action workflow? In this quick guide, we will first create a new environment with one secret and one variable, and then we will use both inside an example workflow run.
Environments are really handy as you can create one for your deployment target, for example, a VPS that you use to host projects. You can then use this environment across all your projects, which saves a lot of time when configuring an action for a new repository. For example this action to deploy a docker image, or this action to deploy a repository to your VPS.

Create a new GitHub environment

First, we will create a completely new GitHub environment. To do so, go to your repository, and click on “Settings” > “Environments” (in the left sidebar):

environment secrets and variables in github action: create a new environment

Now, click on “New environment”, give it a name (Example) and then click on “Configure environment”. With that, we created a new environment that we can use across our repositories. In the next step, we will create a variable and a secret.

Create a variable and a secret inside the GitHub environment

To now create them, you have to click on “Add secret” or “Add variable”:

environment secrets and variables in github action: example environment overview

We will start with a new secret. We click on “Add secret”, give it a name (SUPER_SECRET) and a value (Hello), and submit it. Now we do the same for the variable (name: PUBLIC_VAR, value: World). In the next step, we will use them inside an action.

Create a GitHub action and use the environment secret and variable

To create a new action, we go to the “Actions” tab and click on “set up a workflow yourself”. The action will be super basic and just echo the secret and the variable. To use them, you have to specify the environment at the beginning of the job:

Need help or want to share feedback? Join my discord community!

on:
  push:
    branches:
    - main

jobs:
  example:
    name: example
    environment: Example
    runs-on: ubuntu-22.04
    steps:
    - name: Print values
      run: echo "${{ secrets.SUPER_SECRET }} ${{ vars.PUBLIC_VAR }}"

Next, we commit the action, which then results in the following workflow run:

environment secrets and variables in github action: workflow run using them

And with that we have used the environment secrets and variables inside a GitHub Action run.

KOFI Logo

If this guide is helpful to you and you like what I do, please support me with a coffee!

Conclusion

In this quick guide, you learned how to create a new GitHub environment, for example, for a VPS, and then use its secrets and variables inside a GitHub Action. I hope this guide was useful to you. If you have any questions, feel free to ask them.

Sources:

Discussion (0)

Add Comment

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