๐Ÿณ๐Ÿšจ๐Ÿ‹Travis CI ํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

๊น€์„ฑ์ธยท2023๋…„ 9์›” 21์ผ
0

[DevOps] ๐ŸณDocker & Kubernetes

๋ชฉ๋ก ๋ณด๊ธฐ
7/62

๐ŸšจํŠธ๋Ÿฌ๋ธ” ์ŠˆํŒ…

.travis.yml ํŒŒ์ผ์„ ์ž‘์„ฑํ–ˆ๋˜ ๋ชจ๋“  ๊ณผ์ •๋“ค์„ ํŠธ๋Ÿฌ๋ธ”์ŠˆํŒ…ํ•˜๋ฉด์„œ ํฌ์ŠคํŒ…ํ•ด๋ณด๊ฒ ๋‹ค..
๊ธด ์—ฌ์ •์ด ๋ ๋“ฏ..

์•„์ง ์ด๋ฏธ์ง€ ํŒŒ์ผ ๋ฐ ์ปดํฌ๋„ŒํŠธ ์„ค์ • ํŒŒ์ผ๋“ฑ์„ ๋ฐฐํฌ๋„ ํ•˜์ง€๋ชปํ•ด์„œ EC2์— ๋ฐฐํฌ ํ›„ ์‹คํ–‰์€ ์ผ๋‹จ ์ˆ˜๋™์œผ๋กœ ํ•˜๋Š” ๊ฒƒ์„ ๋ชฉํ‘œ๋กœ ํ•˜์˜€๋‹ค.


์ฐธ๊ณ 

๊ณต์‹ doc

Travis CI ๋นŒ๋“œ ์Šคํ…Œ์ด์ง€ : https://docs.travis-ci.com/user/build-stages/
Travis CI CodeDeploy: https://docs.travis-ci.com/user/deployment/codedeploy/
Travis CI rake aborted: https://travis-ci.community/t/no-rakefile-found-when-adding-travis-to-a-github-project/3520

์ž˜์“ฐ์‹  ๋ธ”๋กœ๊ทธ๋‹˜

https://devlog-wjdrbs96.tistory.com/317
https://devlog-wjdrbs96.tistory.com/search/%EB%AC%B4%EC%A4%91%EB%8B%A8%20%EB%B0%B0%ED%8F%AC%ED%95%98%EA%B8%B0


์›Œํฌํ”Œ๋กœ์šฐ ์ž‘์„ฑ ์‹œ์ž‘

์ฐพ์•„ ๋ดค๋˜ ๋Œ€๋ถ€๋ถ„์˜ .travis.yml ํŒŒ์ผ๋“ค์€ ๋ชจ๋‘ ํฐ ๋‹จ์œ„์˜ ํ•œ ๋ธ”๋Ÿญ์— ๋“ค์–ด๊ฐ€ ์žˆ์—ˆ์Œ...
๋ญ”๊ฐ€ travis.yml ํŒŒ์ผ ๋‚ด์—์„œ๋„ ๊ฐ ๋‹จ๊ณ„๋ณ„๋กœ ์ž‘์—…์„ ์ˆ˜ํ–‰ํ•  ์ˆ˜ ์žˆ๋Š” ๋ฐฉ๋ฒ•์ด ๋”ฐ๋กœ ์žˆ๋Š”๊ฐ€? ๊ณ ๋ฏผํ•ด์„œ GPT์—๊ฒŒ๋„ ๋ฌผ์–ด๋ณด์•˜๋‹ค.

๊ทธ๋Ÿฐ ๊ณผ์ •์—์„œ .travis.yml ํŒŒ์ผ ์•ˆ์—์„œ ์›Œํฌํ”Œ๋กœ์šฐ๋ฅผ ์ •์˜ํ•˜๋Š” ๋ฐฉ๋ฒ•์€ jobs, include, stage ๋ธ”๋Ÿญ์„ ์‚ฌ์šฉํ•˜๋Š” ๊ฒƒ์ด์˜€๋‹ค.


์ฒซ ๋ฒˆ์งธ .travis.yml

jobs:
  include:
    - stage: SpringBoot gradle build
      language: java
      jdk:
        - openjdk11
      cache:
        directories:
          - '$HOME/ .m2/repository'
          - '$HOME/ .gradle'
      before_install:
        - chmod +x gradlew
      script:
        - ./gradlew clean build
      before_deploy:
        - zip -r prac-built ./docker-compose.yml
        - mkdir -p deploy
        - mv prac-built.zip deploy/prac-built.zip

    - stage: Build Docker image
      sudo: required
      language: generic
      services:
        - docker
      script:
        - docker build -t $SPRING-REPO -f ./Dockerfile .
        - docker build -t $NGINX-REPO -f ./Dockerfile-nginx .
        - docker login -u $DOCKER_USER -p $DOCKER_PASS
        - docker push $SPRING-REPO
        - docker push $NGINX-REPO

    - stage: Deploy to EC2
      deploy:
        - provider: s3
          access_key_id: $AWS_ACCESS_KEY
          secret_access_key: $AWS_ACCESS_SECRET
          bucket: practice-build
          region: ap-northeast-2
          skip_cleanup: true
          acl: private
          local_dir: deploy
          wait-until-deployed: true

        - provider: codedeploy
          access_key_id: $AWS_ACCESS_KEY
          secret_access_key: $AWS_ACCESS_SECRET
          bucket: practice-build
          key: SpringBoot.zip
          bundle_type: zip
          application: practiceservice
          deployment_group: practiceservice-group
          region: ap-northeast-2
          wait-until-deployed: true

1-1) ํ™˜๊ฒฝ ๋ณ€์ˆ˜ ์ด์Šˆ

We were unable to parse one of your secure environment variables.
Please make sure to escape special characters such as ' ' (white space) and $ (dollar symbol) with \ (backslash) .
For example, thi$isanexample would be typed as thi\$isanexample. See https://docs.travis-ci.com/user/encryption-keys.

ํ™˜๊ฒฝ ๋ณ€์ˆ˜๋ฅผ ์ง€์ •ํ•  ๋•Œ ๋ ˆํฌ์ด๋ฆ„์„ ๋…ธ์ถœ ์‹œํ‚ค์ง€ ์•Š์œผ๋ ค๊ณ  SPRING-REPO, NGINX-REPO ์ด๋Ÿฐ์‹์œผ๋กœ ์ด๋ฆ„์„ ์คฌ์—ˆ๋‹ค.
๋‹ค๋ฅธ ํ™˜๊ฒฝ๋ณ€์ˆ˜๋Š” _(์–ธ๋”๋ฐ”)๋ฅผ ์ž˜ ์‚ฌ์šฉํ•ด๋†“๊ณ  ํ•ด๋‹น ํ™˜๊ฒฝ๋ณ€์ˆ˜์— -(ํ•˜์ดํ”ˆ)์„ ์จ๋ฒ„๋ ค์„œ Travis CI๊ฐ€ ๋™์ž‘ํ•˜๋Š” ์™€์ค‘์— ์ด๋ฅผ ์‹๋ณ„ํ•˜์ง€ ๋ชปํ•œ๊ฒƒ... -(ํ•˜์ดํ”ˆ)์„ \_(์–ธ๋”๋ฐ”)๋กœ ๋ฐ”๊ฟ”์ฃผ์—ˆ๋‹ค.


1-2) travis.yml stage ๋ณ„ ๋…๋ฆฝ๋œ VM ๊ณต๊ฐ„ ์‚ฌ์šฉ

๐Ÿšจ๋ฌธ์ œ

์ฒซ ๋ฒˆ์งธ๋กœ ์˜ฌ๋ ธ์„ ๋•Œ .jar ํŒŒ์ผ ๋นŒ๋“œ๋Š” ์„ฑ๊ณตํ–ˆ์ง€๋งŒ, ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ์™€, ํ‘ธ์‹œ์—์„œ ์˜ค๋ฅ˜๊ฐ€ ๋‚ฌ์—ˆ๋‹ค. ๊ทธ๋ž˜์„œ Travis CI ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด๋ณด๋‹ˆ

  • (๋ผ์ธ 246) 1๋ฒˆ์งธ ๋‹จ๊ณ„์—์„œ ๋ถ„๋ช…ํžˆ gradlew clean build ๋ช…๋ น์–ด๋ฅผ ํ†ตํ•ด .jarํŒŒ์ผ์„ ๋นŒ๋“œ ์„ฑ๊ณตํ–ˆ๋Š”๋ฐ ์™œ jarfile์„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๊ณ  ํ•˜๋Š”๊ฑด์ง€ ๋„๋ฌด์ง€ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•˜๋‹ค..
  • ํ˜น์‹œ๋‚˜ ์ด์ „ stage์—์„œ ๋นŒ๋“œํ•œ ์ •๋ณด๋ฅผ ๋‹ค์Œ stage ์—์„œ ์•Œ์•„ ๋ณผ ์ˆ˜ ์—†๋Š” ๊ฑฐ๋ผ๋Š” ์ƒ๊ฐ ๋ฐ–์— ๋“ค์ง€ ์•Š์•„์„œ gpt ํ•œํ…Œ ๋ฌผ์–ด๋ดค๋‹ค.

โ˜• ๊ฐ€์ •

๐Ÿ”‘ ๋ฌธ์ œ์  ํ™•์ธ

์ƒ๊ฐํ–ˆ๋˜ ๊ฐ€์ •์ด ๋งž์•˜๊ณ , ์ด์ „ ๋‹จ๊ณ„์—์„œ ์•„๋ฌด๋ฆฌ ๋นŒ๋“œ๋ฅผ ์„ฑ๊ณตํ•˜๊ณ  .jarํŒŒ์ผ์„ ๋งŒ๋“ค์–ด๋„

FROM openjdk:11
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar", "-Dspring.profiles.active=${active}", "-Duser.timezone=Asia/Seoul", "/app.jar"]

๋‹ค์Œ ๋‹จ๊ณ„์—์„œ๋Š” ๋‹ค๋ฅธ ํ™˜๊ฒฝ์—์„œ ์‹คํ–‰๋˜๊ธฐ ๋•Œ๋ฌธ์— ํ•ด๋‹น ๋„์ปคํŒŒ์ผ์ด ๋ฐ”๋ผ๋ณผ .jarํŒŒ์ผ์„ ์ฐพ์„ ์ˆ˜ ์—†๋‹ค๋Š” ๊ฒƒ...

Travis CI ๊ณต์‹ ๋ฌธ์„œ- ๋นŒ๋“œ ๋‹จ๊ณ„

๐Ÿ”ˆ ํ•ด๊ฒฐ ๋ฐฉ๋ฒ•

๊ทธ๋ž˜์„œ ํ•ด๊ฒฐํ•˜๋ ค๋ฉด, .jarํŒŒ์ผ์€ gradle์„ ํ†ตํ•ด ๋นŒ๋“œํ•˜์ž๋งˆ์ž ๊ฐ™์€ ์ž‘์—… ๋‹จ์œ„์—์„œ ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ ํ›„ ๋„์ปค ํ—ˆ๋ธŒ์— ๋ฐ”๋กœ ํ‘ธ์‹œ ํ•ด์•ผ ๊ฒ ๋‹ค๊ณ  ์ƒ๊ฐํ•ด์„œ "Docker Build Image" stage์˜ ์Šคํ”„๋ง๋ถ€ํŠธ ํ”„๋กœ์ ํŠธ๋ฅผ ๋„์ปค ๋นŒ๋“œ ํ•˜๋Š” ๋ถ€๋ถ„์„ ์ฒ˜์Œ stage๋กœ ์˜ฎ๊ฒจ์„œ ๊ฐ™์ด ๋™์ž‘ ํ•˜๋„๋ก ๋ฐ”๊ฟจ๋‹ค.

after_script:

after_script: ์•ˆ์—๋Š” ๋‹จ์ˆœ ๋ช…๋ น์–ด๋งŒ ์ž‘์„ฑํ•˜์—ฌ์•ผํ•จ.


๋‘ ๋ฒˆ์งธ .travis.yml

before_deploy:
  - zip -r prac-built ./docker-compose.yml
  - mkdir -p deploy
  - mv prac-built.zip deploy/prac-built.zip


jobs:
  include:
    - stage: SpringBoot gradle build & Build Gradle Docker Image
      language: java
      jdk:
        - openjdk11
      #cache:
      #  directories:
      #    - '$HOME/ .m2/repository'
      #    - '$HOME/ .gradle'
      before_install:
        - chmod +x gradlew
      script:
        - ./gradlew clean build
        - sudo systemctl start docker
        - docker build -t $SPRING_REPO -f ./Dockerfile .
        - echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
        - docker push $SPRING_REPO

    - stage: Build Nginx Docker image
      sudo: required
      language: generic
      services:
        - docker
      script:
        - docker build -t $NGINX_REPO -f ./Dockerfile-nginx .
        - echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
        - docker push $NGINX_REPO

    - stage: Deploy to EC2
      deploy:
        - provider: s3
          access_key_id: $AWS_ACCESS_KEY
          secret_access_key: $AWS_ACCESS_SECRET
          bucket: practice-build
          region: ap-northeast-2
          skip_cleanup: true
          acl: private
          local_dir: deploy
          wait-until-deployed: true

        - provider: codedeploy
          access_key_id: $AWS_ACCESS_KEY
          secret_access_key: $AWS_ACCESS_SECRET
          bucket: practice-build
          key: SpringBoot.zip
          bundle_type: zip
          application: practiceservice
          deployment_group: practiceservice-group
          region: ap-northeast-2
          wait-until-deployed: true

2-1) language: ๋ฏธ ์„ค์ • ์ด์Šˆ

๐Ÿšจ๋ฌธ์ œ


์ด์ œ ๋„์ปค ์ด๋ฏธ์ง€ ๋นŒ๋“œ ๋ฐ ํ‘ธ์‹œ๊นŒ์ง€๋Š” ์ž˜๋˜๋Š”๋ฐ ๋ฐฐํฌ๋ถ€๋ถ„์ด ๋ฌธ์ œ์˜€๋‹ค.. ์ด์ „์— ๋„์ปค๋ฅผ ์‚ฌ์šฉํ•˜์ง€ ์•Š๊ณ  ๋‹จ์ˆœ S3 + CodeDeploy๋ฅผ ์“ธ๋•Œ๋Š” rake aborted! ๋ผ๋Š” ์˜ค๋ฅ˜๋ฅผ ๋ณธ์ ์ด ์—†๋Š”๋ฐ ์™œ ๋‚˜๋Š”๊ฑด์ง€ ๋„๋ฌด์ง€ ๋˜ ์ดํ•ด๊ฐ€ ๋˜์ง€ ์•Š์•˜์Œ..

๐Ÿ”ˆ ํ•ด๊ฒฐ

https://travis-ci.community/t/no-rakefile-found-when-adding-travis-to-a-github-project/3520
3๋ฒˆ์งธ ์Šคํ…Œ์ด์ง€์—์„œ language: ๋ฅผ ์„ค์ •ํ•ด์ฃผ์ง€ ์•Š์•„์„œ ๋ฐœ์ƒํ•œ ๋ฌธ์ œ์˜€์Œ..
-> ํ•ด๋‹น ์Šคํ…Œ์ด์ง€์— language: generic ์„ ์ถ”๊ฐ€ํ•ด์คŒ


์„ธ ๋ฒˆ์งธ .travis.yml

branches:
  only:
    - master

services:
  - docker

jobs:
  include:
    - stage: SpringBoot gradle build & Build Gradle Docker Image
      language: java
      jdk:
        - openjdk11

      #cache:
      #  directories:
      #    - '$HOME/ .m2/repository'
      #    - '$HOME/ .gradle'
      before_install:
        - chmod +x gradlew
      script:
        - ./gradlew clean build
        - docker build -t $SPRING_REPO -f ./Dockerfile .
        - echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
        - docker push $SPRING_REPO

    - stage: Build Nginx Docker image
      sudo: required
      language: generic
      script:
        - docker build -t $NGINX_REPO -f ./Dockerfile-nginx .
        - echo $DOCKER_PASS | docker login -u $DOCKER_USER --password-stdin
        - docker push $NGINX_REPO

    - stage: Deploy Stage
      language: generic

      before_deploy:
        - zip -r prac-built ./docker-compose.yaml
        - mkdir -p deploy
        - mv prac-built.zip deploy/prac-built.zip
      deploy:
        - provider: s3
          access_key_id: $AWS_ACCESS_KEY
          secret_access_key: $AWS_ACCESS_SECRET
          bucket: practice-build
          region: ap-northeast-2
          skip_cleanup: true
          acl: private
          local_dir: deploy
          wait-until-deployed: true

        - provider: codedeploy
          access_key_id: $AWS_ACCESS_KEY
          secret_access_key: $AWS_ACCESS_SECRET
          bucket: practice-build
          key: SpringBoot.zip
          bundle_type: zip
          application: practiceservice
          deployment_group: practiceservice-group
          region: ap-northeast-2
          wait-until-deployed: true

3-1) S3 ํ‚ค ๊ฐ’์„ deploy์—์„œ ์˜ฌ๋ฐ”๋ฅด๊ฒŒ ์„ค์ •ํ•ด์•ผํ•จ.

๐Ÿšจ๋ฌธ์ œ

S3์—๋Š” ์ž˜ ์˜ฌ๋ผ๊ฐ€๋Š”๋ฐ Deploy ์—์„œ ๋ง‰ํžˆ๊ธธ๋ž˜ EC2 ์ธ์Šคํ„ด์Šค์—์„œ ๋กœ๊ทธ๋ฅผ ํ™•์ธํ•ด ๋ณด์•˜๋‹ค.

2023-09-20T08:57:25 ERROR [codedeploy-agent(2146)]: 
InstanceAgent::Plugins::CodeDeployPlugin::CommandPoller: 
Error during perform: Aws::S3::Errors::NoSuchKey - The specified key does not exist.

๐Ÿ”ˆ ํ•ด๊ฒฐ

ํ‚ค๊ฐ€ ์กด์žฌํ•˜์ง€ ์•Š์Œ..

S3 ๋ฒ„ํ‚ท์— ๋“ค์–ด๊ฐ„ ๋ฐฐํฌํŒŒ์ผ์˜ key ๊ฐ’๊ณผ travis.yml ํŒŒ์ผ์— codedeploy ๋ธ”๋ก์—์„œ์˜ key ๊ฐ’์„ ๋˜‘๊ฐ™์ด ํ•ด์ค˜์•ผํ•˜๋Š”๋ฐ ์‹ค์ˆ˜๋กœ ์ „์— ๋งŒ๋“ค์—ˆ๋˜ travis.yml ์„ ์ˆ˜์ •ํ•˜์ง€ ์•Š์•„์„œ ๋ฐœ์ƒํ–ˆ๋˜ ์˜ค๋ฅ˜์˜€๋‹ค.

        - provider: codedeploy
          access_key_id: $AWS_ACCESS_KEY
          secret_access_key: $AWS_ACCESS_SECRET
          bucket: practice-build
          key: SpringBoot.zip -> prac-build.zip
          bundle_type: zip
          application: practiceservice
          deployment_group: practiceservice-group
          region: ap-northeast-2
          wait-until-deployed: true

4-1) nginx .conf ํŒŒ์ผ ์˜คํƒ€..

๐Ÿšจ๋ฌธ์ œ

2023-09-20 18:40:40.620  INFO 1 --- [nio-8081-exec-1] o.apache.coyote.http11.Http11Processor   : Error parsing HTTP request header
 Note: further occurrences of HTTP request parsing errors will be logged at DEBUG level.

java.lang.IllegalArgumentException: The HTTP header line [x-forwarded=Proto: http] does not conform to RFC 7230. The request has been rejected.
        at org.apache.coyote.http11.Http11InputBuffer.skipLine(Http11InputBuffer.java:1074) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.coyote.http11.Http11InputBuffer.parseHeader(Http11InputBuffer.java:905) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.coyote.http11.Http11InputBuffer.parseHeaders(Http11InputBuffer.java:591) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:286) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:63) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:926) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1790) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:52) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.tomcat.util.threads.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1191) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.tomcat.util.threads.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:659) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-embed-core-9.0.79.jar!/:na]
        at java.base/java.lang.Thread.run(Thread.java:829) ~[na:na]

๐Ÿ”ˆ ํ•ด๊ฒฐ

0๊ฐœ์˜ ๋Œ“๊ธ€