Github Action - AWS EC2에 SpringBoot 배포

2022-11-14
πŸ“‚ tool > git

Git Actions둜 CI/CD κ΅¬ν˜„ν•˜κΈ° - SpringBoot ν”„λ‘œμ νŠΈ AWS EC2에 배포 (κ²°λ‘  : μ‹€νŒ¨)

Key Pair 생성

  1. AWS console > EC2 λ„€λΉ„κ²Œμ΄μ…˜ > λ„€νŠΈμ›Œν¬ 및 λ³΄μ•ˆ > ν‚€ νŽ˜μ–΄ > ν‚€ νŽ˜μ–΄ 생성 Untitled
  2. 이름 / μœ ν˜• / ν˜•μ‹ / νƒœκ·Έ λ“± μ μ ˆν•œ 정보 μž…λ ₯ ν›„ ν‚€νŽ˜μ–΄ 생성 > λ‹€μš΄λ‘œλ“œ image

github Actions secrets 등둝

github repo > Settings > Security > Secrets > Actions > New repository secret

  1. EC2_PEM Screenshot-1
  • Name : actions μ‹€ν–‰νŒŒμΌμ—μ„œ ν˜ΈμΆœν•  μ΄λ¦„μœΌλ‘œ μž„μ˜ μ„€μ •
  • Secret : μœ— 단계 2λ²ˆμ—μ„œ λ‹€μš΄λ‘œλ“œ 받은 .pem 파일의 λ‚΄μš©μ„ λ³΅μ‚¬ν•˜μ—¬ μž…λ ₯
    • [-----BEGIN RSA PRIVATE KEY-----] ~ 전체
  1. EC2_IP & EC2_USER
  • AWS console > EC2 λ„€λΉ„κ²Œμ΄μ…˜ > μΈμŠ€ν„΄μŠ€ > νƒ€κ²Ÿ μΈμŠ€ν„΄μŠ€ 선택 > μž‘μ—… - μ—°κ²° > μΈμŠ€ν„΄μŠ€μ— μ—°κ²° ν™”λ©΄μ—μ„œ IP 확인 image
  • 1번과 같이 secrets둜 각각 등둝

github action μ‹€ν–‰νŒŒμΌ μž‘μ„±

  • deploy.yml 파일 μž‘μ„±ν•˜μ—¬ .github/workflows ν•˜μœ„μ— μ €μž₯
  • java λͺ…λ Ήμ–΄λ‘œ jar 파일 ꡬ동 μ‹œ git actionsκ°€ μ •μƒμ μœΌλ‘œ μ’…λ£Œλ˜μ§ˆ μ•Šμ•„μ„œ 이번 방법은 기둝만 함
1name: springboot-cicd-ec2
2
3on:
4  push:
5    branches:
6      - main
7  workflow_dispatch:
8
9env:
10  AWS_ACCOUNT_NUMBER: **********
11  AWS_IAM_USER: ******
12  AWS_REGION: ap-northeast-2
13
14permissions:
15  id-token: write
16  contents: read
17
18jobs:
19  deploy:
20    runs-on: ubuntu-latest
21    steps:
22      - name: Checkout
23        uses: actions/checkout@v2
24
25      - name: Get current time
26        uses: josStorer/get-current-time@v2
27        id: current-time
28        with:
29          format: YYYYMMDDTHHmmss
30          utcOffset: "+09:00"
31
32      - name: Run scripts in EC2 server
33        uses: appleboy/ssh-action@master
34        with:
35          key: ${{ secrets.EC2_PEM }}
36          host: ${{ secrets.EC2_IP }}
37          username: ${{ secrets.EC2_USER }}
38          script: |
39            cd deploy/git-action-test-ec2
40            git pull origin main
41            chmod +x ./gradlew
42            ./gradlew bootJar
43            cd ../../
44            kill -15 `cat ./web/web.pid`
45            mv ./web/*.jar ./backup
46            mv ./deploy/git-action-test-ec2/build/libs/*.jar ./web/web.${{ steps.current-time.outputs.formattedTime }}.jar
47            java -Xms256m -jar ./web/web.${{ steps.current-time.outputs.formattedTime }}.jar

AWS configuration

/end of Github Action - AWS EC2에 SpringBoot 배포
CONTENT LISTMERRIοΌ‡s DEVELOG
wsl.exe μ‹€ν–‰λ˜μ§€ μ•ŠμŒ
2023-02-21