HANA -J
GitHub Actions + CodeDeploy +EC2 배포 본문
서버배포를 하는 툴을 jenkins와 actions를 고민하다가 jenkins는 서버가 따로 한개 더있어야 한다고 해서 actions를 선택했다.
> workflow
깃허브로 코드를 푸쉬 => 깃헙 액션에서 빌드와 테스트 => 압축된 파일을 s3로 업로드, codeDeploy에 배포 요청을 보낸다 => codeDeploy는 s3로부터 빌드된파일을 전달받아서 ec2 서버에 배포 (codeDeploy는 저장기능이 없다)
> 엄청정리를 잘해주신 블로그(보고 설정부분 따라하면 성공임)
https://ms3864.tistory.com/383?category=1003779
github action으로 ec2에 자동배포하기3
https://ms3864.tistory.com/381 github action으로 ec2에 자동배포하기1 우아한테크캠프 마지막 프로젝트 때 나는 자동배포부분을 맡지 않아서 꼭 혼자서 다시 해보고 싶었다. 그리고 삽집도 많이했는데 다
ms3864.tistory.com
> 준비해야 할 목록
0. 깃헙 레포지토리
1. IAM codeDeploy 권한설정 되어 있는 EC2 인스턴스
2. IAM AmazonS3FullAccess, AWSCodeDeployAccess권한 설정된 s3 사용자 생성
3. 사용자 AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY_ID 저장
> 추가로 작성해야하는 코드
1. .github/workflows/deploy.yml
깃헙 레포지토리에서 Actions탭에 들어가면 프레임워크와 언어에 따라 디폴트로 생성해주는 거 받아오기 누르면 저절로 레포에 생성해준다. pull 받아서 vsc에서 작업
# This workflow will do a clean install of node dependencies, cache/restore them, build the source code and run tests across different versions of node
# For more information see: https://help.github.com/actions/language-and-framework-guides/using-nodejs-with-github-actions
name: deploy
# 해당 브랜치가 push 될 때 해당 액션을 실행
on:
push:
branches: [ main ]
pull_request:
branches: [ main ]
# 액션을 실행시키는 runner의 운영체제
jobs:
build:
runs-on: ubuntu-latest
strategy:
matrix:
node-version: [14.x]
# See supported Node.js release schedule at https://nodejs.org/en/about/releases/
#액션의 실행 수행내용, 단계별로 실행된다.
steps:
- name: Checkout source code.
uses: actions/checkout@v2
- name: Use Node.js ${{ matrix.node-version }}
uses: actions/setup-node@v2
with:
node-version: ${{ matrix.node-version }}
# - name: delete package-lock.json
# run: rm package-lock.json
- name: Install dependencies
run: npm install
# - name: build file
# run: npm run build
- name: create env file
working-directory: ./server
run:
touch .env
cat << EOF >> .env
${{ secrets.ENV }}
EOF
- name: zip distributions
run: zip -r together-zip.zip ./client/dist ./server ./appspec.yml ./scripts
- name: AWS configure 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: ap-northeast-2
#s3,codeDeploy 경로와 이름 알맞게 입력
- name: upload to S3
run: aws s3 cp --region ap-northeast-2 ./together-zip.zip s3://together-zip/public/
- name: deploy with AWS codeDeploy
run: aws deploy create-deployment
--application-name zip-codedeploy
--deployment-config-name CodeDeployDefault.OneAtATime
--deployment-group-name zip-codedeploy
--s3-location bucket=together-zip,bundleType=zip,key=public/together-zip.zip
2. CodeDeploy 동작을 설정하기 위해 appecspec.yml작성 (루트경로)
version: 0.0
os: linux
#source: / => 전체경로받기
#destination : 본인 ec2 디렉토리구조
files:
- source: /
destination: /home/ubuntu/build
overwrite: yes
permissions:
- object: /home/ubuntu
pattern: '**'
owner: ubuntu
group: ubuntu
#BeforeInstall, AfterInstall(codeDeploy 수명주기)에 실행될 파일 지정
hooks:
BeforeInstall:
- location: scripts/before_deploy.sh
runas: ubuntu
AfterInstall:
- location: scripts/after-deploy.sh
timeout: 180
runas: ubuntu
3. 배포용 쉘 스크립트
> BeforeInstall
#!/bin/bash
REPOSITORY=/home/ubuntu/build
sudo pm2 kill
cd $REPOSITORY
sudo rm -rf server
> AfterInstall
#!/bin/bash
REPOSITORY=/home/ubuntu/build
sudo pm2 kill
cd $REPOSITORY
cd server
sudo rm -rf node_modules
sudo npm install
sudo pm2 kill
sudo pm2 start app.js
'what I Learnd > TIL' 카테고리의 다른 글
TIL- AWS EC2 (0) | 2022.01.20 |
---|---|
오류 -Instance is not in the expected state behind the load balancer. It was expected to be registered but current state is draining with reason Target.DeregistrationInProgress. (0) | 2022.01.07 |
TIL - CI/CD (0) | 2022.01.06 |
Sequelize - left outer join (0) | 2021.12.30 |
Sequelize findAll , dataValues만 가져오기 (0) | 2021.12.23 |