Skip to content

๐Ÿ‘ท[CD]:GitHub Actions, Docker ๋ฐฐํฌ ๊ด€๋ จ #43

๐Ÿ‘ท[CD]:GitHub Actions, Docker ๋ฐฐํฌ ๊ด€๋ จ

๐Ÿ‘ท[CD]:GitHub Actions, Docker ๋ฐฐํฌ ๊ด€๋ จ #43

Workflow file for this run

name: Java CI with Gradle & Docker
on:
push:
branches: [ "develop", "feat/*", "cd/*", "fix/*", "hotfix/*", "refactor/*", "chore/*", "docs/*", "style/*", "test/*" ]
pull_request:
types : [ closed ]
workflow_dispatch: # ์ˆ˜๋™ ์‹คํ–‰ ๊ฐ€๋Šฅ
jobs:
build:
runs-on: ubuntu-latest
# develop ๊ธฐ๋ฐ˜ ๋ธŒ๋žœ์น˜๊ฐ€ PR์ด merge ๋์„ ๊ฒฝ์šฐ์—๋งŒ ์ง„ํ–‰
# ๊ฐœ๋ฐœ ์ดํ›„์— if ๋ฌธ ๋ถ™์ด๊ธฐ.. if๋ฌธ ์žˆ์œผ๋ฉด Status skipped ๋จ
# if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'develop'
steps:
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'temurin'
- name: Checkout
uses: actions/checkout@v3
# Spring Boot ์–ดํ”Œ๋ฆฌ์ผ€์ด์…˜ Build (1)
- name: Spring Boot Build
run: ./gradlew clean build -x test --warning-mode=all --stacktrace
# Docker ์ด๋ฏธ์ง€ Build (2)
- name: docker image build
run: docker build -t rimi3226/gamegoo .
# DockerHub Login (3)
- name: docker login
uses: docker/login-action@v2
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PW }}
# Docker Hub push (4)
- name: docker Hub push
run: docker push rimi3226/gamegoo
# GET GitHub IP (5)
- name: get GitHub IP
id: ip
uses: haythem/[email protected]
# Configure AWS Credentials (6) - AWS ์ ‘๊ทผ ๊ถŒํ•œ ์ทจ๋“(IAM)
- 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: ap-northeast-2
# Add github ip to AWS (7)
- name: Add GitHub IP to AWS
run: |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
# AWS EC2 Server Connect & Docker ๋ช…๋ น์–ด ์‹คํ–‰ (8)
- name: AWS EC2 Connection
uses: appleboy/ssh-action@master
with:
host : ec2-3-39-194-35.ap-northeast-2.compute.amazonaws.com
username : root
key : ${{ secrets.EC2_PEM_KEY }}
port : 22
# host: ${{ secrets.EC2_HOST }}
# username: ${{ secrets.EC2_USERNAME }}
# password: ${{ secrets.EC2_PASSWORD }}
# port: ${{ secrets.EC2_SSH_PORT }}
timeout: 50s
script: whoami
# sudo docker stop gamegoo
# sudo docker rm gamegoo
# sudo docker run -it -d -p 8080:8080 --name rimi3226 rimi3226/gamegoo
# REMOVE Github IP FROM security group (9)
- name: Remove IP FROM security group
run: |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32