stages: - update - build - verify - deploy variables: JAVA_HOME: "/opt/openjdk17" # Sets Java version to run (see /opt for details) VERSION_COMMIT: "GitLab CI Versioning Commit" # Commit text for version update VERSION_BRANCH: "update_version" # Branch used to merge version update commit # Sets the artifact cache to a local directory within build area MAVEN_CLI_OPTS: "-Dmaven.repo.local=../.m2/repository --batch-mode -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -P internal" #Workflow rules that filters across the entire pipeline. Cleaner than -o ci.skip since there won't be a "skipped" pipeline. workflow: rules: # If user is not the runner, branch is the default branch, and the pipeline source is a push - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: always - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: always # if the first statement fails, never succeed - when: never pom to next version: stage: update script: - export # --- Get old version - OLD_VERSION=$(mvn $MAVEN_CLI_OPTS -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec) # --- Update the version # ABC-bR-oldhash to ABC-b(R+1)-newhash - UPDATED_VERSION=`echo $OLD_VERSION | awk -F '[-b]+' '{printf "%s-b%s\n",$1,(++$2)}'` - echo $OLD_VERSION " updated to "$UPDATED_VERSION # --- Create temporary branch for merge # This helps "get around" the push restriction, if enforced # Check if the branch exists, if it does then forceably delete it. - if git show-ref --verify --quiet refs/heads/$VERSION_BRANCH; then git branch -D $VERSION_BRANCH; fi - git branch $VERSION_BRANCH - git checkout $VERSION_BRANCH # --- Set the update version in the pom file(s) - mvn $MAVEN_CLI_OPTS versions:set -q -DnewVersion=${UPDATED_VERSION} # --- Merge new version to default branch - git add -u # Only adds updated files, no new files (pom version backup skipped) - git commit -q -m "${VERSION_COMMIT}" - git checkout -q $CI_COMMIT_BRANCH - git pull -q origin $CI_COMMIT_BRANCH - git merge -q $VERSION_BRANCH # Use the runner authentication token to push up the merge commit - URL_TO_PUSH="http://${RUNNER_USER_LOGIN}:${RUNNER_USER_TOKEN}@${CI_SERVER_HOST}/${CI_PROJECT_PATH}.git" # Capture the merge request ID from the stderr output - git push $URL_TO_PUSH HEAD:$VERSION_BRANCH -o merge_request.create -o merge_request.target=$CI_COMMIT_BRANCH -o merge_request.title=auto-merge-request-update-version-to-$UPDATED_VERSION -o merge_request.remove_source_branch 2> merge_request_created - MR_ID=`grep "remote:" merge_request_created | grep http | sed 's:.*/::'` # Accept the merge request with the GITLAB API - URL_TO_MRACCEPT="${CI_SERVER_URL}/api/v4/projects/${CI_PROJECT_ID}/merge_requests/${MR_ID}/merge" # sleep of 1 second seems to resolve the issue of the accept MR failure - sleep 1 - 'curl --header "PRIVATE-TOKEN: ${RUNNER_USER_TOKEN}" --request PUT "${URL_TO_MRACCEPT}" > /dev/null 2>&1' - rm merge_request_created # remove the file used to capture stderr from git push rules: - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: never - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: never maven build: stage: build script: # --- Checks if code compiles - mvn $MAVEN_CLI_OPTS -U clean compile rules: - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: always - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: always maven verify: stage: verify script: # -fn indicates that it should never fail, this allows artifacts to be built even when unit tests fail to execute successfully - mvn $MAVEN_CLI_OPTS clean verify rules: - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: always - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: always # instruct gitlab to capture the test reports # test reporting to be handled in gitlab requires: # sudo gitlab-rails console # irb(main):001:0> Feature.enable(:junit_pipeline_view,Project.find(84)) artifacts: reports: junit: - target/surefire-reports/TEST-*.xml - target/failsafe-reports/TEST-*.xml # Allows a this job to fail, and the pipeline will continue allow_failure: true maven deploy: stage: deploy script: # --- Deploy to artifactory # Again, -fn might be necessary here if artifacts are to be deployed even when unit tests fail # -DskipTests will not execute unit tests, this should allow the artifact to be deployed if unit tests fail in the previous stage # -Dmaven.test.skip=true will also stop the unit tests from being compiled--which potentially would speed this up - mvn $MAVEN_CLI_OPTS -DskipTests clean deploy rules: - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH != $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: manual - if: '$GITLAB_USER_LOGIN != $RUNNER_USER_LOGIN && $CI_COMMIT_BRANCH == $CI_DEFAULT_BRANCH && $CI_PIPELINE_SOURCE == "push"' when: manual