# Extract Fly.io secrets and make them build-time secrets
while read -r secret; do
echo "export ${secret}=${!secret}" >> /srv/.secrets
deploy+=(--build-secret "${secret}=${!secret}")
done < <(flyctl secrets list --json | jq -r ".[].Name")
What this code is doing is reading the name of the secret from the output of the flyctl secrets list --json command (after passing the results through jq to extract the Name). So to populate the secret, you just need to run fly secrets set.
# Use all secrets from the build context
RUN --mount=type=secret,id=ALL_SECRETS \
eval "$(base64 -d /run/secrets/ALL_SECRETS)" && \
echo "Using secrets during build!"
# Copy application code
COPY . .
# Build application
RUN npx prisma generate
RUN npm run build
The RUN command that mounts your secrets only does it for the duration of that one step. What you will want to do instead is:
# Copy application code
COPY . .
# Build application using all secrets from the build context
RUN --mount=type=secret,id=ALL_SECRETS \
eval "$(base64 -d /run/secrets/ALL_SECRETS)" && \
echo "Using secrets during build!" && \
npx prisma generate && \
npm run build
I already have the list of secrets in fly secrets list but I don’t think the fly console can simply retrieve the values of the fly secrets? (am i wrong?)
There are a bunch of more sensitive secrets that I am not sure where to pass in. (for e.g. NEXT_PUBLIC_S3_ACCESS_ID, NEXT_PUBLIC_S3_ACCESS_KEY…). Do I just create another block like this
When you run fly console, the machine that you get has all of your secrets available as environment variables. The while read -r secret will iterate over the names of the secrets and add a --build-secret flag to the deploy command passing in the name and value of the secret. It will also create a script the exports all of the secrets and that script will be base64 encoded and passed as yet another build secret named ALL_SECRETS.