variables:
  MAVEN_CLI_OPTS: "-s $CI_PROJECT_DIR/.m2/settings.xml --batch-mode"
  MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository"
  MAVEN_USER_HOME: "$CI_PROJECT_DIR/.m2"
  REGISTRY_MIRROR: docker.repository.zcunsoft.com

stages:
  - build
  - package

.set-docker-tag: &set-docker-tag
  - if [[ $CI_BUILD_REF_NAME == "main" ]];
    then
        export DOCKER_TAG=latest;
    else
        export DOCKER_TAG="${CI_BUILD_REF_NAME}";
    fi

maven-build:
  image: openjdk:8-slim
  stage: build
  cache:
    key: maven-cache
    paths:
    - .m2/repository/
    - .m2/wrapper/
  script:
  - ./mvnw $MAVEN_CLI_OPTS package
  artifacts:
    name: "$CI_PROJECT_NAME-$CI_COMMIT_REF_NAME"
    paths:
    - "ruoyi-admin/target/*.jar"

docker-build:
  image:
    name: gcr.io/kaniko-project/executor:debug
    entrypoint: [""]
  stage: package
  script:
  - *set-docker-tag
  - mkdir -p /kaniko/.docker
  - echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"gitlab-ci-token\",\"password\":\"$CI_JOB_TOKEN\"}}}" > /kaniko/.docker/config.json
  - /kaniko/executor --context $CI_PROJECT_DIR --dockerfile $CI_PROJECT_DIR/Dockerfile --build-arg JAR_FILE="$(ls ruoyi-admin/target/*.jar | sort | head -n 1)" --destination $CI_REGISTRY_IMAGE:$DOCKER_TAG --cache=true --registry-mirror=$REGISTRY_MIRROR