[SETTING] CD 파이프라인 추가 #22
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: rhkr8521 - CI | |
on: | |
push: | |
branches: [ "develop" ] | |
pull_request: | |
branches: [ "develop" ] | |
permissions: | |
contents: read | |
jobs: | |
# Spring Boot application build and Docker image push process | |
build-docker-image: | |
runs-on: ubuntu-22.04 | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v3 | |
# 1. Set up JDK 17 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'corretto' | |
# 2. Create application.yml file | |
- name: Create application.yml | |
run: | | |
mkdir -p ./ChargeControlBackend/src/main/resources | |
echo "${{ secrets.APPLICATION_YML }}" > ./ChargeControlBackend/src/main/resources/application.yml | |
cat ./ChargeControlBackend/src/main/resources/application.yml | |
shell: bash | |
# 3. Build Spring Boot application | |
- name: Build with Gradle | |
run: | | |
chmod +x ./ChargeControlBackend/gradlew | |
./ChargeControlBackend/gradlew clean bootJar --project-dir ./ChargeControlBackend | |
shell: bash | |
# 4. Build Docker image | |
- name: Docker image build | |
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/chargecontrol_backend ./ChargeControlBackend | |
# 5. DockerHub login | |
- name: Docker login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
# 6. Push Docker image to DockerHub | |
- name: Docker Hub push | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/chargecontrol_backend | |
# 위 과정에서 푸시한 이미지를 ec2에서 풀받아서 실행시키는 과정 | |
run-docker-image-on-ec2: | |
# build-docker-image (위)과정이 완료되어야 실행됩니다. | |
needs: build-docker-image | |
runs-on: self-hosted | |
steps: | |
# 1. 최신 이미지를 풀받습니다 | |
- name: docker pull | |
run: sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/chargecontrol_backend | |
# 2. 기존의 컨테이너를 중지시킵니다 | |
- name: docker stop container | |
run: sudo docker stop $(sudo docker ps -q) 2>/dev/null || true | |
# 3. 최신 이미지를 컨테이너화하여 실행시킵니다 | |
- name: docker run new container | |
run: sudo docker run --name chargecontrol_backend --rm -d -p 8080:8080 ${{ secrets.DOCKERHUB_USERNAME }}/chargecontrol_backend | |
# 4. 미사용 이미지를 정리합니다 | |
- name: delete old docker image | |
run: sudo docker system prune -f |