In the K8S environment, docker image is used for normal packaging. The best solution would be to pull the required JAR packages every time you pack, so that each project doesn’t have any impact, but that would be too expensive, and there would be coverage conflicts on the Nexus for each branch package. For example, if we have four environments where all development packages use snapshot, it is possible for two people A\B to use different branches to package at the same time, and they both depend on A common package, then it is possible for A to use B’s package and cause problems. This is called string package. So our solution is as follows,

  • Local warehouse according to the environment, to solve the problem of different environments
  • Version :set -dnewVersion =${CI_COMMIT_REF_NAME} -snapshot

The script is as follows, according to the branch version has not improved their own line modification, part of the variable description

  • GITLAB_AT: gitlab – Access_token
  • CI_COMMIT_REF_NAME: Git default environment variable, branch name
  • DEPENDENCY_LIST Specifies the build project to be depended on. A depends on B. Build B first and then A
  • BUILD_SUPPORT Custom business framework project build switch
  • BUILD_ARCH Custom infrastructure project build switch

The above variables are defined in Gitlab CI Variables.

#! /usr/bin/env bash
set -e
CUR_DIR=$(cd $(dirname $0);pwd)
function build(){
    GIT_URL="http://${GITLAB_USER:-username}:${GITLAB_AT}@gitlab.xxx.cn/reponame/The $1.git"
    if [ -d The $1 ];then
        rm -rf ${CUR_DIR}/The $1
    fi;
    git clone $GIT_URL
    cd ${CUR_DIR}/The $1
    count=$(git branch -a|grep ${CI_COMMIT_REF_NAME}|wc -l)
    if [ $count != 0 ];then
      git checkout ${CI_COMMIT_REF_NAME}
      env_name=$(get_env)
      mvn clean install -Dmaven.test.failure.ignore=true -DskipTests=true -Dmaven.repo.local=/root/.m2/repository-$env_name -U
    fi
    cd ${CUR_DIR}
    rm -rf ${CUR_DIR}/The $1
}

function build_dependency() {for PRO in $DEPENDENCY_LIST ;do
        build $PRO
    done
}

function get_env() {if [[ "${CI_COMMIT_REF_NAME}" =~ dev[0-9]* ]]; then
        echo "dev"
    elif [[ "${CI_COMMIT_REF_NAME}" =~ sit[0-9]* ]] || [[ "${CI_COMMIT_REF_NAME}" =~ sit[0-9]* ]]; then
        echo "sit"
    elif [[ "${CI_COMMIT_REF_NAME}"= ="master"[[]] | |"${CI_COMMIT_REF_NAME}"= ="release-"[[*]] | |"${CI_COMMIT_REF_NAME}"= ="hotfix-"*]];then
        echo "prod"
    else
        echo "dev"
    fi
}
function main() {if [[ -n ${BUILD_ARCH}[[]] &&${BUILD_ARCH}= =true]].then
      build framework-all
    fi
    if [[ -n ${BUILD_SUPPORT}[[]] &&${BUILD_SUPPORT}= =true]].then
      build support
    fi
    if [[ -n ${DEPENDENCY_LIST}]].then
      build_dependency
    fi
    env_name=$(get_env)
    mvn clean package -Dmaven.test.failure.ignore=true -DskipTests=true -Dmaven.repo.local=/root/.m2/repository-$env_name -U
}
main $*
Copy the code

You can use curl to pull the script from gitLab, and then configure the stage


mvn-package:
  stage: mvn-package
  script:
    - bash build.sh
    - cp target/app.war ${CACHE_PIPELINE}/app.war
  only:
    - sit
    - /^dev\d+$/
    - /^feature-.*$/
   
.auto_devops: &auto_devops | echo "GITLAB_AT=$GITLAB_AT; GITLAB_USER=$GITLAB_USER; BUILD_ARCH=$BUILD_ARCH; BUILD_SUPPORT=$BUILD_SUPPORT; DEPENDENCY_LIST=$DEPENDENCY_LIST" build_status=`curl -O -s -m 10 --connect-timeout 10 -w %{http_code} --header "PRIVATE-TOKEN:${GITLAB_AT}" http://gitlab.xxx.cn/package/bash/raw/master/build.sh` if [ $build_status != 200 ]; Then echo 'GITLAB_AT' exit 1 fi
Copy the code