cicd : git actions 수정 #36
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: Java CI/CD with Gradle | |
on: | |
push: | |
branches: [ "cicd/#98" ] | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 21 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '21' | |
distribution: 'temurin' | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 | |
- name: Grant execute permission for gradlew | |
run: chmod +x gradlew | |
- name: Build with Gradle Wrapper | |
run: ./gradlew clean build --exclude-task test --warning-mode all | |
# DockerHub Login | |
- name: docker login | |
uses: docker/login-action@v2 | |
with: | |
username: cowboysj | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4.0.0 | |
with: | |
context: . | |
file: ./Dockerfile | |
push: true | |
build-args: | | |
DB_URL=${{ secrets.DB_URL }} \ | |
DB_USER=${{ secrets.DB_USER }} \ | |
DB_PW=${{ secrets.DB_PW }} \ | |
DB_ROOTPW=${{ secrets.DB_ROOTPW }} \ | |
JWT_SECRET=${{ secrets.JWT_SECRET }} \ | |
OAUTH_ID=${{ secrets.OAUTH_ID }} \ | |
OAUTH_PW=${{ secrets.OAUTH_PW }} \ | |
KAKAO_ID=${{ secrets.KAKAO_ID }} \ | |
KAKAO_URI=${{ secrets.KAKAO_URI }} \ | |
KAKAO_SECRET=${{ secrets.KAKAO_SECRET }} \ | |
MAIL_NAME=${{ secrets.MAIL_NAME }} \ | |
MAIL_PW=${{ secrets.MAIL_PW }} \ | |
CLAUDE_API_KEY=${{ secrets.CLAUDE_API_KEY }} \ | |
SERVER_URL=${{ secrets.SERVER_URL }} \ | |
JAVA_HOME=/usr/local/openjdk-21 \ | |
tags: cowboysj/simter:latest | |
deploy-to-ec2: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v3 | |
- name: Set up SSH | |
uses: webfactory/ssh-agent@v0.5.3 | |
with: | |
ssh-private-key: ${{ secrets.EC2_SSH_KEY }} | |
- name: Pull and run Docker on EC2 | |
run: | | |
ssh -o StrictHostKeyChecking=no ec2-user@${{ secrets.EC2_PUBLIC_IP }} << 'EOF' | |
sudo docker pull cowboysj/simter:latest | |
if sudo docker ps -a -q -f name=simter-container | grep -q .; then | |
sudo docker stop simter-container | |
sudo docker rm simter-container | |
fi | |
sudo docker run -d \ | |
-p 8080:8080 \ | |
--name simter-container \ | |
-e DB_URL=${{ secrets.DB_URL }} \ | |
-e DB_USER=${{ secrets.DB_USER }} \ | |
-e DB_PW=${{ secrets.DB_PW }} \ | |
-e DB_ROOTPW=${{ secrets.DB_ROOTPW }} \ | |
-e JWT_SECRET=${{ secrets.JWT_SECRET }} \ | |
-e OAUTH_ID=${{ secrets.OAUTH_ID }} \ | |
-e OAUTH_PW=${{ secrets.OAUTH_PW }} \ | |
-e KAKAO_ID=${{ secrets.KAKAO_ID }} \ | |
-e KAKAO_URI=${{ secrets.KAKAO_URI }} \ | |
-e KAKAO_SECRET=${{ secrets.KAKAO_SECRET }} \ | |
-e MAIL_NAME=${{ secrets.MAIL_NAME }} \ | |
-e MAIL_PW=${{ secrets.MAIL_PW }} \ | |
-e CLAUDE_API_KEY=${{ secrets.CLAUDE_API_KEY }} \ | |
-e SERVER_URL=${{ secrets.SERVER_URL }} \ | |
-e JAVA_HOME=/usr/local/openjdk-21 \ | |
cowboysj/simter:latest | |
sudo docker system prune -f | |
EOF |