how to make the latest image built with gitlab ci "latest"?

Viewed 114

I've got a spring boot with jib project where the latest Docker image that was built gets the "latest" tag. Now I've created a Dockerfile and the pipeline definition for my angular project but the latest tag is not being created. How do I make the latest image automatically latest, as is the case with my spring boot jib project?

My Dockerfile:

FROM nginx:latest
COPY dist/test-docker-angular-app/ /usr/share/nginx/html
COPY nginx.conf /etc/nginx/nginx.conf

My gitlab-ci.yml:

stages:
  - build
  - package

variables:
  VERSION: ${CI_COMMIT_SHORT_SHA}-${CI_COMMIT_REF_SLUG}

build-prod-app:
  stage: build
  image: node:latest
  script:
    - npm install -g @angular/cli@9.1.0
    - npm install
    - ng build --prod
  artifacts:
    paths:
      - dist/
    expire_in: 2 hours
  cache:
    paths:
      - node_modules/
docker-build:
  image: docker:stable
  stage: package
  services:
    - docker:dind
  before_script:
    - echo $CI_BUILD_TOKEN | docker login -u "$CI_REGISTRY_USER"
      --password-stdin $CI_REGISTRY
  script:
    - IMAGE_NAME="$CI_REGISTRY_IMAGE:$VERSION"
    - docker build --pull -t "$IMAGE_NAME" -f Dockerfile .
    - docker push "$IMAGE_NAME"
1 Answers

This has worked for me:

docker-build-master:
  image: docker:stable
  stage: package
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build --pull -t "$CI_REGISTRY_IMAGE" .
    - docker push "$CI_REGISTRY_IMAGE"
  only:
    - master

docker-build:
  image: docker:stable
  stage: package
  services:
    - docker:dind
  before_script:
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - IMAGE_NAME="$CI_REGISTRY_IMAGE:$VERSION"
    - docker build --pull -t "$IMAGE_NAME" .
    - docker push "$IMAGE_NAME"
  except:
    - master
Related