diff --git a/.github/workflows/deploy.yml b/.github/workflows/deploy.yml new file mode 100644 index 0000000..a8e9662 --- /dev/null +++ b/.github/workflows/deploy.yml @@ -0,0 +1,102 @@ +name: backend + +on: + push: + branches: + - release + pull_request: + branches: + - dev + +jobs: + build-and-test: + runs-on: ubuntu-latest + steps: + - name: Checkout code + uses: actions/checkout@v3 # 체크아웃으로 깃허브에서 코드 가져오기 + + - name: Set up JDK 17 + uses: actions/setup-java@v3 # 자바 버전 및 JDK 세팅 + with: + java-version: 17 # 17버전 + distribution: 'zulu' # Zulu 사용 + + - name: Grant execute permission for gradlew # 권한으로 인해 넣었던 걸로 기억. 테스트 필요 + run: chmod +x gradlew + + - name: add application-yml + run: | + mkdir -p ./src/main/resources + cat << 'EOF' > ./src/main/resources/application.yml + ${{ secrets.APP_YML }} + EOF + find src + + - name: Build with Gradle # 체크아웃한 코드들을 기반으로 그래들 빌드 + run: ./gradlew build -x test + + - name: Check resources + run: | + ls -R build/resources/main + + - name: where is jar? # build/libs 경로 안에 jar 파일이 존재하는지 디버깅하는 step + run: ls -R build/libs + + - name: upload jar # build 한 .jar 파일을 artifact 에 보관하여 다음 job 으로 넘겨줌 + uses: actions/upload-artifact@v4 + with: + name: jar-artifact + path: build/libs/28delivery-0.0.1-SNAPSHOT.jar + + docker_build_and_push: + needs: build-and-test + runs-on: ubuntu-latest # 여기서 아키텍쳐와 버전을 지정했기 때문에 도커 빌드할때 플랫폼 지정하지 않아도 자동으로 AMD 아키텍쳐로 이미지 파일 빌드를 수행함 + steps: + - name: Checkout code + uses: actions/checkout@v3 + + - name: Download jar # 아티팩트에 업로드한 jar 파일을 다운로드 + uses: actions/download-artifact@v4 + with: + name: jar-artifact + + - name: jar is here? + run: ls + + - name: Log in to Docker Hub + run: echo ${{ secrets.DOCKER_PASSWORD_C }} | docker login -u ${{ secrets.DOCKER_HUB_USERNAME_C }} --password-stdin + + - name: Check files + run: | + pwd + ls -al + find . -name Dockerfile + + - name: Build and push Docker images + run: | + BUILD_VERSION=$((GITHUB_RUN_NUMBER % 10)) + MAJOR_VERSION=$((GITHUB_RUN_NUMBER / 10 + 1)) + FULL_VERSION=v${MAJOR_VERSION}.${BUILD_VERSION} + echo "Docker image version: ${FULL_VERSION}" + docker build -t ${{ secrets.DOCKER_HUB_USERNAME_C }}/${{ secrets.DOCKER_HUB_REPOSITORY_C }}:${FULL_VERSION} . + docker tag ${{ secrets.DOCKER_HUB_USERNAME_C }}/${{ secrets.DOCKER_HUB_REPOSITORY_C }}:${FULL_VERSION} ${{ secrets.DOCKER_HUB_USERNAME_C }}/${{ secrets.DOCKER_HUB_REPOSITORY_C }}:latest + docker push ${{ secrets.DOCKER_HUB_USERNAME_C }}/${{ secrets.DOCKER_HUB_REPOSITORY_C }}:${FULL_VERSION} + docker push ${{ secrets.DOCKER_HUB_USERNAME_C }}/${{ secrets.DOCKER_HUB_REPOSITORY_C }}:latest + + deploy: + runs-on: ubuntu-latest + needs: docker_build_and_push + + steps: + - name: Deploy to server + uses: appleboy/ssh-action@v1.2.1 + with: + host: ${{ secrets.SERVER_HOST_C }} + username: ${{ secrets.SERVER_USERNAME }} + key: ${{ secrets.SERVER_SSH_KEY_C }} + port: 22 + script: | + sudo docker compose down + sudo docker compose pull + sudo docker compose up -d + sudo docker image prune -f \ No newline at end of file diff --git a/.gitignore b/.gitignore index cf7451b..836c99c 100644 --- a/.gitignore +++ b/.gitignore @@ -43,4 +43,9 @@ out/ ### Application Setting File ### application.properties -application.yml \ No newline at end of file +application.yml +application-deploy.yml +application-local.yml + +### Docker ### +docker-compose.yml \ No newline at end of file diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..37380d5 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,4 @@ +FROM openjdk:17.0.1-jdk-slim +VOLUME /tmp +COPY ./28delivery-0.0.1-SNAPSHOT.jar app.jar +ENTRYPOINT ["java", "-jar", "/app.jar"] \ No newline at end of file