.release_docker_images_base:
  stage: release
  extends:
  - .docker
  - .docker_qemu
  - .linux-dependency-checksums
  variables:
    PUBLISH_IMAGES: "true"
    PUSH_TO_DOCKER_HUB: "true"
    PUSH_TO_ECR_PUBLIC: "false"
  before_script:
  - source ci/touch_make_dependencies
  retry: 2

.release_docker_images:
  extends:
  - .release_docker_images_base
  dependencies:
  - 'binaries'
  - 'package-deb'
  - 'binaries-fips'
  - 'package-rpm: [package-rpm-fips]'
  script:
  - make release_docker_images
  parallel:
    matrix:
    - IMAGE_FLAVOR: ubi-fips
      TARGET_ARCHS: amd64
    - IMAGE_FLAVOR: ubuntu
      TARGET_ARCHS: amd64 arm64 ppc64le s390x
    - IMAGE_FLAVOR: alpine3.12
      TARGET_ARCHS: amd64 arm64 ppc64le s390x
    - IMAGE_FLAVOR: alpine3.13
      TARGET_ARCHS: amd64 arm64 ppc64le s390x
    - IMAGE_FLAVOR: alpine3.14
      TARGET_ARCHS: amd64 arm64 ppc64le s390x
    - IMAGE_FLAVOR: alpine3.15
      TARGET_ARCHS: amd64 arm64 ppc64le s390x

.release_helper_docker_images:
  extends:
  - .release_docker_images_base
  dependencies:
  - 'binaries'
  - 'helper images'
  - 'helper images fips'
  script:
  - make release_helper_docker_images
  parallel:
    matrix:
    - IMAGE_FLAVOR: "alpine"
      IMAGE_TAG: ["alpine3.12-"]
    - IMAGE_FLAVOR: "alpine3.13"
      IMAGE_TAG: ["alpine3.13-"]
    - IMAGE_FLAVOR: "alpine3.14"
      IMAGE_TAG: ["alpine3.14-"]
    - IMAGE_FLAVOR: "alpine3.15"
      IMAGE_TAG: ["", "alpine3.15-"]
    - IMAGE_FLAVOR: "alpine-latest"
      IMAGE_TAG: ["alpine-latest-"]
    - IMAGE_FLAVOR: "ubuntu"
      IMAGE_TAG: ["ubuntu-"]
    - IMAGE_FLAVOR: "ubi-fips"
      IMAGE_TAG: ["ubi-fips-"]

.release:
  stage: release
  before_script:
  - source ci/touch_make_dependencies

.release_s3:
  extends:
  - .release
  dependencies:
  - 'helper images'
  - 'helper images binaries'
  - 'binaries'
  - 'binaries-fips'
  - 'package-deb'
  - 'package-rpm'
  before_script:
  - source ci/touch_make_dependencies
  - |
    # 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

.release_packagecloud:
  extends:
  - .release
  dependencies:
  - 'package-deb'
  - 'package-rpm'
  script:
  - make release_packagecloud

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

development docker images:
  extends:
  - .release_docker_images
  - .rules:release:development:merge-requests
  variables:
    PUBLISH_IMAGES: "false"
    PUSH_TO_DOCKER_HUB: "false"
    PUSH_TO_ECR_PUBLIC: "false"

development helper docker images:
  extends:
  - .release_helper_docker_images
  - .rules:release:development:merge-requests
  variables:
    PUBLISH_IMAGES: "false"
    PUSH_TO_DOCKER_HUB: "false"
    PUSH_TO_ECR_PUBLIC: "false"

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

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

.bleding_edge_docker_images_base:
  extends:
  - .rules:release:bleeding-edge
  environment:
    name: bleeding_edge/docker_images/linux
    url: https://hub.docker.com/r/gitlab/gitlab-runner/tags/

bleeding edge docker images:
  extends:
  - .bleding_edge_docker_images_base
  - .release_docker_images

bleeding edge helper docker images:
  extends:
  - .bleding_edge_docker_images_base
  - .release_helper_docker_images

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

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

.docker_images_registry_base_variables_overwrite:
  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

.stable_docker_images_base:
  extends:
  - .rules:release:stable:branch
  - .docker_images_registry_base_variables_overwrite
  environment:
    name: stable/docker_images/linux
    url: https://hub.docker.com/r/gitlab/gitlab-runner/tags/

stable docker images:
  extends:
  - .stable_docker_images_base
  - .release_docker_images

stable helper docker images:
  extends:
  - .stable_docker_images_base
  - .release_helper_docker_images

stable gitlab release:
  stage: postrelease
  extends:
  - .rules:release:stable-or-rc
  dependencies: []
  image: registry.gitlab.com/gitlab-org/release-cli:latest
  variables:
    CHANGELOG: https://gitlab.com/gitlab-org/gitlab-runner/blob/$CI_COMMIT_TAG/CHANGELOG.md
    S3: https://gitlab-runner-downloads.s3.amazonaws.com/$CI_COMMIT_TAG
    # Setting the CI_PROJECT_ID 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_PROJECT_ID: 250833
  environment:
    name: stable/gitlab
    url: https://gitlab.com/gitlab-org/gitlab-runner/-/releases
  before_script: []
  script:
  - echo "Releasing to $S3"
  release:
    name: '$CI_COMMIT_TAG'
    description: |
      See [the changelog]($CHANGELOG) :rocket:

      GitLab Runner documentation can be found at https://docs.gitlab.com/runner/.
    tag_name: '$CI_COMMIT_TAG'
    ref: '$CI_COMMIT_TAG'
    assets:
      links:
      # binaries
      - name: 'binary: Linux amd64'
        url: '$S3/binaries/gitlab-runner-linux-amd64'
        filepath: '/binaries/gitlab-runner-linux-amd64'
      - name: 'binary: Linux arm'
        url: '$S3/binaries/gitlab-runner-linux-arm'
        filepath: '/binaries/gitlab-runner-linux-arm'
      - name: 'binary: Linux s390x'
        url: '$S3/binaries/gitlab-runner-linux-s390x'
        filepath: '/binaries/gitlab-runner-linux-s390x'

      - name: 'binary: macOS amd64'
        url: '$S3/binaries/gitlab-runner-darwin-amd64'
        filepath: '/binaries/gitlab-runner-darwin-amd64'
      - name: 'binary: macOS arm64'
        url: '$S3/binaries/gitlab-runner-darwin-arm64'
        filepath: '/binaries/gitlab-runner-darwin-arm64'

      - name: 'binary: Windows amd64'
        url: '$S3/binaries/gitlab-runner-windows-amd64.zip'
        filepath: '/binaries/gitlab-runner-windows-amd64.zip'
      - name: 'binary: Windows i386'
        url: '$S3/binaries/gitlab-runner-windows-386.zip'
        filepath: '/binaries/gitlab-runner-windows-386.zip'

      # DEB packages
      - name: 'package: DEB amd64'
        url: '$S3/deb/gitlab-runner_amd64.deb'
        filepath: '/packages/deb/gitlab-runner_amd64.deb'
      - name: 'package: DEB i386'
        url: '$S3/deb/gitlab-runner_i386.deb'
        filepath: '/packages/deb/gitlab-runner_i386.deb'
      - name: 'package: DEB armel'
        url: '$S3/deb/gitlab-runner_armel.deb'
        filepath: '/packages/deb/gitlab-runner_armel.deb'
      - name: 'package: DEB armhf'
        url: '$S3/deb/gitlab-runner_armhf.deb'
        filepath: '/packages/deb/gitlab-runner_armhf.deb'
      - name: 'package: DEB aarch64'
        url: '$S3/deb/gitlab-runner_aarch64.deb'
        filepath: '/packages/deb/gitlab-runner_aarch64.deb'
      - name: 'package: DEB arm64'
        url: '$S3/deb/gitlab-runner_arm64.deb'
        filepath: '/packages/deb/gitlab-runner_arm64.deb'

      # RPM packages
      - name: 'package: RPM amd64'
        url: '$S3/rpm/gitlab-runner_amd64.rpm'
        filepath: '/packages/rpm/gitlab-runner_amd64.rpm'
      - name: 'package: RPM i686'
        url: '$S3/rpm/gitlab-runner_i686.rpm'
        filepath: '/packages/rpm/gitlab-runner_i686.rpm'
      - name: 'package: RPM arm'
        url: '$S3/rpm/gitlab-runner_arm.rpm'
        filepath: '/packages/rpm/gitlab-runner_arm.rpm'
      - name: 'package: RPM armhf'
        url: '$S3/rpm/gitlab-runner_armhf.rpm'
        filepath: '/packages/rpm/gitlab-runner_armhf.rpm'
      - name: 'package: RPM arm64'
        url: '$S3/rpm/gitlab-runner_arm64.rpm'
        filepath: '/packages/rpm/gitlab-runner_arm64.rpm'
      - name: 'package: RPM aarch64'
        url: '$S3/rpm/gitlab-runner_aarch64.rpm'
        filepath: '/packages/rpm/gitlab-runner_aarch64.rpm'

      # Other files
      - name: 'checksums'
        url: '$S3/release.sha256'
        filepath: '/release.sha256'
      - name: 'checksums GPG signature'
        url: '$S3/release.sha256.asc'
        filepath: '/release.sha256.asc'
      - name: 'other release artifacts'
        url: '$S3/index.html'
        filepath: '/index.html'
