Github Action - AWS EC2μ SpringBoot λ°°ν¬
2022-11-14
Git Actionsλ‘ CI/CD ꡬννκΈ° - SpringBoot νλ‘μ νΈ AWS EC2μ λ°°ν¬ (κ²°λ‘ : μ€ν¨)
Key Pair μμ±
- AWS console > EC2 λ€λΉκ²μ΄μ
> λ€νΈμν¬ λ° λ³΄μ > ν€ νμ΄ > ν€ νμ΄ μμ±

- μ΄λ¦ / μ ν / νμ / νκ·Έ λ± μ μ ν μ 보 μ
λ ₯ ν ν€νμ΄ μμ± > λ€μ΄λ‘λ

github Actions secrets λ±λ‘
github repo > Settings > Security > Secrets > Actions > New repository secret
- EC2_PEM

- Name : actions μ€ννμΌμμ νΈμΆν μ΄λ¦μΌλ‘ μμ μ€μ
- Secret : μ λ¨κ³ 2λ²μμ λ€μ΄λ‘λ λ°μ .pem νμΌμ λ΄μ©μ 볡μ¬νμ¬ μ
λ ₯
- [-----BEGIN RSA PRIVATE KEY-----] ~ μ 체
- EC2_IP & EC2_USER
- AWS console > EC2 λ€λΉκ²μ΄μ
> μΈμ€ν΄μ€ > νκ² μΈμ€ν΄μ€ μ ν > μμ
- μ°κ²° > μΈμ€ν΄μ€μ μ°κ²° νλ©΄μμ IP νμΈ

- 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
- μ μμ
μ μννκΈ° μ μ IAMμμ ν΄μΌν μΌμ΄ μμ
Configure AWS Credentials For GitHub Actions with OIDC
/end of Github Action - AWS EC2μ SpringBoot λ°°ν¬
CONTENT LISTMERRIοΌs DEVELOG
2023-02-21
2022-11-09