variables:
  DEBUG: 'true'
  RUSTC_TARGET: x86_64-unknown-linux-gnu

default:
  image: rust:1.95-bullseye

Test:
  script:
    - cargo test

Build Image:
  image:
    name: gcr.io/kaniko-project/executor:v1.23.2-debug
    entrypoint:
      - ""
  needs:
    - Test
  variables:
    DOCKER_CONFIG: /kaniko/.docker
  rules:
    - if: $CI_COMMIT_BRANCH
    - if: $CI_COMMIT_TAG
  script:
    - mkdir -p "$DOCKER_CONFIG"
    - |
      cat > "$DOCKER_CONFIG/config.json" <<EOF
      {"auths":{"$CI_REGISTRY":{"username":"$CI_REGISTRY_USER","password":"$CI_REGISTRY_PASSWORD"}}}
      EOF
    - |
      DESTINATIONS="--destination ${CI_REGISTRY_IMAGE}:${CI_COMMIT_SHA}"
      if [ "$CI_COMMIT_BRANCH" = "$CI_DEFAULT_BRANCH" ]; then
        DESTINATIONS="$DESTINATIONS --destination ${CI_REGISTRY_IMAGE}:latest"
      fi
      if [ -n "$CI_COMMIT_TAG" ]; then
        DESTINATIONS="$DESTINATIONS --destination ${CI_REGISTRY_IMAGE}:${CI_COMMIT_TAG}"
      fi

      /kaniko/executor \
        --context "$CI_PROJECT_DIR" \
        --dockerfile "$CI_PROJECT_DIR/Dockerfile" \
        --cache=true \
        $DESTINATIONS

Image Smoke:
  image: "$CI_REGISTRY_IMAGE:$CI_COMMIT_SHA"
  needs:
    - Build Image
  rules:
    - if: $CI_COMMIT_BRANCH
    - if: $CI_COMMIT_TAG
  script:
    - hen --version
    - hen verify ./examples/map_request.hen --output json > /dev/null

Publish:
  needs:
    - Test
  rules:
    - if: $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH
  script:
    - cargo publish --token $CARGO_PUBLISH_TOKEN


