# S3 Releases
#############

.release_s3:
  stage: release
  dependencies:
  - 'prebuilt helper images'
  - 'binaries'
  - 'package-deb'
  - 'package-rpm'
  - 'package-helpers'
  before_script:
  - ./ci/touch_git
  - |
    # checking GPG signing support
    if [ -f "$GPG_KEY_PATH" ]; then
      export GPG_KEY=$(cat ${GPG_KEY_PATH})
      export GPG_PASSPHRASE=$(cat ${GPG_PASSPHRASE_PATH})
    else
      echo -e "\033[0;31m****** GPG signing disabled ******\033[0m"
    fi
  script:
  - make release_s3
  tags:
  - !reference [.instance-medium]

.release_packagecloud:
  stage: release
  dependencies:
  - 'package-deb'
  - 'package-rpm'
  - 'package-helpers'
  before_script:
  - ./ci/touch_git
  script:
  - mage packagecloud:push deb "$CI_JOB_NAME" "$DIST_FLAVOR"
  - mage packagecloud:push rpm "$CI_JOB_NAME" "$DIST_FLAVOR"

.release_artifacts:
  artifacts:
    paths:
      - out/release_artifacts/*

development S3:
  extends:
  - .release_s3
  - .rules:runner-only:release:development:merge-requests
  environment:
    name: development/s3/${CI_COMMIT_REF_NAME}
    url: https://gitlab-runner-downloads.s3.amazonaws.com/${CI_COMMIT_REF_NAME}/index.html

bleeding edge S3:
  extends:
  - .release_s3
  - .rules:release:bleeding-edge
  environment:
    name: bleeding_edge/s3
    url: https://gitlab-runner-downloads.s3.amazonaws.com/${CI_COMMIT_REF_NAME}/index.html

stable S3:
  extends:
  - .release_s3
  - .rules:release:stable:branch
  environment:
    name: stable/s3
    url: https://gitlab-runner-downloads.s3.amazonaws.com/${CI_COMMIT_REF_NAME}/index.html

bleeding edge packagecloud:
  extends:
  - .release_packagecloud
  - .rules:release:bleeding-edge
  environment:
    name: bleeding_edge/packagecloud
    url: https://packages.gitlab.com/runner/unstable
  variables:
    PACKAGE_CLOUD_NAMESPACE: runner/unstable
  parallel:
    matrix:
    - DIST_FLAVOR:
      - debian
      - ubuntu
      - el
      - fedora
      - amazon
      - sles
      - opensuse

stable packagecloud:
  extends:
  - .release_packagecloud
  - .rules:release:stable:branch
  environment:
    name: stable/packagecloud
    url: https://packages.gitlab.com/runner/gitlab-runner
  parallel:
    matrix:
    - DIST_FLAVOR:
      - debian
      - ubuntu
      - raspbian
      - linuxmint
      - el
      - ol
      - fedora
      - amazon
      - sles
      - opensuse

# Image Registry Releases
#########################

development docker images:
  stage: release
  extends:
  - .docker
  - .go-cache
  - .rules:release:development:merge-requests
  dependencies:
  - 'helper images'
  - 'runner images'
  artifacts:
    paths:
      - out/release_artifacts/*
  script:
    - echo "${CI_REGISTRY_PASSWORD}" | docker login --username "${CI_REGISTRY_USER}" --password-stdin "${CI_REGISTRY}"
    - (cd scripts/pusher && go build)
    - tags=$(make print_image_tags)
    # dev gitlab registry images
    - (cd scripts/pusher && ./pusher runner-images.json "${CI_REGISTRY_IMAGE}/gitlab-runner-dev" $tags)
    - (cd scripts/pusher && ./pusher helper-images.json "${CI_REGISTRY_IMAGE}/gitlab-runner-helper-dev" $tags)

bleeding edge docker images:
  stage: release
  extends:
  - .docker
  - .go-cache
  - .rules:release:bleeding-edge
  environment:
    name: bleeding_edge/docker_images/linux
    url: https://hub.docker.com/r/gitlab/gitlab-runner/tags/
  dependencies:
  - 'helper images'
  - 'runner images'
  artifacts:
    paths:
      - out/release_artifacts/*
  script:
    - echo "${CI_REGISTRY_PASSWORD}" | docker login --username "${CI_REGISTRY_USER}" --password-stdin "${CI_REGISTRY}"
    - echo "${DOCKER_HUB_PASSWORD}" | docker login --username "${DOCKER_HUB_USER}" --password-stdin "registry.hub.docker.com/gitlab"
    - (cd scripts/pusher && go build)
    - tags=$(make print_image_tags)
    # bleeding gitlab registry images
    - (cd scripts/pusher && ./pusher runner-images.json "${CI_REGISTRY_IMAGE}" $tags)
    - (cd scripts/pusher && ./pusher helper-images.json "${CI_REGISTRY_IMAGE}/gitlab-runner-helper" $tags)
    # bleeding docker hub registry images
    - (cd scripts/pusher && ./pusher runner-images.json "registry.hub.docker.com/gitlab/gitlab-runner" $tags)
    - (cd scripts/pusher && ./pusher helper-images.json "registry.hub.docker.com/gitlab/gitlab-runner-helper" $tags)

stable docker images:
  stage: release
  variables:
    # Setting the CI_PROJECT_ID and CI_REGISTRY_IMAGE variable explicitly because we're
    # running this job also from the https://gitlab.com/gitlab-org/security/gitlab-runner
    # fork. But it should still create the release entry in the canonical one.
    CI_REGISTRY: registry.gitlab.com
    CI_REGISTRY_IMAGE: registry.gitlab.com/gitlab-org/gitlab-runner
  extends:
  - .docker
  - .go-cache
  - .rules:release:stable:branch
  environment:
    name: stable/docker_images/linux
    url: https://hub.docker.com/r/gitlab/gitlab-runner/tags/
  dependencies:
  - 'helper images'
  - 'runner images'
  artifacts:
    paths:
      - out/release_artifacts/*
  script:
    - echo "${CI_REGISTRY_PASSWORD}" | docker login --username "${CI_REGISTRY_USER}" --password-stdin "${CI_REGISTRY}"
    - echo "${DOCKER_HUB_PASSWORD}" | docker login --username "${DOCKER_HUB_USER}" --password-stdin "registry.hub.docker.com/gitlab"
    - aws --region us-east-1 ecr-public get-login-password | docker login --username "AWS" --password-stdin "public.ecr.aws/gitlab"
    - (cd scripts/pusher && go build)
    - tags=$(make print_image_tags)
    # stable gitlab registry images
    - (cd scripts/pusher && ./pusher runner-images.json "${CI_REGISTRY_IMAGE}" $tags)
    - (cd scripts/pusher && ./pusher helper-images.json "${CI_REGISTRY_IMAGE}/gitlab-runner-helper" $tags)
    # stable docker hub registry images
    - (cd scripts/pusher && ./pusher runner-images.json "registry.hub.docker.com/gitlab/gitlab-runner" $tags)
    - (cd scripts/pusher && ./pusher helper-images.json "registry.hub.docker.com/gitlab/gitlab-runner-helper" $tags)
    # stable aws registry images
    - (cd scripts/pusher && ./pusher runner-images.json "public.ecr.aws/gitlab/gitlab-runner" $tags)
    - (cd scripts/pusher && ./pusher helper-images.json "public.ecr.aws/gitlab/gitlab-runner-helper" $tags)
