티스토리 뷰

계속 해봐야지 해봐야지 미루고 미루던 CI/CD를 해보려 한다! 🏃‍♂️

 

0-1. CI란 무엇인가?

Continuous Integration으로 직역하면 지속적인 통합이다.

다시말해 여러 개발자가 작성하거나 수정한 코드를 지속적으로 통합하고 테스트하는 것을 말한다.

 

0-2. CD란 무엇인가?

Continuous Delivery/Deployment으로 직역하면 지속적인 배포이다.

다시말해 개발, 통합, 배포, 릴리즈, 테스트를 자동화하여 지속적으로 배포하는 것을 말한다.

 

우리는 gitlab master branch에 push하면 자동으로 AWS EC2에 빌드&배포 까지 할예정이다.

 

gitlab에 프로젝트 만들기와 AWS의 EC2 인스턴스 만들기는 미리 해두었다.

 

1. gitlab-runner 설치 & 등록

curl -L https://packages.gitlab.com/install/repositories/runner/gitlab-runner/script.deb.sh | sudo bash
sudo apt-get install -y gitlab-runner
sudo gitlab-runner register
// gitlab웹 > 해당 레퍼지토리 > 설정 > CI/CD > Runners > Specific Runners > Set up a specific Runner manually > URL 정보 복사 후 붙여넣기
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.com/

// gitlab웹 > 해당 레퍼지토리 > 설정 > CI/CD > Runners > Specific Runners > Set up a specific Runner manually > Token 정보 복사 후 붙여넣기
Please enter the gitlab-ci token for this runner:
zzkFSL5wUN9T-tMfDz2e

Please enter the gitlab-ci description for this runner:
[ubuntu]: Test CI/CD

Please enter the gitlab-ci tags for this runner (comma separated):
build-server

Please enter the executor: ssh, virtualbox, docker-ssh+machine, kubernetes, docker, docker-ssh, parallels, shell, docker+machine:
shell

gitlab웹 > 해당 레퍼지토리 > 설정 > CI/CD > Runners > Runners activated for this project 에 들어가 실행중인 Runner가 있는지 확인해보기

 

2. gitlab CI/CD 코드 작성

프로젝트/.gitlab-ci.yml 파일을 만든다.

image: java:8-jdk

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy

cache:
    paths:
        - .gradle/wrapper
        - .gradle/caches

build:
    stage: build
    script: ./gradlew clean build
    artifacts:
        paths:
            - build/libs/*.jar
        expire_in: 1 week
    only:
        - main

test: 
    stage: test
    script: ./gradlew test

deploy-to-ec2:
    stage: deploy
    before_script:
        - 'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )'
        - eval $(ssh-agent -s)
        - echo "$SSH_PRIVATE_KEY" | tr -d '\r' | ssh-add - > /dev/null
        - mkdir -p ~/.ssh
        - chmod 700 ~/.ssh
        - echo "$SSH_KNOWN_HOSTS" >> ~/.ssh/known_hosts
        - chmod 644 ~/.ssh/known_hosts

    script:
        - ssh -o StrictHostKeyChecking=no ubuntu@"$DEPLOY_SERVER" 'rm -f ~/test-springboot-server/build/libs/*.jar'
        - ssh -o StrictHostKeyChecking=no ubuntu@"$DEPLOY_SERVER" 'mkdir -p ~/test-springboot-server/build/libs'
        - scp build/libs/*.jar ubuntu@"$DEPLOY_SERVER":~/test-springboot-server/build/libs/*.jar
        - ssh -o StrictHostKeyChecking=no ubuntu@"$DEPLOY_SERVER" '~/test-springboot-server/test-cd.sh'
    only:
        - main

 

3. gitlab 변수 만들기

gitlab 웹페이지에서 해당 프로젝트의 설정>CI/CD>Variables에 들어가 변수를 add한다.

- DEPLOY_SERVER : 배포할 서버의 IP주소를 넣어준다.

- SSH_PRIVATE_KEY : 서버의 pem키 값을 넣어준다.

 

4. sh 파일 만들기

sh 파일이란? shell script에 실행해야하는 명령어들이 많다면 미리 파일로 명령어들을 리스트업 해두고 해당 파일을 실행시키면 써둔 명령어들이 순차적으로 실행되게 된다.

 

sudo vim test-cd.sh 명령어로 sh 파일을 만든다.

REPOSITORY=/home/ubuntu/test-springboot-server/build/libs
cd $REPOSITORY/

echo "> 현재 구동중인 애플리케이션 pid 확인"

CURRENT_PID=$(pgrep -f demo)

echo "$CURRENT_PID"

if [ -z $CURRENT_PID ]; then
        echo "> 현재 구동중인 애플리케이션이 없음"
else
        echo "> kill -9 $CURRENT_PID"
        kill -9 $CURRENT_PID
        sleep 5
fi

echo "> 새 애플리케이션 배포"

JAR_NAME=$(ls $REPOSITORY/ grep 'demo' | tail -n 1)

echo "> JAR_NAME=$JAR_NAME"

nohup java -jar $REPOSITORY/$JAR_NAME &> dev/null &

 

sudo chmod 755 test-cd.sh 명령어로 gitlab에서 사용할 수 있도록 sh파일의 권한을 변경해준다.

 

sudo git update-index --chmod=+x gradlew 명령어로 gradlew 권한을 변경시켜준다.

 

 

[출처🙏]

https://sg-choi.tistory.com/552

 

[Gitlab] CI/CD Pipeline 사용하기

기본 설정 설명 Gitlab CI/CD Pipeline은 Repository에 Push/Merge와 같은 이벤트가 발생했을 경우 테스트/빌드/배포와 같은 작업을 자동으로 수행해주는 것을 말한다. gitlab-runner 설치 및 등록 과정은 원격서

sg-choi.tistory.com

https://lurutia.tistory.com/803

 

gitlab spring boot ci/cd EC2에 자동배포

목표 gitlab의 CI/CD기능을 이용해서 master branch에 push하게 되면 AWS EC2에 spring boot jar를 빌드 및 배포 까지 완료하는것입니다. 시작하기 gitlab ci/cd 코드 작성 최대한 민감한 정보는 환경변수로 설정..

lurutia.tistory.com

 

댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
Total
Today
Yesterday
링크
«   2025/02   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28
글 보관함