인프라

gitlab17 gitlab-runner 이용한 CI/CD 설정

소행성왕자 2024. 12. 18. 17:31

gitlab17 과 gitlab-runner 이용하여  spring boot 프로젝트를 자동배포 설정해봅시다.

branch 는 main 사용합니다.

.gitlab-ci.yml

deploy-to-develop-server:
  stage: deploy
  only:
    - develop
  script:
    - pwd
    - ssh -p '22' 'testacc@1.1.1.1' "cd html && git checkout develop && git pull origin develop && exit"
  tags:
    - deploy

deploy-to-main-server:
  stage: deploy
  only:
    - main
  script:
    - pwd
    - id
    - cd /home/coforward/data-converter/ && git checkout main && git pull origin main && ./gradlew build -x test && cp -f build/libs/relay-0.0.1-SNAPSHOT.war /home/coforward/webapps/ROOT.war
    - /opt/tomcat/bin/shutdown.sh && /opt/tomcat/bin/startup.sh

  tags:
    - deploy

디렉토리 구조

  • data-converter : gitlab 저장소 폴더
  • html : apache  DocumentRoot
  • webapps : tomcat DocumentRoot
$ cd /home/
drwxr-xr-x.  8 coforward     coforward     4096 Dec 18 16:09 coforward
drwx------. 15 eugene        eugene        4096 Dec 17 20:15 eugene
drwx------   5 gitlab-runner gitlab-runner  128 Dec 18 16:43 gitlab-runner


$ cd coforward
drwxr-xr-x   7 gitlab-runner gitlab-runner     4096 Dec 18 16:43 data-converter
drwxr-xr-x. 17 coforward     coforward         4096 Dec 18 14:09 html
drwxrwx--x   3 coforward     gitlab-runner       34 Dec 18 16:43 webapps

tomcat 설정

vi /opt/tomcat/conf/server.xml

<Host name="192.168.0.2" appBase="/home/coforward/webapps/"
          unpackWARs="true" autoDeploy="true">


  <!--
  <Host name="192.168.0.2" appBase="/home/coforward/html/webapps/"
          unpackWARs="true" autoDeploy="true">


    <Context path="/" docBase="./demo-0.0.1-SNAPSHOT.war"
          reloadable="false" allowLinking="false" />
  -->