Skip to content
Snippets Groups Projects
Commit 4ed10884 authored by Lene Wasskog's avatar Lene Wasskog
Browse files

chore: Reorganize gitlab ci/cd for build + deploy to production

Remove snapshot before building, commit new version
Deploy war file built in previous step to production
Deploy the same file to package registry, and tag the release
parent 99581187
No related branches found
No related tags found
1 merge request!35build: New dummy version for testing the pipeline
...@@ -37,7 +37,7 @@ before_script: ...@@ -37,7 +37,7 @@ before_script:
- apt-get update -y && apt-get install -y python3 && apt-get install -y rsync openssh-client - apt-get update -y && apt-get install -y python3 && apt-get install -y rsync openssh-client
- "python3 build_pom_with_models.py" - "python3 build_pom_with_models.py"
build: build-for-staging:
stage: build stage: build
script: script:
- "./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS package -DskipTests" - "./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS package -DskipTests"
...@@ -48,6 +48,51 @@ build: ...@@ -48,6 +48,51 @@ build:
- target/*.war - target/*.war
rules: rules:
- if: $CI_COMMIT_REF_NAME == $MAIN_BRANCH - if: $CI_COMMIT_REF_NAME == $MAIN_BRANCH
build-for-production:
stage: build
script:
# Use access token given by CI/CD variable ACCESS_TOKEN to authenticate
# This is necessary in order to push changes to git
- &authenticate
- export NEW_REPO_URL=https://root:$ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git
- git remote set-url --push origin $NEW_REPO_URL
- git fetch origin --prune --prune-tags
# Checkout release branch
- &checkout_release
- git stash -a
- git checkout $RELEASE_BRANCH
- git reset --hard origin/$RELEASE_BRANCH
- git stash pop
# Set release version. Commit and push if tag for release version does not already exist.
- "./mvnw $MAVEN_CLI_OPTS $MAVEN_OPTS validate -DremoveSnapshot"
- "python3 build_pom_with_models.py"
- export RELEASE_VERSION=$(./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS --batch-mode --no-transfer-progress --non-recursive help:evaluate -Dexpression=project.version | grep -v "\[.*")
- echo "New version in release '$RELEASE_VERSION'"
# If tag for new release version already exist, exit with error
- export RELEASE_TAG="v${RELEASE_VERSION}"
- echo "Release tag '$RELEASE_TAG'"
- export EXISTING_TAGS=$(git ls-remote --tags origin | cut -f 2 | grep "refs/tags/$RELEASE_TAG$")
- echo "Existing tags '$EXISTING_TAGS'"
- if [ -n "$EXISTING_TAGS" ]; then
echo "Tag $RELEASE_TAG already exists"; exit 1;
fi
# If release tag does not already exist, commit and push pom with new version
- git add pom.xml
- git commit -m "[ci skip] Set release version ${RELEASE_VERSION}"
- git push origin $RELEASE_BRANCH
# Build new war file
- echo "Build war file with version '$RELEASE_VERSION'"
- "./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS package -DskipTests"
tags:
- vips-java
artifacts:
paths:
- target/*.war
rules:
- if: $CI_COMMIT_REF_NAME == $RELEASE_BRANCH - if: $CI_COMMIT_REF_NAME == $RELEASE_BRANCH
test: test:
...@@ -116,7 +161,7 @@ upload-snapshot: ...@@ -116,7 +161,7 @@ upload-snapshot:
deploy-to-production: deploy-to-production:
stage: deploy stage: deploy
script: script:
# Authentication setup # Authentication setup, for running commands on server
- mkdir -p ~/.ssh - mkdir -p ~/.ssh
- echo "$SSH_PRIVATE_KEY" >> ~/.ssh/id_dsa - echo "$SSH_PRIVATE_KEY" >> ~/.ssh/id_dsa
- chmod 600 ~/.ssh/id_dsa - chmod 600 ~/.ssh/id_dsa
...@@ -157,56 +202,52 @@ upload-and-tag-release: ...@@ -157,56 +202,52 @@ upload-and-tag-release:
stage: upload stage: upload
script: script:
# Use access token given by CI/CD variable ACCESS_TOKEN to authenticate # Use access token given by CI/CD variable ACCESS_TOKEN to authenticate
- &authenticate # This is necessary in order to push changes to git
- export NEW_REPO_URL=https://root:$ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git - export NEW_REPO_URL=https://root:$ACCESS_TOKEN@$CI_SERVER_HOST/$CI_PROJECT_PATH.git
- git remote set-url --push origin $NEW_REPO_URL - git remote set-url --push origin $NEW_REPO_URL
- git fetch origin --prune --prune-tags - git fetch origin --prune --prune-tags
# Checkout release branch # Checkout release branch
- &checkout_release - git stash -a
- git stash -a - git checkout $RELEASE_BRANCH
- git checkout $RELEASE_BRANCH - git reset --hard origin/$RELEASE_BRANCH
- git reset --hard origin/$RELEASE_BRANCH - git stash pop
- git stash pop
# Set release version and deploy. Commit, tag and push. # Set release version and deploy. Commit, tag and push.
- &release_and_tag # Change version in pom.xml - build new pom_with_models.xml - export RELEASE_VERSION=$(./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS --batch-mode --no-transfer-progress --non-recursive help:evaluate -Dexpression=project.version | grep -v "\[.*")
- "./mvnw $MAVEN_CLI_OPTS $MAVEN_OPTS validate -DremoveSnapshot" - export RELEASE_TAG="v${RELEASE_VERSION}"
- "python3 build_pom_with_models.py" - echo "Version to deploy '$RELEASE_VERSION', will be tagged '$RELEASE_TAG'"
- export RELEASE_VERSION=$(./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS --batch-mode --no-transfer-progress --non-recursive help:evaluate -Dexpression=project.version | grep -v "\[.*")
- echo "New version in release '$RELEASE_VERSION'" # Find name of war file in target folder (built previously in build stage), exit if none exists
- export RELEASE_TAG="v${RELEASE_VERSION}" - WAR_FILE=$(find target -maxdepth 1 -type f -name "*.war" | sort -n | tail -1 | xargs basename)
- echo "Release tag '$RELEASE_TAG'" - if [ -z "$WAR_FILE" ]; then echo "No war file found in target directory"; exit 1; fi
- export EXISTING_TAGS=$(git ls-remote --tags origin | cut -f 2 | grep "refs/tags/$RELEASE_TAG$") - echo "War file built in build job '$WAR_FILE'"
- echo "Existing tags '$EXISTING_TAGS'"
- if [ -n "$EXISTING_TAGS" ]; then # Old way of deploying newly built war:
echo "Tag $RELEASE_TAG already exists"; exit 1; # - "./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS deploy -DskipTests"
fi
- "./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS deploy -DskipTests" # TRY THIS - MIGHT NOT WORK DUE TO MISSING repositoryId and url - https://maven.apache.org/guides/mini/guide-3rd-party-jars-remote.html
- git add pom.xml - "./mvnw deploy:deploy-file -DpomFile=pom.xml -Dfile=target/$WAR_FILE"
- git commit -m "[ci skip] Set release version ${RELEASE_VERSION}"
- git tag -a $RELEASE_TAG -m "Tag release ${RELEASE_TAG}" - git tag -a $RELEASE_TAG -m "Tag release ${RELEASE_TAG}"
- git push origin $RELEASE_TAG - git push origin $RELEASE_TAG
- git push origin $RELEASE_BRANCH
# Checkout main branch # Checkout main branch
- &checkout_main - git stash -a
- git stash -a - git checkout $MAIN_BRANCH
- git checkout $MAIN_BRANCH - git reset --hard origin/$MAIN_BRANCH
- git reset --hard origin/$MAIN_BRANCH - git stash pop
- git stash pop
# Merge release branch, bump patch version, push commits. # Merge release branch, bump patch version, push commits.
- &merge_and_bump - git merge -m "[ci skip] Merge branch '${RELEASE_BRANCH}' into ${MAIN_BRANCH}" --no-ff $RELEASE_BRANCH
- git merge -m "[ci skip] Merge branch '${RELEASE_BRANCH}' into ${MAIN_BRANCH}" --no-ff $RELEASE_BRANCH # Change version in pom.xml - build new pom_with_models.xml
# Change version in pom.xml - build new pom_with_models.xml - "./mvnw $MAVEN_CLI_OPTS $MAVEN_OPTS validate -DbumpPatch"
- "./mvnw $MAVEN_CLI_OPTS $MAVEN_OPTS validate -DbumpPatch" - "python3 build_pom_with_models.py"
- "python3 build_pom_with_models.py" - export SNAPSHOT_VERSION=$(./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS --batch-mode --no-transfer-progress --non-recursive help:evaluate -Dexpression=project.version | grep -v "\[.*")
- export SNAPSHOT_VERSION=$(./mvnw $MAVEN_POM_WITH_MODELS $MAVEN_CLI_OPTS $MAVEN_OPTS --batch-mode --no-transfer-progress --non-recursive help:evaluate -Dexpression=project.version | grep -v "\[.*") - echo "New version in main '$SNAPSHOT_VERSION'"
- echo "New version in main '$SNAPSHOT_VERSION'" - git add pom.xml
- git add pom.xml - git commit -m "[ci skip] Set snapshot version ${SNAPSHOT_VERSION}"
- git commit -m "[ci skip] Set snapshot version ${SNAPSHOT_VERSION}" - git push origin $MAIN_BRANCH
- git push origin $MAIN_BRANCH
tags: tags:
- vips-java - vips-java
rules: rules:
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment