簡體   English   中英

AWS CodePipeline 將 Spring Boot 應用程序部署到 Elastic BeansTalk

[英]AWS CodePipeline deploy Spring Boot application to Elastic BeansTalk

我通過 3 個步驟為 SpringBoot Java 應用程序構建了一個簡單的 CodePipeline:

  • 源碼:從GitHub獲取源碼
  • 構建:一個ja​​r文件
  • 部署:到 AWS Elastic Beanstalk 實例

1 和 2 步驟成功通過,而 Deploy 步驟失敗。 我在 Elastic Beanstalk 日志中看到的唯一錯誤:

01_configure_application.sh] : Activity execution failed, because: Executing: /usr/bin/unzip -o -d /var/app/staging /opt/elasticbeanstalk/deploy/appsource/source_bundle
  FileMagic v0.7.1: compiled magic version [5.21] does not match with shared library magic version [5.37]
  Archive:  /opt/elasticbeanstalk/deploy/appsource/source_bundle
    inflating: /var/app/staging/microservices/my-service/target/my-service.jar  
  Unable to launch application as the source bundle does not contain either a file named application.jar or a Procfile.
  Unable to launch application as the source bundle does not contain either a file named application.jar or a Procfile. (ElasticBeanstalk::ExternalInvocationError)
caused by: Executing: /usr/bin/unzip -o -d /var/app/staging /opt/elasticbeanstalk/deploy/appsource/source_bundle
  FileMagic v0.7.1: compiled magic version [5.21] does not match with shared library magic version [5.37]
  Archive:  /opt/elasticbeanstalk/deploy/appsource/source_bundle
    inflating: /var/app/staging/microservices/my-service/target/my-service.jar  
  Unable to launch application as the source bundle does not contain either a file named application.jar or a Procfile.
  Unable to launch application as the source bundle does not contain either a file named application.jar or a Procfile. (Executor::NonZeroExitStatus)

我的構建規范:

build:
  commands:
      - mvn -P ci --settings settings.xml install -DskipTests
artifacts:
  files:
     - microservices/my-service/target/my-service.jar

如果我使用 AWS Web Interface 將此 jar 直接部署到 AWS Elastic Beanstalk,它會完美運行。

請幫我。 我已准備好按需共享任何其他配置。

雖然 Denys 的回答是正確的,但我認為它沒有清楚地解釋了這個問題。

artifacts指令下輸出時,artifact必須是頂層或最多一個深度目錄

例如使用 gradle 的 spring 項目的默認值是在執行bootJar任務后輸出到<project root>/build/libs/<artifact name>.jar

直覺上你會定義這樣的東西:

version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto8
  build:
    commands:
      # bootJar task outputs to build/libs/<name>.jar
      - ./gradlew bootJar
artifacts:
  files:
      - build/libs/<name>.jar

此構建將成功並將壓縮的工件上傳到 S3。 解壓后你會得到:

build/libs/<name>.jar 您可以通過從 s3 下載工件並自行解壓縮來確認這一點(就像彈性豆莖在 vm 中所做的那樣)。

因此,當彈性 beanstalk 嘗試部署它時,它會失敗,因為它查找頂級<name>.jar至多somedir/<name>.jar並且您會收到相當神秘的錯誤消息

無法啟動應用程序,因為源包不包含名為 application.jar 的文件或 Procfile。

它很神秘,因為它暗示工件必須命名為application.jar或必須添加 Procfile。 這些都不是真的

所以解決方案圍繞使該 jar 文件成為頂級。 你可以:

將構建工具的輸出工件定義為將其放置在頂層(不理想)

build階段添加第二個命令,將該 jar 文件移出到頂層,例如:

version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto8
  build:
    commands:
      # bootJar task outputs to build/libs/<name>.jar
      - ./gradlew bootJar
      # move the jar (by wildcard, agnostic to its name) to top level app.jar
      - mv build/libs/*.jar app.jar
artifacts:
  files:
      # publish the now top level app.jar as the artifact
      - app.jar

最合適的解決方案是使用post_build指令,該指令專為清理/重組步驟而設計:

version: 0.2

phases:
  install:
    runtime-versions:
      java: corretto8
  build:
    commands:
      # bootJar task outputs to build/libs/<name>.jar
      - ./gradlew bootJar
  post_build:
    commands:
      # move the jar (by wildcard, agnostic to its name) to top level app.jar
      - mv build/libs/*.jar app.jar
artifacts:
  files:
      # publish the now top level app.jar as the artifact
      - app.jar

因此,解壓縮后您將獲得app.jar頂級,並且彈性豆莖很高興! 請注意app.jarbuild/libs是任意的,設置它們但對您和您的項目有意義。 重要的是artifacts.files是頂級 jar 文件。 彈性豆莖將照顧其余的

如果您不config.yml CP 或有一個簡單的項目,則無需按照其他類似問題的建議使用config.yml進行覆蓋或添加Procfile

希望這對其他人有幫助。

問題出在工件的子文件夾中,不可能像在我的 Buildspec 中那樣使用工件位置:

artifacts:
  files:
     - microservices/my-service/target/my-service.jar

唯一正確的方法是folder/myapp.jar ,例如:

artifacts:
  files:
     - target/my-service.jar

所以你應該在你的 Maven 配置中指定outputDirectory如果它不同

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM