「Spring」Githubアクション+AWS CodeDeployを使用してプロジェクトを自動配置

今回Springバックエンドプロジェクトをして、私がやりたいのはMVC Patternを応用してAPIを作成するだけではありません.Github ActionとAWSを利用して、自動統合と導入が可能なCI/CDを構築します.Dockerに一緒に適用したいのですが、使ったことのないものでまた勉強の機会を得てみたいです.
総ての順序


ここで重点的に紹介する部分はname、run、shellであり、実際には私たちがプッシュするときに実行する順序と要素です.
name: cafein
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'temurin'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build


2.構築されたjarをAWS 3にアップロードする





name: cafein
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
env:
S3_BUCKET_NAME: cafein-deploy
jobs:
build:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Set up JDK 11
uses: actions/setup-java@v2
with:
java-version: '11'
distribution: 'temurin'
- name: Grant execute permission for gradlew
run: chmod +x gradlew
- name: Build with Gradle
run: ./gradlew clean build
- name: Make zip file
run: zip -r ./$GITHUB_SHA.zip .
shell: bash
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ${{ secrets.AWS_REGION }}
- name: Upload to S3
run: aws s3 cp --region ap-northeast-2 ./$GITHUB_SHA.zip s3://$S3_BUCKET_NAME/$GITHUB_SHA.zip


3.CodeDeployのインストールと設定
前に見たのはCIプロセスで、サーバに配備されているわけではありません.これで、CDをサーバに配布するプロセスを完了するには、CodeDeployを使用する必要があります.
ec 2ロールを設定する必要がありますが、実行するec 2タスク-セキュリティの変更-IAMロール-新しいロールを作成する場合は、次のロールを作成して付与します.そして再起動します.



chmod 400 key이름.pem
ssh -i "key이름.pem" ubuntu@ip주소
sudo apt update
sudo apt install ruby-full
sudo apt install wget
cd /home/ubuntu
sudo apt install awscli
aws s3 cp s3://aws-codedeploy-ap-northeast-2/latest/install . --region ap-northeast-2
chmod +x ./install
sudo ./install auto
# 이걸로 설치 확인을 할 수 있다.
sudo service codedeploy-agent status




4.スクリプトの追加
1) appspec.yml設定
version: 0.0
os: linux
files:
- source: /
destination: /home/ubuntu/action
overwrite: yes
permissions:
- object: /
pattern: "**"
owner: ubuntu
group: ubuntu
hooks:
ApplicationStart:
- location: scripts/deploy.sh
timeout: 60
runas: ubuntu
2)Github Actions ymlの追加- name: Code Deploy
run: aws deploy create-deployment --application-name [만든 어플리케이션 이름] --deployment-config-name CodeDeployDefault.AllAtOnce --deployment-group-name [생성한 배포 그룹 이름] --s3-location bucket=$S3_BUCKET_NAME,bundleType=zip,key=$GITHUB_SHA.zip
注意:Amazon LinuxがUbuntu、appspecであるかどうかによって.ymlパスとowner、group、runasがubuntuまたはec 2-userに区別されるため、ec 2環境のユーザーアカウントの管理を参照してください.5.最終確認




SpringベースのGithub ActionとAWSの連動過程において,自動配置によるShell Scriptの使用により多くのことを学びました.
Reference
この問題について(「Spring」Githubアクション+AWS CodeDeployを使用してプロジェクトを自動配置), 我々は、より多くの情報をここで見つけました https://velog.io/@guri_coding/Spring-Github-Action-AWS-CodeDeploy를-이용하여-프로젝트-자동-배포하기テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol