image: golang:1.13
variables:
  GO111MODULE: "on"
  PROJECTNAME: "datadog-operator"
  TARGET_TAG: "v$CI_PIPELINE_ID-$CI_COMMIT_SHORT_SHA"
  DOCKER_REGISTRY: "486234852809.dkr.ecr.us-east-1.amazonaws.com/ci"
  GOPATH: "$CI_PROJECT_DIR/.cache"
  GIT_TERMINAL_PROMPT: "1"
  GOFLAGS: "-mod=vendor"
cache:
  paths:
  - .cache

stages:
  - build
  - test
  - image
  - release

before_script:
  - mkdir -p .cache
  - make install-tools

build:
  stage: build
  tags: [ "runner:main", "size:large" ]
  script:
    - make build

unit_tests:
  stage: test
  tags: [ "runner:main", "size:large" ]
  script:
    - make test

lint_code:
  stage: test
  tags: [ "runner:main", "size:large" ]
  script:
    - make validate

generate_code:
  stage: test
  tags: [ "runner:main", "size:large" ]
  script:
    - make generate
    - git diff --exit-code

build_image:
  stage: image
  tags: [ "runner:docker", "size:large" ]
  image: 486234852809.dkr.ecr.us-east-1.amazonaws.com/docker-notary:0.6.1
  before_script: []
  script:
    - IMAGE_NAME=$DOCKER_REGISTRY/$PROJECTNAME:$TARGET_TAG
    - docker build --build-arg TAG=$TARGET_TAG -t $IMAGE_NAME -f Dockerfile .
    - docker push $IMAGE_NAME

push_release_tag:
  only:
    - tags
  stage: release
  tags: [ "runner:docker", "size:large" ]
  image: 486234852809.dkr.ecr.us-east-1.amazonaws.com/docker-notary:0.6.1
  before_script: []
  script:
    - IMAGE_NAME=$DOCKER_REGISTRY/$PROJECTNAME:$TARGET_TAG
    - RELEASE_IMAGE_NAME=$DOCKER_REGISTRY/$PROJECTNAME:${CI_COMMIT_TAG:1}
    - docker pull $IMAGE_NAME
    - docker tag $IMAGE_NAME $RELEASE_IMAGE_NAME
    - docker push $RELEASE_IMAGE_NAME

.docker_hub_variables: &docker_hub_variables
  DOCKER_REGISTRY_LOGIN_SSM_KEY: docker_hub_login
  DOCKER_REGISTRY_PWD_SSM_KEY: docker_hub_pwd
  DOCKER_REGISTRY_URL: docker.io
  DOCKERHUB_REPO: operator
  DOCKERHUB_ORG: datadog

push_tag_to_docker_hub:
  only:
    - tags
  stage: release
  tags: [ "runner:docker", "size:large" ]
  image: 486234852809.dkr.ecr.us-east-1.amazonaws.com/docker-notary:0.6.1
  variables:
    <<: *docker_hub_variables
  before_script: []
  when: manual
  script:
    - DOCKER_REGISTRY_LOGIN=$(aws ssm get-parameter --region us-east-1 --name ci.datadog-operator.$DOCKER_REGISTRY_LOGIN_SSM_KEY --with-decryption --query "Parameter.Value" --out text)
    - aws ssm get-parameter --region us-east-1 --name ci.datadog-operator.$DOCKER_REGISTRY_PWD_SSM_KEY --with-decryption --query "Parameter.Value" --out text | docker login --username "$DOCKER_REGISTRY_LOGIN" --password-stdin $DOCKER_REGISTRY_URL
    - IMAGE_NAME=$DOCKERHUB_ORG/$DOCKERHUB_REPO:${CI_COMMIT_TAG:1}
    - docker build --build-arg TAG=${CI_COMMIT_TAG:1} -t $IMAGE_NAME -f Dockerfile .
    - docker push $IMAGE_NAME

# remove the leading dot from the name to let the job appear in gitlab
.push_latest_to_docker_hub:
  only:
    - tags
  stage: release
  tags: [ "runner:docker", "size:large" ]
  image: 486234852809.dkr.ecr.us-east-1.amazonaws.com/docker-notary:0.6.1
  variables:
    <<: *docker_hub_variables
  before_script: []
  when: manual
  script:
    - DOCKER_REGISTRY_LOGIN=$(aws ssm get-parameter --region us-east-1 --name ci.datadog-operator.$DOCKER_REGISTRY_LOGIN_SSM_KEY --with-decryption --query "Parameter.Value" --out text)
    - aws ssm get-parameter --region us-east-1 --name ci.datadog-operator.$DOCKER_REGISTRY_PWD_SSM_KEY --with-decryption --query "Parameter.Value" --out text | docker login --username "$DOCKER_REGISTRY_LOGIN" --password-stdin $DOCKER_REGISTRY_URL
    - IMAGE_NAME=$DOCKERHUB_ORG/$DOCKERHUB_REPO:latest
    - docker build --build-arg TAG=${CI_COMMIT_TAG:1} -t $IMAGE_NAME -f Dockerfile .
    - docker push $IMAGE_NAME
