Skip to content

Instantly share code, notes, and snippets.

@ei-grad
Last active April 26, 2017 22:05
Show Gist options
  • Save ei-grad/7efa32afdea48bedd3c09bcec9307217 to your computer and use it in GitHub Desktop.
Save ei-grad/7efa32afdea48bedd3c09bcec9307217 to your computer and use it in GitHub Desktop.
Build docker images in Gitlab CI
#!/usr/bin/env bash
set -x
set -e
# allows `docker build ...` to use images cache
# could be installed with: pip install restore_commit_times
#restore_commit_times .
DOCKERFILE_LOCATION="$1"
[ -z "$DOCKERFILE_LOCATION" ] && DOCKERFILE_LOCATION=.
[ -z "$DOCKER_NAME" ] && DOCKER_NAME=$CI_REGISTRY/$CI_PROJECT_PATH
[ -z "$DOCKER_TAG" ] && DOCKER_TAG=gitlab-$CI_PIPELINE_ID
docker build -t $DOCKER_NAME:$DOCKER_TAG $DOCKERFILE_LOCATION
(
# wait for 60 seconds for lock to be released and fail on timeout
flock -w 60 9 || exit 1
docker login -u gitlab-ci-token -p $CI_JOB_TOKEN $CI_REGISTRY
docker push $DOCKER_NAME:$DOCKER_TAG
docker tag $DOCKER_NAME:$DOCKER_TAG $DOCKER_NAME:latest
docker push $DOCKER_NAME:latest
docker tag $DOCKER_NAME:$DOCKER_TAG $DOCKER_NAME:$CI_COMMIT_REF_NAME
docker push $DOCKER_NAME:$CI_COMMIT_REF_NAME
) 9>/tmp/gitlab-ci-docker-build.lock
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment