0

EDIT

I want to pass the last git short hash into my React app build with the following command: git log -1 --pretty=%h

Thus in my Dockerfile I want something such as:

ARG REACT_APP_GIT_SHORTHASH
RUN git clone https://token@github.com/paulywill/repo.git && cd repo && export REACT_APP_GIT_SHORTHASH=`git log -1 --pretty=%h`
ENV REACT_APP_GIT_SHORTHASH $REACT_APP_GIT_SHORTHASH

In my Github Actions build I'm getting the following:

Step 6/14 : ARG REACT_APP_GIT_SHORTHASH
 ---> Running in f45f530d5c76
Removing intermediate container f45f530d5c76
 ---> 87a91c010aaf
Step 7/14 : RUN git clone https://***@github.com/paulywill/repo.git && cd repo && export REACT_APP_GIT_SHORTHASH=$(git log -1 --pretty=%h)
 ---> Running in b8a8fa3cd703
Cloning into 'repo'...
Removing intermediate container b8a8fa3cd703
 ---> 5bbf3a76b928
Step 8/14 : ENV REACT_APP_GIT_SHORTHASH $REACT_APP_GIT_SHORTHASH
 ---> Running in f624f2e59dc6
Removing intermediate container f624f2e59dc6
 ---> d15c3c276062

Are these command even visible or able to pass values if they're different intermediate containers?

torek
  • 448,244
  • 59
  • 642
  • 775
paulywill
  • 629
  • 10
  • 29
  • 1
    I don't think this is a docker issue. You're running `$REACT_APP_GIT_SHORTHASH=git log -1 --pretty=%h` which is expanding the variable first. You probably want `&& export REACT_APP_GIT_SHORTHASH=...` (note no dollar sign) although you may want that on a separate run line, i'm not sure. The env variable might not be available to the next docker command if it's set in a RUN line. – Andy Ray Jan 09 '22 at 05:52
  • You're right about the export... but then the command following is not working, something to do with different `immediate container`? – paulywill Jan 09 '22 at 06:09
  • 1
    `RUN export` doesn't really do anything; see _e.g._ [docker ENV vs RUN export](https://stackoverflow.com/questions/33379393/docker-env-vs-run-export). There's not a good way to set an environment variable at build time based on the result of a command. If you can pass the value in as an `ARG`, though, you can set it from the `docker build` command; is that a possible approach? – David Maze Jan 09 '22 at 11:34
  • (You probably want to run `git clone` outside the Dockerfile, too: given what you've shown, your GitHub credentials will be in clear text in `docker history` of the image for anyone to see and reuse.) – David Maze Jan 09 '22 at 11:35
  • Since I'm using Github Actions, I'm going to place token in Secrets and attempt to set command with the `docker build` as you suggested. – paulywill Jan 12 '22 at 16:14

2 Answers2

1
ROM ubuntu:20.04
ARG REACT_APP_GIT_SHORTHASH
RUN apt-get update -y
RUN apt-get install git -y
RUN git clone https://github.com/pooya-mohammadi/deep_utils.git
WORKDIR deep_utils
RUN REACT_APP_GIT_SHORTHASH=`git log -1 --pretty=%h`
ENV REACT_APP_GIT_SHORTHASH $REACT_APP_GIT_SHORTHASH

Change deep_utils with your repo name. I found the cd <directory> to be problematic.

  • These are excellent steps... although I'm running a monorepo that's adding difficultes with the `WORKDIR` option, the `.git` is located in the parent directory above my `client` directory – paulywill Jan 12 '22 at 16:16
0

As per a previous answer and thanks in part for @david-maze for pointing me in the right direction, I can easily grab the git short hash before the docker build.

.github/deploy.yml

...
 - name: Set outputs
      id: vars
      run: echo "::set-output name=sha_short::$(git rev-parse --short HEAD)"
    - name: Check outputs
      run: echo ${{ steps.vars.outputs.sha_short }}
   ...
     docker build --build-arg REACT_APP_GIT_SHORTHASH=${{ steps.vars.outputs.sha_short }} -t $ECR_REPOSITORY_CLIENT .

Dockerfile

FROM node:16-alpine
ARG VERSION
ENV VERSION $VERSION
ARG REACT_APP_GIT_SHORTHASH
ENV REACT_APP_GIT_SHORTHASH $REACT_APP_GIT_SHORTHASH
...
paulywill
  • 629
  • 10
  • 29