Home > Net >  Use build-arg from docker to create json file
Use build-arg from docker to create json file

Time:05-06

I have a docker build command which I'm running in Jenkins execute shell

docker build -f ./fastlane.dockerfile \
  -t fastlane-test \
  --build-arg PLAY_STORE_CREDENTIALS=$(cat PLAY_STORE_CREDENTIALS) \
  .

PLAY_STORE_CREDENTIALS is a JSON file saved in Jenkins using managed files. And, then, inside my Dockerfile, I have

ARG PLAY_STORE_CREDENTIALS
ENV PLAY_STORE_CREDENTIALS=$PLAY_STORE_CREDENTIALS
WORKDIR /app/packages/web/android/fastlane/PlayStoreCredentials
RUN touch play-store-credentials.json
RUN echo $PLAY_STORE_CREDENTIALS >> ./play-store-credentials.json
RUN cat play-store-credentials.json

cat logs out a empty line or nothing at all.

Content of PLAY_STORE_CREDENTIALS:

{
  "type": "...",
  "project_id": "...",
  "private_key_id": "...",
  "private_key": "...",
  "client_email": "...",
  "client_id": "...",
  "auth_uri": "...",
  "token_uri": "...",
  "auth_provider_x509_cert_url": "...",
  "client_x509_cert_url": "..."
}

Any idea where the problem is?

CodePudding user response:

Is there actually a file named PLAY_STORE_CREDENTIALS? If it is, and if it's a standard JSON file, I would expect your given command line to fail; if the file contains any whitespace (which is typical for JSON files), that command should result in an error like...

"docker build" requires exactly 1 argument.

For example, if I have in PLAY_STORE_CREDENTIALS the sample content from your question, we see:

$ docker build -t fastlane-test --build-arg PLAY_STORE_CREDENTIALS=$(cat PLAY_STORE_CREDENTIALS) .
"docker build" requires exactly 1 argument.
See 'docker build --help'.

Usage:  docker build [OPTIONS] PATH | URL | -

...because you are not properly quoting your arguments. If you adopt @β.εηοιτ.βε's suggestion and quote the cat command, it appears to build as expected:

$ docker build -t fastlane-test --build-arg PLAY_STORE_CREDENTIALS="$(cat PLAY_STORE_CREDENTIALS)" .

[...]

Step 7/7 : RUN cat play-store-credentials.json
 ---> Running in 29f95ee4da19
{ "type": "...", "project_id": "...", "private_key_id": "...", "private_key": "...", "client_email": "...", "client_id": "...", "auth_uri": "...", "token_uri": "...", "auth_provider_x509_cert_url": "...", "client_x509_cert_url": "..." }
Removing intermediate container 29f95ee4da19
 ---> b0fb95a9d894
Successfully built b0fb95a9d894
Successfully tagged fastlane-test:latest

You'll note that the resulting file does not preserve line endings; that's because you're not quoting the variable $PLAY_STORE_CREDENTIALS in your echo statement. You should write that as:

RUN echo "$PLAY_STORE_CREDENTIALS" >> ./play-store-credentials.json

Lastly, it's not clear why you're transferring this data using environment variables, rather than just using the COPY command:

COPY PLAY_STORE_CREDENTIALS ./play-store-credentials.json

In the above examples, I'm testing things using the following Dockerfile:

FROM docker.io/alpine:latest

ARG PLAY_STORE_CREDENTIALS
ENV PLAY_STORE_CREDENTIALS=$PLAY_STORE_CREDENTIALS
WORKDIR /app/packages/web/android/fastlane/PlayStoreCredentials
RUN touch play-store-credentials.json
RUN echo $PLAY_STORE_CREDENTIALS >> ./play-store-credentials.json
RUN cat play-store-credentials.json

Update

Here's an example using the COPY command, where the value of the PLAY_STORE_CREDENTIALS build argument is a filename:

FROM docker.io/alpine:latest

ARG PLAY_STORE_CREDENTIALS
WORKDIR /app/packages/web/android/fastlane/PlayStoreCredentials
COPY ${PLAY_STORE_CREDENTIALS} play-store-credentials.json
RUN cat play-store-credentials.json

If I have credentials in a file named creds.json, this builds successfully like this:

docker build -t fastlane-test --build-arg PLAY_STORE_CREDENTIALS=creds.json .
  • Related