chore: ec2 docker 배포 #1
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: CI/CD using github actions & docker | |
on: | |
push: | |
branches: [ "main" ] # 해당 branch에 푸쉬 발생 시 업데이트 | |
permissions: | |
contents: read | |
jobs: | |
CI-CD: | |
runs-on: ubuntu-latest | |
steps: | |
- uses: actions/checkout@v3 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v2 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: set chmod | |
run: chmod +x ./gradlew | |
- name: Gradle Caching # 속도 최적화 | |
uses: actions/cache@v4 | |
with: | |
path: | | |
~/.gradle/caches | |
~/.gradle/wrapper | |
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }} | |
restore-keys: | | |
${{ runner.os }}-gradle- | |
- name: Build with Gradle | |
run: ./gradlew bootJar | |
- name: Docker build & push to prod | |
run: | | |
sudo docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }} | |
sudo docker build -f Dockerfile -t ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} . | |
sudo docker push ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} | |
- name: Deploy | |
uses: appleboy/ssh-action@master | |
id: deploy | |
with: | |
host: ${{ secrets.HOST_PROD }} # EC2 퍼블릭 IPv4 DNS | |
username: ubuntu | |
key: ${{ secrets.PRIVATE_KEY }} # EC2 private key | |
envs: GITHUB_SHA | |
script: | | |
sudo docker rm -f $(docker ps -qa) | |
sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} | |
sudo docker run --env-file .env -d -p 8080:8080 ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_REPO }} | |
# sudo docker-compose up -d | |
# sudo docker image prune -f |