π μ μ² api μλ΅μΌλ‘ applyId λ°ν (#39) #13
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 CICD with Gradle | |
on: | |
push: | |
branches: [ "main" ] | |
jobs: | |
ci: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
steps: | |
- uses: actions/checkout@v4 | |
- name: Set up JDK 17 | |
uses: actions/setup-java@v4 | |
with: | |
java-version: '17' | |
distribution: 'temurin' | |
- name: Create .env file | |
run: | | |
echo "${{ secrets.ENV_FILE_CONTENTS }}" > .env | |
- name: Setup Gradle | |
uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 | |
- name: Build with Gradle Wrapper | |
run: ./gradlew build | |
# Docker μ΄λ―Έμ§ Build | |
- name: docker image build | |
run: docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/ivory-backend . | |
# DockerHub Login (push νκΈ° μν΄) | |
- name: docker login | |
uses: docker/login-action@v2 | |
with: | |
username: ${{ secrets.DOCKERHUB_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_PASSWORD }} | |
# Docker hub λ‘ push | |
- name: Docker Hub push | |
run: docker push ${{ secrets.DOCKERHUB_USERNAME }}/ivory-backend | |
cd: | |
needs: ci | |
runs-on: self-hosted | |
steps: | |
# 1. κΈ°μ‘΄μ 컨ν μ΄λλ₯Ό μ€μ§μν΅λλ€ | |
- name: Stop existing containers | |
run: sudo docker stop $(sudo docker ps -q) 2>/dev/null || true | |
# 2. κΈ°μ‘΄μ 컨ν μ΄λλ₯Ό μ κ±°ν©λλ€ | |
- name: Remove existing containers | |
run: sudo docker rm $(sudo docker ps -a -q) 2>/dev/null || true | |
# 3. κΈ°μ‘΄μ μ΄λ―Έμ§λ₯Ό μ κ±°ν©λλ€ | |
- name: Remove existing images | |
run: sudo docker rmi $(sudo docker images -q) 2>/dev/null || true | |
# 4. μ¬μ©νμ§ μλ λ³Όλ₯¨μ μ κ±°ν©λλ€ | |
- name: Remove unused volumes | |
run: sudo docker volume prune -f | |
# 5. Git μ΅μ μνλ‘ μ λ°μ΄νΈ (git pull) | |
- name: Pull latest changes | |
run: | | |
cd /home/ec2-user/2024_DANPOONG_TEAM_15_BE | |
git pull origin main | |
# 6. μ΅μ μ΄λ―Έμ§λ₯Ό 컨ν μ΄λννμ¬ μ€νμν΅λλ€ | |
- name: Run new containers with docker-compose | |
run: sudo docker-compose -f /home/ec2-user/2024_DANPOONG_TEAM_15_BE/docker-compose.prod.yml up --build -d | |