Docker와 github Action을 이용해서 구현했습니다.
1. 프로젝트에 github-action workflow 작성할 yml 파일 생성
- .github/workflows 폴더 안에 위치해야 합니다.
name: CI/CD
on:
push:
branches: ["dev"] # dev 브랜치에 push 했을경우
pull_request:
types: [closed] # pull_request closed 됐을 경우
workflow_dispatch: # 수동 실행 가능
jobs:
build:
runs-on: ubuntu-latest # OS 환경
#if: github.event.pull_request.merged == true && github.event.pull_request.base.ref == 'dev'
# pull_request가 dev 브랜치에 merge 됐을 경우
steps:
- name: Checkout
uses: actions/checkout@v2 # 코드 check out
- name: Set up JDK 17
uses: actions/setup-java@v3
with:
java-version: 17 # 자바 설치
distribution: 'adopt'
- name: Set Environment
uses: microsoft/variable-substitution@v1
with:
files: ./src/main/resources/application.yml
env:
spring.datasource.username: ${{ secrets.DB_USERNAME }}
spring.datasource.url: ${{ secrets.DB_URL }}
spring.datasource.password: ${{ secrets.DB_PASSWORD }}
jwt.token.secret: ${{ secrets.JWT_TOKEN_SECRET }}
spring.mail.username: ${{ secrets.MAIL_USERNAME }}
spring.mail.password: ${{ secrets.MAIL_PASSWORD }}
oauth2.kakao.client-id: ${{ secrets.KAKAO_CLIENT_ID }}
oauth2.kakao.redirect-uri: ${{ secrets.KAKAO_REDIRECT_URI }}
oauth2.kakao.info-url: ${{ secrets.KAKAO_INFO_URL }}
oauth2.kakao.base-url: ${{ secrets.KAKAO_BASE_URL }}
spring.redis.host: ${{ secrets.REDIS_HOST }}
spring.redis.port: ${{ secrets.REDIS_PORT }}
spring.redis.password: ${{ secrets.REDIS_PASSWORD }}
coolsms.api.key: ${{ secrets.COOLSMS_API_KEY }}
coolsms.api.secret: ${{ secrets.COOLSMS_API_SECRET }}
coolsms.senderNumber: ${{ secrets.COOLSMS_SENDER_NUMBER }}
- name: Grant execute permission for gradlew
run: chmod +x ./gradlew
shell: bash # (6).권한 부여
- name: Build with Gradle
run: ./gradlew clean build -x test
shell: bash # (7).build 시작
- name: Get current time
uses: 1466587594/get-current-time@v2
id: current-time
with:
format: YYYY-MM-DDTHH-mm-ss
utcOffset: "+09:00"
- name: Show Current Time
run: echo "CurrentTime=$"
shell: bash
- name: Login to DockerHub
uses: docker/login-action@v1
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Docker build
run: |
docker build -t ${{ secrets.PROJECT_NAME }} .
docker tag ${{ secrets.PROJECT_NAME }} ${{ secrets.DOCKER_HUB_REPO }}:${GITHUB_SHA::7}
docker push ${{ secrets.DOCKER_HUB_REPO }}:${GITHUB_SHA::7}
- name: Deploy
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.EC2_SERVER_HOST }}
username: ubuntu
key: ${{ secrets.PRIVATE_KEY }}
envs: GITHUB_SHA
script: |
docker rmi $(docker images -q)
docker pull ${{ secrets.DOCKER_HUB_REPO }}:${GITHUB_SHA::7}
docker tag ${{ secrets.DOCKER_HUB_REPO }}:${GITHUB_SHA::7} ${{ secrets.PROJECT_NAME }}
docker stop ${{ secrets.PROJECT_NAME }}
docker rm ${{ secrets.PROJECT_NAME }}
docker run -d --name ${{ secrets.PROJECT_NAME }} -p 80:8080 ${{ secrets.PROJECT_NAME }}
2. Dockerfile 생성
FROM openjdk:17
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
3. 환경변수 설정
- secrets.이 붙은 변수들은 github Settings에서 Secrets and Variables의 Actions에 Repository Secrets에 모두 등록해줘야 합니다.
4. ec2에 docker 설치 및 docker container가 잘 동작하는지 확인하기
sudo apt update
sudo apt install apt-transport-https ca-certificates curl software-properties-common
curl -fsSL https://download.docker.com/linux/ubuntu/gpg | sudo apt-key add -
sudo add-apt-repository "deb [arch=amd64] https://download.docker.com/linux/ubuntu focal stable"
sudo apt update
sudo apt install docker-ce
도커 설치 확인
이제 프로젝트를 github에 pr하거나 dev에 merge하면 자동으로 docker container가 실행됩니다.