创建用于 EC2 部署的 appspec.yml 文件。

huangapple go评论77阅读模式
英文:

create appspec.yml file for ec2 deployment

问题

我已经创建了一个Spring Boot应用程序,可以在AWS EC2实例上启动,它运行良好。然而,我现在想使用CodeDeploy自动化这个过程,这样我就不必使用puTTy连接到实例,手动下载新的jar文件并运行它。

我的问题是,我不知道在appspec.yml文件中应该写什么内容,以便实现以下操作:

  • 停止应用程序
  • 下载位于我的S3存储桶中.zip文件内的新的.jar文件(相当于# wget -N jarlink)
  • 运行应用程序(相当于# java -jar jarname)

我知道我需要一些脚本来实现这些操作,但是我不知道从哪里获取合适的脚本。

谢谢,
Kris

英文:

I have created a spring boot app that I can launch on AWS EC2 instance and it works fine, however, I would now like to automate the process using CodeDeploy, so I wouldn't have to use puTTy to connect to the instance, download the new jar file and run it manually.

My issue is that I don't know what to write in the appspec.yml file in order to:

  • Stop the application
  • Download the new .jar file that sits inside of .zip file in my S3 bucket (equivalent to # wget -N jarlink)
  • Run the application (equivalent to # java -jar jarname)

I am aware that I need some scripts for this, however, I don't where to get the right ones.

Cheers,
Kris

答案1

得分: 1

已解决,我的appspec.yml文件如下所示:

version: 0.0

os: linux

files:
  - source: /artifact_name.jar
    destination: /tmp

hooks:
  ApplicationStop:
    - location: stop-process.sh
      timeout: 180
      runas: root
  ApplicationStart:
    - location: start-process.sh
      timeout: 180
      runas: root

stop-process.sh:

#!/bin/bash

ps -ef | grep artifact_name.jar | grep -v grep | awk '{print $2}' | xargs kill

start-process.sh:

#!/bin/bash

java -jar /tmp/artifact_name.jar > /dev/null 2>&1 < /dev/null &

还在buildspec.yml中包括了这两个脚本artifact:

version: 0.2

phases:
  build:
    commands:
      - mvn clean install

artifacts:
  files:
    - target/artifact_name.jar
    - appspec.yml
    - scripts/start-process.sh
    - scripts/stop-process.sh
  discard-paths: yes
英文:

Figured it out, my appspec.yml file looks as follows:

version: 0.0

os: linux

files:
  - source: /artifact_name.jar
    destination: /tmp

hooks:
  ApplicationStop:
    - location: stop-process.sh
      timeout: 180
      runas: root
  ApplicationStart:
    - location: start-process.sh
      timeout: 180
      runas: root

stop-process.sh:

#!/bin/bash

ps -ef | grep artifact_name.jar | grep -v grep | awk &#39;{print $2}&#39; | xargs kill

start-process.sh:

#!/bin/bash

java -jar /tmp/artifact_name.jar &gt; /dev/null 2&gt; /dev/null &lt; /dev/null &amp;

also included both script artifacts in buildspec.yml:

version: 0.2

phases:
  build:
    commands:
      - mvn clean install

artifacts:
  files:
    - target/artifact_name.jar
    - appspec.yml
    - scripts/start-process.sh
    - scripts/stop-process.sh
  discard-paths: yes

huangapple
  • 本文由 发表于 2020年10月8日 22:27:46
  • 转载请务必保留本文链接:https://go.coder-hub.com/64264688.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定