-
Notifications
You must be signed in to change notification settings - Fork 577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to add github secrets to env variable #390
Comments
Wonder why you would include/use database credentials while building a Docker image?
Can't tell without a Dockerfile. Do you have a link to your repo? |
@crazy-max It's a private repo for crawler application, so need to save the data to MySQL db. below is the Dockerfile
Below is the python script call genenv.py. It can get the MySQL db information from the environment, and create the .env file for config to get the MySQL db information.
|
@machineCYC Thanks for your feedback. So in your case your workflow is right but the Dockerfile doesn't use those secrets through the # syntax=docker/dockerfile:1.2
FROM continuumio/miniconda3:4.3.27
RUN apt-get update
RUN mkdir /workspace
COPY . /workspace/
WORKDIR /workspace/
# install package
RUN pip install pipenv && pipenv sync
# genenv
RUN --mount=type=secret,id=MYSQL_HOST \
--mount=type=secret,id=MYSQL_USER \
--mount=type=secret,id=MYSQL_PASSWORD \
--mount=type=secret,id=MYSQL_PORT \
export MYSQL_HOST=$(cat /run/secrets/MYSQL_HOST) && \
export MYSQL_USER=$(cat /run/secrets/MYSQL_USER) && \
export MYSQL_PASSWORD=$(cat /run/secrets/MYSQL_PASSWORD) && \
export MYSQL_PORT=$(cat /run/secrets/MYSQL_PORT) && \
python genenv.py
# time
RUN echo "Asia/Taipei" > /etc/timezone
RUN dpkg-reconfigure -f noninteractive tzdata
CMD ["/bin/bash"] Fyi, we can't use the secret id directly as an env var atm. That's why we have to |
@crazy-max it's a great method, but this method can not build the image locally right? |
@machineCYC Everything this action does, you can do the same thing locally. You just have to copy the generated build command displayed in the action log and that's it. This one for example: For secrets you have to create them first. |
@crazy-max Create the secrets locally. before executing the blue color comment? |
Yes
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 RUN --mount=type=secret,id=mysql_host \
export MYSQL_HOST=$(cat /run/secrets/mysql_host) && \
echo $MYSQL_HOST |
This worked for me. Thanks, I documented everything here https://andrei-calazans.com/posts/2021-06-23/passing-secrets-github-actions-docker |
@crazy-max Thanks |
Hi, i tried to reproduce the same code as @AndreiCalazans but when i enter my docker container the secrets folder is empty
Docker file
|
I am getting a similar result. It seems that the secret comes through as Workflow
Dockerfile
Output
|
It's only at build time, they are not persisted.
Log output of GitHub Actions hide sensitive data like secrets with |
Thanks, @crazy-max. It hadn't occurred to me to check that! Everything was working fine, it turned out that the reason I was seeing an error was that my private key was not PEM-formatted. 😅 |
Hello I'm trying to do this but when I tried to access to my secret key in python is set to Workflow
Dockerfile
|
@amigohuhu can you provide a code sample. |
Below is the yml file, I hope to add github secrets to env variable.
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?
The text was updated successfully, but these errors were encountered: