Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between Github's "Environment" and "Repository" secrets?

In the GitHub documentation it states that the precedence of secrets is from lowest to highest (Environment > Repository > Organization), it also states that the Organization secrets are available for all repositories in the organization. But it doesn't state anything about Environment and Repository secrets.

My questions are:

  1. What is the difference is between Environment and Repository secrets?
  2. When should I use Environment secrets?
  3. When should I use Repository secrets?
like image 890
Adam Avatar asked Jan 29 '21 15:01

Adam


People also ask

What are repository secrets?

Secrets are encrypted environment variables that you create in an organization, repository, or repository environment. The secrets that you create are available to use in GitHub Actions workflows.

What are GitHub secrets used for?

GitHub Secrets are encrypted and allow you to store sensitive information, such as access tokens, in your repository.

Which type of repository in GitHub helps to make the update in a secure environment?

Secret scanning is enabled for all public repositories and is available for private repositories owned by organizations that are part of an enterprise with a license for GitHub Advanced Security. For more information, see the GitHub Enterprise Cloud documentation.

What are environment secrets in GitHub actions?

Well, environment secrets are specific to an environment in Github Actions which allow you to run different configurations for jobs in a single repository, e.g. to deploy to staging first and later to production.

What are GitHub secrets and how to use them?

Instead of hard-coding, you may want to store your environment variable securely, and GitHub secrets can do just that. GitHub encrypts the values you put in secrets, so they are not visible nor readable in the naked eye. The secret created with this method is accessible to the entire workflow, jobs, and steps; there are no restrictions. 1.

What is the difference between repository secrets and environment secrets?

Repository secrets are specific to a single repository (and all environments used in there), while organisation secrets are specific to an entire organisation and all repositories under it. You can use environment secrets if you have secrets which are specific to an environment.

How to add a repository secret in GitHub?

To store these files go to your GitHub project and go to Settings and then Secrets. Inside this menu click on New repository secret button to add the first one. As you have probably already noticed there is no way to upload your certificates directly to this interface.


2 Answers

Well, environment secrets are specific to an environment in Github Actions which allow you to run different configurations for jobs in a single repository, e.g. to deploy to staging first and later to production.

Repository secrets are specific to a single repository (and all environments used in there), while organisation secrets are specific to an entire organisation and all repositories under it.

You can use environment secrets if you have secrets which are specific to an environment.

If you are unsure, you could also start with repository secrets for everything. If you later introduce different environments which require different secrets, you can move the repository secrets to the specific environments. Due to the inheritance chain, this should be transparent to the jobs.

like image 57
Holger Just Avatar answered Oct 11 '22 10:10

Holger Just


To add to Holger Just's answer with an example workflow. The GitHub docs show an example when using the jobs.<job_id>.environment option in a workflow, but I think this is a more appropriate example.

name: Some task

on:
  push:
    branches:
      - main

jobs:
  prod-task:
    runs-on: ubuntu-latest
    environment: production
    steps:
      # uses production enviroment secrets over repository secrets
      - name: Run node build process
        run: "NODE_ENV=${{ env.NODE_ENV }} npm run build"
  dev-task:
    runs-on: ubuntu-latest
    environment: development
    steps:
      # uses development enviroment secrets over repository secrets
      - name: Run node build process
        run: "NODE_ENV=${{ env.NODE_ENV }} npm run build"
  task:
    runs-on: ubuntu-latest
    steps:
      # uses repository secrets as no environment is defined
      - name: Run node build process
        run: "NODE_ENV=${{ env.NODE_ENV }} npm run build"

Note: In the example above you can see the script accessing the environment variables via the env context using expressions.

So the idea is that when an environment is specified for a job, any secret used within that job, will use any environment-specific secret before using the repository secret.

To set an environment secret, navigate to the repo settings under the Environments section (i.e. https://github.com/<owner>/<repo>/settings/environments). Create or select an environment. Then add any secrets you need, see screenshot below. Make sure to provide the secret across all required environments which are to access it, otherwise the value will be inherited from parent env scope or possibly return ''.

enter image description here

like image 3
Nickofthyme Avatar answered Oct 11 '22 10:10

Nickofthyme