question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How to add github secrets to env variable

See original GitHub issue

Below is the yml file, I hope to add github secrets to env variable.

name: Publish Docker image
on:
  push:
    branches: [ main ]

jobs:
  push_to_registry:
    name: Push Docker image to Docker Hub
    runs-on: ubuntu-latest

    steps:
      - name: Check out the repo
        uses: actions/checkout@v2

      - name: Log in to Docker Hub
        uses: docker/login-action@v1
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}

      - name: Push to Docker Hub
        uses: docker/build-push-action@v2
        with:
          push: true
          tags: docker_hub_name/image_name:latest
          secrets: |
            "MYSQL_HOST=${{ secrets.MYSQL_HOST}}"
            "MYSQL_USER=${{ secrets.MYSQL_USER}}"
            "MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD}}"
            "MYSQL_PORT=${{ secrets.MYSQL_PORT }}"

Then use syntax os.environ.get("MYSQL_HOST", "") to get my database information when build image.

But use the below method still not work, have something syntax wrong? or other methods can do it?

secrets: |
  "MYSQL_HOST=${{ secrets.MYSQL_HOST}}"
  "MYSQL_USER=${{ secrets.MYSQL_USER}}"
  "MYSQL_PASSWORD=${{ secrets.MYSQL_PASSWORD}}"
  "MYSQL_PORT=${{ secrets.MYSQL_PORT }}"

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:5
  • Comments:14 (5 by maintainers)

github_iconTop GitHub Comments

15reactions
AndreiCalazanscommented, Jun 23, 2021

This worked for me. Thanks, I documented everything here https://andrei-calazans.com/posts/2021-06-23/passing-secrets-github-actions-docker

6reactions
crazy-maxcommented, Jun 23, 2021

@machineCYC

Create the secrets locally. before executing the blue color comment?

Yes

How to create the secrets locally?

https://docs.docker.com/engine/reference/commandline/secret_create/#create-a-secret

You can also provide secrets through env vars. So let’s say you already have the env var MYSQL_HOST set locally, you can then set secret in buildx command: --secret id=mysql_host,env=MYSQL_HOST and use it like this in your Dockerfile:

RUN --mount=type=secret,id=mysql_host \
  export MYSQL_HOST=$(cat /run/secrets/mysql_host) && \
  echo $MYSQL_HOST
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use environment secret on github action? · Issue #785
Instead of putting it in your environmental variables and using the os.environ() command, you can just reference the secret directly. E.g. ...
Read more >
Use Secret Environment Variables in GitHub Actions
Head to your repo in GitHub that needs the variables and click on the Settings tab. Inside of Settings, go down the sidebar...
Read more >
How to use environment variables and secrets using GitHub ...
To store these files go to your GitHub project and go to Settings and then Secrets . Secrets tab. Inside this menu click...
Read more >
Launching GitHub Actions Environment Variables and Secrets
Setting for GitHub Actions Environment Variables · 1. Create a directory named . · 2. Next, create a file with your preferred name...
Read more >
Github Actions Environment Variables: Guide, Examples & Tips
Github Actions environment variables allow you to store information―API keys, login credentials, app secrets, constants, etc―to use in your ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found