Pass System Env Variable to Self-Hosted Github-Runner: A Step-by-Step Guide
Image by Felipo - hkhazo.biz.id

Pass System Env Variable to Self-Hosted Github-Runner: A Step-by-Step Guide

Posted on

Are you tired of hardcoding environment variables in your GitHub Actions workflows? Do you want to keep your secrets, well, secret? Look no further! In this article, we’ll take you on a journey to pass system environment variables to your self-hosted GitHub runner, ensuring a more secure and efficient workflow.

Why Do I Need to Pass System Env Variables?

Imagine having to update your workflow file every time you need to change a database password or an API key. Not only is it a hassle, but it also poses a significant security risk. Hardcoding sensitive information in your workflow file is a big no-no! By passing system environment variables, you can keep your secrets out of your codebase and focus on what matters – building amazing software.

What Are System Environment Variables?

Prerequisites

Before we dive into the tutorial, make sure you have the following:

  • A self-hosted GitHub runner set up and running
  • Access to your GitHub repository’s settings
  • A basic understanding of YAML and GitHub Actions

Step 1: Set System Environment Variables on Your Self-Hosted Runner

Log in to your self-hosted GitHub runner machine and set the system environment variables using the command line or your operating system’s environment variable settings. For this example, let’s set a variable named `MY_SECRET_KEY` with the value `super_secret_key`.

$ export MY_SECRET_KEY=super_secret_key

Verify that the variable has been set by running:

$ echo $MY_SECRET_KEY

This should output `super_secret_key`.

Step 2: Create a GitHub Actions Workflow File

Create a new file in your GitHub repository’s `.github/workflows` directory, e.g., `.github/workflows/my-workflow.yml`. This file will contain your workflow configuration.

name: My Workflow

on:
  push:
    branches:
      - main

jobs:
  my-job:
    runs-on: self-hosted
    steps:
      - name: Print MY_SECRET_KEY
        env:
          MY_SECRET_KEY: $MY_SECRET_KEY
        run: |
          echo "MY_SECRET_KEY: $MY_SECRET_KEY"

What’s Happening in This Workflow File?

This workflow file does the following:

  • Triggers on push events to the `main` branch
  • Runs on a self-hosted GitHub runner
  • Sets the `MY_SECRET_KEY` environment variable to the value of the system environment variable `MY_SECRET_KEY`
  • Prints the value of `MY_SECRET_KEY` to the console

Step 3: Update Your GitHub Repository Settings

Go to your GitHub repository’s settings and add a new secret environment variable. Name it `MY_SECRET_KEY` and set its value to the same value you set on your self-hosted runner (`super_secret_key`). This will allow your workflow to access the secret variable.

Variable Name Value
MY_SECRET_KEY super_secret_key

Step 4: Run Your Workflow

Push a change to your repository to trigger the workflow. You should see the output `MY_SECRET_KEY: super_secret_key` in your workflow logs.

Troubleshooting

If you encounter issues, make sure:

  • Your self-hosted GitHub runner is properly configured and running
  • The system environment variable is set correctly on your runner machine
  • The GitHub repository secret environment variable is set correctly
  • Your workflow file is correctly formatted and saved

Security Considerations

Remember to keep your system environment variables and GitHub repository secrets secure! Use secure practices when handling sensitive information, such as:

  • Limit access to your self-hosted runner machine and GitHub repository
  • Use secure protocols for communication (e.g., HTTPS)
  • Avoid hardcoding sensitive information in your codebase

Conclusion

Passing system environment variables to your self-hosted GitHub runner is a straightforward process that can greatly enhance the security and efficiency of your workflows. By following this guide, you’ve taken a significant step in keeping your secrets, well, secret! 🤫

Happy coding, and remember to keep it secure! 🚀

This article provides a comprehensive guide on passing system environment variables to a self-hosted GitHub runner, covering the reasons why, prerequisites, step-by-step instructions, and security considerations. The article is formatted using a variety of HTML tags to improve readability and is optimized for the target keyword “Pass system env variable to self-hosted github-runner”.

Frequently Asked Question

Passing system environment variables to self-hosted GitHub runners can be a bit tricky, but don’t worry, we’ve got you covered!

How do I pass system environment variables to my self-hosted GitHub runner?

You can pass system environment variables to your self-hosted GitHub runner by adding them to the `env` block in your `config.yml` file. For example, if you want to pass the `MY_VAR` system environment variable, you can add the following lines to your `config.yml` file: env: MY_VAR: $MY_VAR. This will make the `MY_VAR` environment variable available to your GitHub runner.

Can I pass all system environment variables to my self-hosted GitHub runner?

Yes, you can pass all system environment variables to your self-hosted GitHub runner by adding the following lines to your `config.yml` file: env: inherits: runner.os: true. This will inherit all system environment variables from the runner’s operating system.

How do I access system environment variables in my GitHub Actions workflow?

You can access system environment variables in your GitHub Actions workflow using the `env` context. For example, if you want to access the `MY_VAR` environment variable, you can use the following syntax: ${{ env.MY_VAR }}.

Can I override system environment variables in my GitHub Actions workflow?

Yes, you can override system environment variables in your GitHub Actions workflow by redefining them in the `env` block of your workflow file. For example, if you want to override the `MY_VAR` environment variable, you can add the following lines to your workflow file: env: MY_VAR: new_value.

Are system environment variables passed to my GitHub runner securely?

Yes, system environment variables are passed to your GitHub runner securely using encryption. GitHub ensures that environment variables are transmitted securely between the GitHub Actions server and your self-hosted runner.

Leave a Reply

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