stages:
  - test
  - release
  - build

test amd64:
  image: docker:latest
  stage: test
  services:
    - name: docker:dind
      alias: docker
  variables:
    project_name: $CI_PROJECT_NAME
    SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build -t "$CI_REGISTRY_IMAGE:amd64-develop" -t "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-develop"
      --build-arg BUILD_DATE="$CI_PIPELINE_CREATED_AT"
      --build-arg VERSION="$CI_COMMIT_SHORT_SHA"
      --build-arg OVERLAY_ARCH="amd64"
      --build-arg FETCH=amd64/ .
    - docker push "$CI_REGISTRY_IMAGE:amd64-develop"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-develop"
    - docker build -t "$CI_REGISTRY_IMAGE:armhf-develop" -t "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-develop"
      --build-arg BUILD_DATE="$CI_PIPELINE_CREATED_AT"
      --build-arg VERSION="$CI_COMMIT_SHORT_SHA"
      --build-arg OVERLAY_ARCH="arm"
      --build-arg FETCH=armhf/ .
    - docker push "$CI_REGISTRY_IMAGE:armhf-develop"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-develop"
    - docker build -t "$CI_REGISTRY_IMAGE:arm64-develop" -t "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-develop"
      --build-arg BUILD_DATE="$CI_PIPELINE_CREATED_AT"
      --build-arg VERSION="$CI_COMMIT_SHORT_SHA"
      --build-arg OVERLAY_ARCH="aarch64"
      --build-arg FETCH=arm64/ .
    - docker push "$CI_REGISTRY_IMAGE:arm64-develop"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-develop"
    - docker manifest create "$CI_REGISTRY_IMAGE:develop"
      --amend "$CI_REGISTRY_IMAGE:amd64-develop"
      --amend "$CI_REGISTRY_IMAGE:armhf-develop"
      --amend "$CI_REGISTRY_IMAGE:arm64-develop"
    - docker push "$CI_REGISTRY_IMAGE:develop"
    - docker manifest create "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:develop"
      --amend "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-develop"
      --amend "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-develop"
      --amend "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-develop"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:develop"
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_TITLE =~ /^RELEASE:.+$/

release:
  needs: ['test docker']
  stage: release
  image: griefed/gitlab-semantic-release:latest
  services:
    - name: docker:dind
      alias: docker
  variables:
    project_name: $CI_PROJECT_NAME
    SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
  script:
    - npx semantic-release
  only:
    - master
  except:
    refs:
      - tags
    variables:
      - $CI_COMMIT_TITLE =~ /^RELEASE:.+$/

build:
  image: docker:latest
  stage: build
  services:
    - name: docker:dind
      alias: docker
  variables:
    project_name: $CI_PROJECT_NAME
    SEMANTIC_RELEASE_PACKAGE: $CI_PROJECT_NAME
  before_script:
    - docker login -u "$DOCKERHUB_USER" -p "$DOCKERHUB_TOKEN" docker.io
    - docker login -u "$CI_REGISTRY_USER" -p "$CI_REGISTRY_PASSWORD" $CI_REGISTRY
  script:
    - docker build -t "$CI_REGISTRY_IMAGE:amd64-latest" -t "$CI_REGISTRY_IMAGE:amd64-$CI_COMMIT_TAG" -t "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-latest" -t "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-$CI_COMMIT_TAG"
      --build-arg BUILD_DATE="$CI_PIPELINE_CREATED_AT"
      --build-arg VERSION="$CI_COMMIT_TAG"
      --build-arg OVERLAY_ARCH="amd64"
      --build-arg FETCH=amd64/ .
    - docker push "$CI_REGISTRY_IMAGE:amd64-latest"
    - docker push "$CI_REGISTRY_IMAGE:amd64-$CI_COMMIT_TAG"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-latest"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-$CI_COMMIT_TAG"
    - docker build -t "$CI_REGISTRY_IMAGE:armhf-latest" -t "$CI_REGISTRY_IMAGE:armhf-$CI_COMMIT_TAG" -t "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-latest" -t "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-$CI_COMMIT_TAG"
      --build-arg BUILD_DATE="$CI_PIPELINE_CREATED_AT"
      --build-arg VERSION="$CI_COMMIT_TAG"
      --build-arg OVERLAY_ARCH="arm"
      --build-arg FETCH=armhf/ .
    - docker push "$CI_REGISTRY_IMAGE:armhf-latest"
    - docker push "$CI_REGISTRY_IMAGE:armhf-$CI_COMMIT_TAG"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-latest"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-$CI_COMMIT_TAG"
    - docker build -t "$CI_REGISTRY_IMAGE:arm64-latest" -t "$CI_REGISTRY_IMAGE:arm64-$CI_COMMIT_TAG" -t "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-latest" -t "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-$CI_COMMIT_TAG"
      --build-arg BUILD_DATE="$CI_PIPELINE_CREATED_AT"
      --build-arg VERSION="$CI_COMMIT_TAG"
      --build-arg OVERLAY_ARCH="aarch64"
      --build-arg FETCH=arm64/ .
    - docker push "$CI_REGISTRY_IMAGE:arm64-latest"
    - docker push "$CI_REGISTRY_IMAGE:arm64-$CI_COMMIT_TAG"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-latest"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-$CI_COMMIT_TAG"
    - docker manifest create "$CI_REGISTRY_IMAGE:latest"
      --amend "$CI_REGISTRY_IMAGE:amd64-latest"
      --amend "$CI_REGISTRY_IMAGE:armhf-latest"
      --amend "$CI_REGISTRY_IMAGE:arm64-latest"
    - docker push "$CI_REGISTRY_IMAGE:latest"
    - docker manifest create "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"
      --amend "$CI_REGISTRY_IMAGE:amd64-$CI_COMMIT_TAG"
      --amend "$CI_REGISTRY_IMAGE:armhf-$CI_COMMIT_TAG"
      --amend "$CI_REGISTRY_IMAGE:arm64-$CI_COMMIT_TAG"
    - docker push "$CI_REGISTRY_IMAGE:$CI_COMMIT_TAG"
    - docker manifest create "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest"
      --amend "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-latest"
      --amend "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-latest"
      --amend "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-latest"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:latest"
    - docker manifest create "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
      --amend "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:amd64-$CI_COMMIT_TAG"
      --amend "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:armhf-$CI_COMMIT_TAG"
      --amend "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:arm64-$CI_COMMIT_TAG"
    - docker push "index.docker.io/$DOCKERHUB_USER/$DOCKERHUB_REPO:$CI_COMMIT_TAG"
  only:
    - tags