[Setting] Dockerfile ์์ #24
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
# github repository actions ํ์ด์ง์ ๋ํ๋ ์ด๋ฆ | |
name: CI/CD using github actions & docker | |
# event trigger | |
# main์ด๋ develop ๋ธ๋์น์ push๊ฐ ๋์์ ๋ ์คํ | |
on: | |
push: | |
branches: [ "main" ] | |
permissions: | |
contents: read | |
jobs: | |
CI-CD: | |
runs-on: ubuntu-latest | |
steps: | |
# JDK setting - github actions์์ ์ฌ์ฉํ JDK ์ค์ (ํ๋ก์ ํธ๋ AWS์ java ๋ฒ์ ๊ณผ ๋ฌ๋ผ๋ ๋ฌด๋ฐฉ) | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v3 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
# gradle caching - ๋น๋ ์๊ฐ ํฅ์ | |
- name: Gradle Caching | |
uses: actions/cache@v3 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
# ํ๊ฒฝ๋ณ yml ํ์ผ ์์ฑ - application.yml | |
- name: make application.yml | |
if: | | |
contains(github.ref, 'main') | |
run: | | |
mkdir ./planfit/src/main/resources # resources ํด๋ ์์ฑ | |
cd ./planfit/src/main/resources # resources ํด๋๋ก ์ด๋ | |
touch ./application.yml # application.yml ์์ฑ | |
echo "${{ secrets.YML }}" > ./application.yml # github actions์์ ์ค์ ํ ๊ฐ์ application.yml ํ์ผ์ ์ฐ๊ธฐ | |
shell: bash | |
# gradle build | |
- name: Build with Gradle | |
run: | | |
./planfit/gradlew build --project-dir ./planfit -x test | |
# docker login | |
- name: Docker login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
# docker build & push to production | |
- name: Docker build & push to prod | |
if: contains(github.ref, 'main') | |
run: | | |
pwd | |
docker build -f ./planfit/Dockerfile -t ${{ secrets.DOCKERHUB_USERNAME }}/planfit . | |
docker push ${{ secrets.DOCKERHUB_USERNAME }}/planfit | |
## deploy to production | |
- name: Deploy to prod | |
uses: appleboy/ssh-action@master | |
id: deploy-prod | |
if: contains(github.ref, 'main') | |
with: | |
host: ${{ secrets.HOST_PROD }} # EC2 ํผ๋ธ๋ฆญ IPv4 DNS | |
username: ubuntu | |
key: ${{ secrets.PRIVATE_KEY }} | |
envs: GITHUB_SHA | |
script: | | |
sudo docker ps | |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/plnafit | |
sudo docker run -d -p 8081:8081 ${{ secrets.DOCKER_USERNAME }}/planfit | |
sudo docker image prune -f |