image: docker:latest

variables:
  DOCKER_DRIVER: overlay2
  IMAGE_NAME: gitlab.nexdev.uk:5555/pub/rails-base-image-builder

stages:
  - prebuild
  - build
  - release

docker_lint:
  stage: prebuild
  image: hadolint/hadolint:latest-debian
  script:
  # Ignoring DL3008 - Pin Versions of apt-get modules
  - hadolint --ignore DL3008 Dockerfile
  tags:
  - nexus_runner

build:
  stage: build
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.nexdev.uk:5555
    - docker build -t $IMAGE_NAME:$CI_COMMIT_SHA .
    - docker push $IMAGE_NAME:$CI_COMMIT_SHA
  only:
    - tags
  tags:
    - nexus_runner

release:version:
  image: gitlab.nexdev.uk:5555/pub/nexus-release-image/master
  stage: release
  before_script:
    - eval $(ssh-agent -s)
    - echo "$CI_DEPLOY_KEY" | tr -d '\r' | ssh-add -
    - ssh-keyscan gitlab.nexdev.uk >> ~/.ssh/known_hosts
  script:
    - /build/release.sh --run-tag-pipeline
  tags:
    - nexus_runner
  only:
    - master

release_tag:
  stage: release
  script:
    - docker login -u gitlab-ci-token -p $CI_JOB_TOKEN gitlab.nexdev.uk:5555

    - docker tag $IMAGE_NAME:$CI_COMMIT_SHA $IMAGE_NAME:$CI_COMMIT_REF_NAME
    - docker push $IMAGE_NAME:$CI_COMMIT_SHA
    - docker push $IMAGE_NAME:$CI_COMMIT_REF_NAME
  only:
    - tags
  tags:
    - nexus_runner