Autobuild - how to get git info into the build process

I want my application to display git tag and commit information both when built locally and when built automatically on docker hub
After researching the documentation, I created the below build.sh file in the root folder

Script (/hooks/build)

#!/bin/bash
docker build -t $IMAGE_NAME --build-arg SOURCE_BRANCH=$SOURCE_BRANCH --build-arg SOURCE_COMMIT=$SOURCE_COMMIT  --build-arg IMAGE_NAME=$IMAGE_NAME .

Furthermore, there are steps in the build script that capture this information and copy them into a VERSION file

ARG SOURCE_BRANCH
ARG SOURCE_COMMIT
RUN echo ${SOURCE_BRANCH} - ${SOURCE_COMMIT} > VERSION
RUN cat ./VERSION

However, the VERSION file only contains ’ - '.

What am I doing wrong ?

Here’s the full Dockerfile

FROM node:10.15.3 as builder

ARG AP="/usr/myapp"

WORKDIR ${AP}
COPY . .
RUN npm install
RUN npm run build-prod

FROM node:10.15.3
LABEL "maintainer"="me@mycompany.com"
ARG AP="/usr/myapp"
ARG SOURCE_BRANCH
ARG SOURCE_COMMIT
ARG IMAGE_NAME="mycompany/batch-processes:latest"

RUN echo "building ${IMAGE_NAME}, branch ${SOURCE_BRANCH}:${SOURCE_COMMIT}"

# Set the working directory
WORKDIR ${AP}

# Install Node.js dependencies
COPY --from=builder ${AP}/package.json .
COPY --from=builder ${AP}/package-lock.json .
RUN npm install --production --no-optional

# Copy application files
COPY --from=builder --chown=node:node ${AP}/build .
RUN echo ${SOURCE_BRANCH} - ${SOURCE_COMMIT} > VERSION
RUN cat ./VERSION

# Change user
USER node

# Run the container under "node" user by default
CMD [ "node", "app.js" ]

According to other documentation, the build file should not have any extension.
Here is the (rather nugatory) error message I get when removing the extension on build.sh

Cloning into '.'...
Warning: Permanently added the RSA host key for IP address '18.205.93.0' to the list of known hosts.
Reset branch 'master'
Your branch is up-to-date with 'origin/master'.
Pulling cache layers for index.docker.io/bruyland/batch-processes:latest...
Done!
Executing build hook...
Unexpected error
Encountered error: [Errno 2] No such file or directory
Traceback (most recent call last):
File "/stage/builder/runner.py", line 290, in _run
self.build()
File "/stage/builder/runner.py", line 214, in build
self._build()
File "/stage/builder/runner.py", line 196, in _build
if not hooks.run('build'):
File "/stage/builder/hooks.py", line 21, in run
utils.execute_command(hook_path, '{} hook failed!'.format(name))
File "/stage/builder/utils.py", line 43, in execute_command
bufsize=1)
File "/usr/lib/python2.7/subprocess.py", line 711, in __init__
errread, errwrite)
File "/usr/lib/python2.7/subprocess.py", line 1343, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory

I would suggest to add the command env in the line before docker build .... This will allow you to see the actual available variables and their values.

Also it quite helpfull to add some logic to fill the variables, in case you want to build on your host:

# fallback when build on host
[ -n "$SOURCE_BRANCH" ]  || SOURCE_BRANCH=$(git symbolic-ref -q --short HEAD)
if [[ "${SOURCE_BRANCH}" =~ "master" ]]; then 
	VERSION=latest
elif [[ "${SOURCE_BRANCH/-*/}" =~ ^[0-9.]+*$ ]]; then
    VERSION=${SOURCE_BRANCH/-*/}
fi
[ -n "$IMAGE_NAME" ] || IMAGE_NAME=myrepo/myimage:${VERSION}

# additional details
BUILD_DATE=$(date -u +"%Y-%m-%dT%H:%M:%SZ") 
COMMIT_ID=$(git rev-parse --short HEAD)

I would also suggest to add the option --pull to your build command, to ensure the latest build of the parent image’s tag is used.

Turns out this was a problem with line endings.
Switching line endings to LF from CRLF solved the problem.
This is a bug in docker-hub’s build system. There’s no reason why line endings should matter in a script file.