Commit 8582ae23 authored by nimrod's avatar nimrod
Browse files

Output Docker build hash.

When building ci-images, images on the host are sometimes (in the case
of the docker image, always) pulled from the registry (overwriting the
tag) but before pushing. So the image pushed is that pulled just now.
Instead save the build hash (and pretty much everthing else) and re-tag
before pushing (using the hash so it's the right image). While at it,
unify the Docker push jobs (the Docker Hub and the personal registry
one).
parent 7f8085bf
Loading
Loading
Loading
Loading
Loading
+52 −42
Original line number Diff line number Diff line
@@ -3,7 +3,7 @@ dummy:
  stage: .post
  image: &image registry.shore.co.il/ci-images:docker
  before_script:
    - &tag |-
    - &TAG |-
      if [ -n "${CI_COMMIT_TAG:-}" ]
      then
        TAG="$CI_COMMIT_TAG"
@@ -14,20 +14,34 @@ dummy:
        TAG="$CI_COMMIT_BRANCH"
      fi
      export TAG
      echo "TAG=${TAG:-not defined}" | tee -a docker.env
    - &IMAGE |-
      CI_PROJECT_NAME_SLUG="$(echo $CI_PROJECT_NAME|awk '{print tolower($0)}')"
      IMAGE="${IMAGE:-$CI_PROJECT_NAME_SLUG}"
      export IMAGE
    - &debug |-
      echo "TAG: ${TAG:-not defined}"
      echo "IMAGE: ${IMAGE:-not defined}"
      echo "IMAGE=${IMAGE:-not defined}" | tee -a docker.env
    - &FULL_IMAGE |-
      FULL_IMAGE="$DOCKER_REGISTRY/$IMAGE:$TAG"
      export FULL_IMAGE
      echo "FULL_IMAGE=${FULL_IMAGE:-not defined}" | tee -a docker.env
    - &BUILD_HASH |-
      BUILD_HASH="$(docker image inspect --format '{{ .ID }}' "$FULL_IMAGE")"
      export BUILD_HASH
      echo "BUILD_HASH=${BUILD_HASH:-not defined}" | tee -a docker.env
    - &FULL_HASH |-
      FULL_HASH="$DOCKER_REGISTRY/$IMAGE@$BUILD_HASH"
      export FULL_HASH
      echo "FULL_HASH=${FULL_HASH:-not defined}" | tee -a docker.env
    - &DOCKER_REGISTRY |-
      echo "DOCKER_REGISTRY=${DOCKER_REGISTRY:-not defined}" | tee -a docker.env
    - &hub-login >-
      docker
      login
      --username adarnimrod
      --password "$DOCKER_HUB_TOKEN"
      [ "${DOCKER_REGISTRY:-}" = 'registry.shore.co.il' ] ||
      docker login --username adarnimrod --password "$DOCKER_HUB_TOKEN"
  script:
    - !!str true
  artifacts: &build-artifacts
    reports:
      dotenv: docker.env
  rules:
    - when: never

@@ -38,8 +52,7 @@ dummy:
    COMPOSE_DOCKER_CLI_BUILD: "1"
    DOCKER_BUILDKIT: "1"
  before_script:
    - *tag
    - *debug
    - *TAG
  script:
    - docker-compose build --no-cache --pull

@@ -48,13 +61,21 @@ dummy:
  stage: build
  variables:
    DOCKER_BUILDKIT: "1"
    DOCKER_REGISTRY: registry.hub.docker.com/adarnimrod
  before_script:
    - *tag
    - *TAG
    - *IMAGE
    - *debug
    - *hub-login
  script:
    - docker build -t "adarnimrod/$IMAGE:$TAG" ./
  script: &docker-build
    - docker build --pull --no-cache --tag "$DOCKER_REGISTRY/$IMAGE:$TAG" ./
  after_script: &docker-build-after-script
    - rm docker.env
    - *TAG
    - *IMAGE
    - *DOCKER_REGISTRY
    - *FULL_IMAGE
    - *BUILD_HASH
    - *FULL_HASH
  artifacts: *build-artifacts

.docker-build-shore:
  image: *image
@@ -62,62 +83,51 @@ dummy:
  tags: ["ns4.shore.co.il"]
  variables:
    DOCKER_BUILDKIT: "1"
    DOCKER_REGISTRY: registry.shore.co.il
  before_script:
    - *tag
    - *TAG
    - *IMAGE
    - *debug
  script:
    - docker build -t "registry.shore.co.il/$IMAGE:$TAG" ./
  script: *docker-build
  after_script: *docker-build-after-script
  artifacts: *build-artifacts

.compose-push:
  image: *image
  stage: deploy
  tags: ["ns4.shore.co.il"]
  needs:
    - job: build
      artifacts: true
  only:
    - branches
    - tags
  before_script:
    - *tag
    - *TAG
    - *hub-login
    - *debug
  script:
    - docker-compose push

.docker-push-hub:
.docker-push:
  image: *image
  stage: deploy
  needs:
    - job: build
      artifacts: true
  only:
    - branches
    - tags
  before_script:
    - *tag
    - *IMAGE
    - *debug
    - *hub-login
  script:
    - docker push "adarnimrod/$IMAGE:$TAG"

.docker-push-shore:
  image: *image
  stage: deploy
  tags: ["ns4.shore.co.il"]
  only:
    - branches
    - tags
  before_script:
    - *tag
    - *IMAGE
    - *debug
  script:
    - docker push "registry.shore.co.il/$IMAGE:$TAG"
    - docker image tag "$BUILD_HASH" "$FULL_IMAGE"
    - docker image push "$FULL_IMAGE"

.compose-run:
  image: *image
  stage: deploy
  when: manual
  before_script:
    - *tag
    - *debug
    - *TAG
  script:
    - docker-compose pull --quiet
    - docker-compose up --detach --remove-orphans