Last active
September 13, 2023 03:52
-
-
Save jei0486/c111ed795ba274b5715608239dc31183 to your computer and use it in GitHub Desktop.
Tomcat war deploy [CI > Gitlab, CD > gitops & ArgoCD]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# ---------------------------- | |
# stages | |
# ---------------------------- | |
stages: | |
- build_source # 소스 빌드 단계 | |
- build_push_docker_image # 도커라이징 & 도커 이미지 push 단계 | |
- deploy # gitops push | |
# ---------------------------- | |
# variables | |
# ---------------------------- | |
variables: | |
# -------------- variables > docker -------------- | |
DOCKER_HOST: tcp://docker-svc.gitlab-managed-apps:2375/ | |
DOCKER_DRIVER: overlay2 | |
DOCKER_TLS_CERTDIR: "" | |
# -------------- DOCKER IMAGE -------------- | |
REGISTRY_IMAGE: $REGISTRY_URL/$CI_PROJECT_NAME # DOCKER 이미지 이름 | |
BUILD_TAG: $CI_COMMIT_REF_NAME-$CI_PROJECT_ID-$CI_PIPELINE_ID # DOCKER 이미지 tag | |
# -------------- variables > maven -------------- | |
MAVEN_OPTS: "-Dmaven.repo.local=$CI_PROJECT_DIR/.m2/repository -Dmaven.test.skip=true" # 메이븐 옵션 | |
# -------------- 추후 project ci/cd variables 로 등록 -------------- | |
GITOPS_REPO_URL: seulseul.com/demo/demo-gitops.git | |
GITOPS_REPO_PATH: demo-2/be | |
BUILD_BASE_IMAGE: maven:3.6.0-jdk-8-slim | |
DOCKER_BASE_IMAGE_DEV: tomcat:9-jdk8 | |
DOCKER_BASE_IMAGE_PRD: tomcat:9-jre8-alpine | |
#------------------------------------------------------------------- | |
# ---------------------------------------------- | |
# rules | |
# ---------------------------------------------- | |
.rules_template: | |
rules: | |
- if: $CI_COMMIT_BRANCH == "main" || $CI_COMMIT_BRANCH == "master" | |
variables: | |
DOCKER_BASE_IMAGE: $DOCKER_BASE_IMAGE_PRD | |
when: always | |
- if: $CI_COMMIT_BRANCH == "develop" || $CI_COMMIT_BRANCH == "dev" | |
variables: | |
DOCKER_BASE_IMAGE: $DOCKER_BASE_IMAGE_DEV | |
when: always | |
- if: $CI_COMMIT_BRANCH == "main" && $CI_COMMIT_TAG != null | |
variables: | |
BUILD_TAG: $CI_COMMIT_TAG | |
DOCKER_BASE_IMAGE: $DOCKER_BASE_IMAGE_PRD | |
when: always | |
- when: never | |
# ---------------------------------------------- | |
# SCRIPT - Build Source | |
# ---------------------------------------------- | |
build_source: | |
rules: | |
- !reference [ .rules_template, rules ] | |
stage: build_source | |
image: $BUILD_BASE_IMAGE | |
script: | |
- echo "------------------------------" | |
- echo "Build $CI_PROJECT_PATH" | |
- echo "------------------------------" | |
# war 파일 생성 | |
- mvn clean package $MAVEN_OPTS | |
- echo "------------------------------" | |
- echo "$CI_PROJECT_NAME successfully build" | |
- echo "------------------------------" | |
artifacts: | |
paths: | |
- "target/*.war" | |
expire_in: 5 mins | |
# ---------------------------------------------- | |
# SCRIPT - Build & Push docker image | |
# ---------------------------------------------- | |
build_push_docker_image: | |
rules: | |
- !reference [.rules_template, rules] | |
stage: build_push_docker_image | |
image: | |
name: docker/compose:latest | |
services: | |
- docker:dind | |
script: | |
- echo "------------------------------" | |
- echo "Build & Push docker image $REGISTRY_IMAGE" | |
- echo "------------------------------" | |
# docker image build & push in image registry | |
- ls -al $CI_PROJECT_DIR/target | |
- chmod +x $CI_PROJECT_DIR/script/build-image.sh | |
- sh $CI_PROJECT_DIR/script/build-image.sh | |
- chmod +x $CI_PROJECT_DIR/script/push-image.sh | |
- sh $CI_PROJECT_DIR/script/push-image.sh | |
- echo "------------------------------" | |
- echo "clean stage" | |
- echo "------------------------------" | |
- chmod +x $CI_PROJECT_DIR/script/remove-docker-image.sh | |
- sh $CI_PROJECT_DIR/script/remove-docker-image.sh | |
artifacts: | |
paths: | |
- "target/*.war" | |
# ---------------------------------------------- | |
# SCRIPT - GitOps Repository Image Change | |
# ---------------------------------------------- | |
deploy: | |
rules: | |
- !reference [ .rules_template, rules ] | |
stage: deploy | |
image: alpine:latest | |
before_script: | |
- apk add --no-cache git curl bash coreutils | |
- curl -s "https://raw.githubusercontent.com/kubernetes-sigs/kustomize/master/hack/install_kustomize.sh" | bash | |
- mv kustomize /usr/local/bin/ | |
- git remote set-url origin "https://${CI_USERNAME}:${CI_PUSH_TOKEN}@$GITOPS_REPO_URL" | |
- git config --global user.email "${GITLAB_USER_EMAIL}" | |
- git config --global user.name "${GITLAB_USER_LOGIN}" | |
script: | |
- echo "------------------------------------------------------------" | |
- echo "GitOps Repository deploy" | |
- echo "------------------------------------------------------------" | |
# Reset to remote main branch | |
- git fetch | |
- git switch main | |
- git reset --hard origin/main | |
- cd $GITOPS_REPO_PATH | |
- ls -la | |
- kustomize edit set image $REGISTRY_IMAGE:$BUILD_TAG | |
- cat kustomization.yaml | |
- git commit -am "CI Deploy to $BUILD_TAG" | |
- git push origin main |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# build-image.sh | |
export APP_FILE=target/*.war | |
# case1 env 파일로 docker-compose build | |
#docker-compose --env-file .env_docker build | |
# case2 | |
docker-compose build --build-arg DOCKER_BASE_IMAGE=${DOCKER_BASE_IMAGE} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# build-push-image.sh | |
# export PROJECT_VERSION=$(sh mvnw -q -Dexec.executable=echo -Dexec.args='${project.version}' --non-recursive exec:exec 2>/dev/null) | |
# export APP_FILE=./target/${CI_PROJECT_NAME}-${PROJECT_VERSION}.war | |
# docker-compose --env-file .env_docker build | |
# export APP_FILE=target/ROOT.war | |
echo "------------------------------" | |
echo "Build docker image ${REGISTRY_IMAGE}:${BUILD_TAG}" | |
echo "------------------------------" | |
docker login --username ${CI_REGISTRY_USER} --password ${CI_REGISTRY_PASSWORD} ${REGISTRY_URL} | |
docker build --build-arg DOCKER_BASE_IMAGE=${DOCKER_BASE_IMAGE} --build-arg BUILD_BASE_IMAGE=${BUILD_BASE_IMAGE} -t ${REGISTRY_IMAGE}:${BUILD_TAG} . | |
docker push ${REGISTRY_IMAGE}:${BUILD_TAG} | |
echo "------------------------------" | |
echo "Success Push Image ${REGISTRY_IMAGE}:${BUILD_TAG}" | |
echo "------------------------------" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
version: "3.7" | |
services: | |
portal: | |
image: ${CI_PROJECT_NAME} | |
container_name: ${CI_PROJECT_NAME} | |
build: | |
args: | |
APP_FILE: ${APP_FILE} | |
DOCKER_BASE_IMAGE: ${DOCKER_BASE_IMAGE} | |
context: . | |
dockerfile: Dockerfile |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
ARG DOCKER_BASE_IMAGE | |
#FROM $DOCKER_BASE_IMAGE | |
FROM tomcat:9-jre8-alpine | |
RUN echo $DOCKER_BASE_IMAGE | |
RUN rm -rf /usr/local/tomcat/webapps/* | |
ARG APP_FILE | |
#COPY ${APP_FILE} /usr/local/tomcat/webapps/ROOT.war | |
COPY target/ROOT.war /usr/local/tomcat/webapps/ROOT.war | |
ENV TZ=Asia/Seoul | |
EXPOSE 8080 | |
CMD ["catalina.sh","run"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# push-image.sh | |
docker login --username ${CI_REGISTRY_USER} --password ${CI_REGISTRY_PASSWORD} ${REGISTRY_URL} | |
docker tag ${CI_PROJECT_NAME} ${REGISTRY_IMAGE}:${BUILD_TAG} | |
docker push ${REGISTRY_IMAGE}:${BUILD_TAG} | |
echo "------------------------------" | |
echo "Success Push Image ${REGISTRY_IMAGE}:${BUILD_TAG}" | |
echo "------------------------------" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# remove-docker-image.sh | |
docker images -a | |
# project name docker image 삭제 | |
docker rmi $(docker image ls | grep "${CI_PROJECT_NAME}" | awk '{print $1 ":" $2}') | |
docker images -a | |
echo "------------------------------" | |
echo "Success Clean UP" | |
echo "------------------------------" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment