Spring Boot와 Ec2로 CI/CD 하는 과정의 Log를 기록.
Web Framework: Spring Boot
AWS: EC2, CodeDeploy, IAM(role)
Etc: Github actions
AmazonS3FullAccess
AWSCodeDeployFullAccess
AWSCodeDeployRole
AmazonEC2RoleforAWSCodeDeploy
AmazonS3FullAccess
AWSCodeDeployRole
{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": [
"codedeploy.ap-northeast-2.amazonaws.com",
"ec2.amazonaws.com"
]
},
"Action": "sts:AssumeRole"
}
]
}
sudo apt update
sudo apt install ruby-full
sudo apt install wget
cd /home/ubuntu
wget https://aws-codedeploy-ap-southeast-1.s3.ap-southeast-1.amazonaws.com/latest/install
chmod +x ./install
sudo ./install auto > /tmp/logfile
sudo service codedeploy-agent start
@RestController
@RequestMapping("/")
public class HelloworldController {
@RequestMapping()
@GetMapping()
public String main(){
return "Hello World";
}
}
bootJar{
archivesBaseName = 'HelloWorld'
archiveFileName = 'helloworld.jar'
archiveVersion = "0.0.1"
}
jar {
enabled = false
}
name: Deploy to Amazon EC2
on:
push:
branches:
- master
env:
S3_BUCKET_NAME: backble //S3의 버킷명
CODE_DEPLOY_APPLICATION_NAME: SpringCICD //CodeDeploy생성시 어플리케이션명
CODE_DEPLOY_DEPLOYMENT_GROUP_NAME: SpringCICD //CodeDeploy를 생성후 어플리케이션 내부의 배포그룹명
permissions:
contents: read
jobs:
deploy:
name: Deploy
runs-on: ubuntu-latest
environment: production
steps:
- name: Checkout
uses: actions/checkout@v3
- name: Set up JDK 17 //프로젝트 JDK에 맞는 버전 설정 (build.gradle 참고)
uses: actions/setup-java@v1
with:
java-version: '17' //프로젝트 JDK에 맞는 버전 설정 (build.gradle 참고)
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }} //IAM의 ACCESS KEY ID를 GITHUB SECRET KEY로 설정
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }} //GITHUB SECRET KEY로 설정
aws-region: ${{ secrets.AWS_REGION }} //GITHUB SECRET KEY중 AWS_REGION의 값을 ap-northeast-2로 설정
- name: Upload to AWS S3
run: |
aws deploy push \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--ignore-hidden-files \
--s3-location s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip \
--source .
- name: Deploy to AWS EC2 from S3
run: |
aws deploy create-deployment \
--application-name ${{ env.CODE_DEPLOY_APPLICATION_NAME }} \
--deployment-config-name CodeDeployDefault.AllAtOnce \
--deployment-group-name ${{ env.CODE_DEPLOY_DEPLOYMENT_GROUP_NAME }} \
--s3-location bucket=$S3_BUCKET_NAME,key=$GITHUB_SHA.zip,bundleType=zip
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/app
overwrite: yes
permissions:
- object: /
pattern: "**"
owner: ubuntu
group: ubuntu
hooks:
AfterInstall:
- location: scripts/stop.sh
timeout: 60
runas: ubuntu
ApplicationStart:
- location: scripts/start.sh
timeout: 60
runas: ubuntu
#!/usr/bin/env bash
PROJECT_ROOT="/home/ubuntu/app" #코드가 주입되는 경로
JAR_FILE="$PROJECT_ROOT/helloworld.jar" #build.gradle에서 설정한 파일명으로 변경
APP_LOG="$PROJECT_ROOT/application.log"
ERROR_LOG="$PROJECT_ROOT/error.log"
DEPLOY_LOG="$PROJECT_ROOT/deploy.log"
TIME_NOW=$(date +%c)
echo "$TIME_NOW > $JAR_FILE 파일 복사" >> $DEPLOY_LOG
cp $PROJECT_ROOT/build/libs/*.jar $JAR_FILE
echo "$TIME_NOW > $JAR_FILE 파일 실행" >> $DEPLOY_LOG
nohup java -jar $JAR_FILE > $APP_LOG 2> $ERROR_LOG &
CURRENT_PID=$(pgrep -f $JAR_FILE)
echo "$TIME_NOW > 실행된 프로세스 아이디 $CURRENT_PID 입니다." >> $DEPLOY_LOG
#!/usr/bin/env bash
PROJECT_ROOT="/home/ubuntu/app" #코드가 주입되는 경로
JAR_FILE="$PROJECT_ROOT/helloworld.jar" #build.gradle에서 설정한 파일명으로 변경
DEPLOY_LOG="$PROJECT_ROOT/deploy.log"
TIME_NOW=$(date +%c)
CURRENT_PID=$(pgrep -f $JAR_FILE)
if [ -z $CURRENT_PID ]; then
echo "$TIME_NOW > 현재 실행중인 애플리케이션이 없습니다" >> $DEPLOY_LOG
else
echo "$TIME_NOW > 실행중인 $CURRENT_PID 애플리케이션 종료 " >> $DEPLOY_LOG
kill -15 $CURRENT_PID
fi
https://wangkisa.tistory.com/67
https://bcp0109.tistory.com/363