diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..89bce03 --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,27 @@ +name: Docker Image CI + +on: + push: + branches: [ "ci/cd" ] + pull_request: + branches: [ "ci/cd" ] + +jobs: + + build: + + runs-on: ubuntu-latest + + steps: + - name: Build with Gradle + uses: gradle/gradle-build-action@67421db6bd0bf253fb4bd25b31ebb98943c375e1 + with: + arguments: clean build -x test + + # Docker build + - name: Docker build + run: | + docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} + docker build -t seongwonyoon/wabi . + docker tag wabi seongwonyoon/wabi:${GITHUB_SHA::7} + docker push seongwonyoon/wabi:${GITHUB_SHA::7} diff --git a/.github/workflows/gradle.yml b/.github/workflows/gradle.yml index e9c5021..77b011c 100644 --- a/.github/workflows/gradle.yml +++ b/.github/workflows/gradle.yml @@ -18,6 +18,9 @@ jobs: # 기본 체크아웃 - name: Checkout uses: actions/checkout@v3 + # 폴더 정보 확인 + - name: List the repository structure + run: ls -R # JDK 17 세팅 - name: Set up JDK 17 @@ -26,17 +29,14 @@ jobs: java-version: '17' distribution: 'temurin' - # Gradle 버전 설치 (Wrapper 없을 경우) - - name: Set up Gradle - uses: gradle/wrapper-validation-action@v2 - with: - gradle-version: '8.8' # 필요한 Gradle 버전 설정 + # Gradlew 실행 허용 + - name: Grant execute permission for gradlew + run: chmod +x ./wabi/gradlew # 환경 변수 설정 - name: Set environment values run: | - cd ./wabi/src/main/resources - touch ./env.properties + touch ./wabi/src/main/resources/env.properties echo "SPRING_DATASOURCE_URL=${{ secrets.SPRING_DATASOURCE_URL }}" >> ./env.properties echo "SPRING_DATASOURCE_USERNAME=${{ secrets.SPRING_DATASOURCE_USERNAME }}" >> ./env.properties echo "SPRING_DATASOURCE_PASSWORD=${{ secrets.SPRING_DATASOURCE_PASSWORD }}" >> ./env.properties @@ -47,4 +47,49 @@ jobs: # Gradle build (Test 제외) - name: Build with Gradle - run: ./wabi/gradlew clean build -x test # Gradle Wrapper로 빌드 + run: | + ls + cd wabi + ls + ./gradlew clean build -x test + + - name: Docker build + run: | + docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} + cd wabi + docker build -t ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest . # 이미지에 latest 태그를 추가 + docker tag ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest seongwonyoon/wabi:${GITHUB_SHA::7} # GITHUB_SHA를 이용한 커밋 태그 추가 + docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:latest # latest 버전 푸시 + docker push ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }}:${GITHUB_SHA::7} # 커밋 해시 버전 푸시 + + # Deploy + - name: Deploy + uses: appleboy/ssh-action@master + with: + host: ${{ secrets.SERVER_IP }} + username: ${{ secrets.SSH_USERNAME }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + passphrase: ${{ secrets.SSH_PASSPHRASE }} + envs: GITHUB_SHA + script: | + #!/bin/bash + ls + cd / + sudo docker login -u ${{ secrets.DOCKERHUB_USERNAME }} -p ${{ secrets.DOCKERHUB_PASSWORD }} + sudo docker pull ${{ secrets.DOCKERHUB_USERNAME }}/${{ secrets.DOCKERHUB_REPOSITORY }} + + # Create .env file in the correct directory + echo "SPRING_DATASOURCE_URL=${{ secrets.SPRING_DATASOURCE_URL }}" | sudo tee -a .env > /dev/null + echo "SPRING_DATASOURCE_USERNAME=${{ secrets.SPRING_DATASOURCE_USERNAME }}" | sudo tee -a .env > /dev/null + echo "SPRING_DATASOURCE_PASSWORD=${{ secrets.SPRING_DATASOURCE_PASSWORD }}" | sudo tee -a .env > /dev/null + echo "JWT_KEY=${{ secrets.JWT_KEY }}" | sudo tee -a .env > /dev/null + echo "JWT_EXPIRATION_HOURS=${{ secrets.JWT_EXPIRATION_HOURS }}" | sudo tee -a .env > /dev/null + echo "JWT_ISSUER=${{ secrets.JWT_ISSUER }}" | sudo tee -a .env > /dev/null + echo "DOCKERHUB_USERNAME=${{ secrets.DOCKERHUB_USERNAME }}" | sudo tee -a .env > /dev/null + echo "DOCKERHUB_REPOSITORY=${{ secrets.DOCKERHUB_REPOSITORY }}" | sudo tee -a .env > /dev/null + + sudo docker-compose down + sudo docker-compose up -d + + +