From e59269746078a478d29b97c8efb4bc50255280b7 Mon Sep 17 00:00:00 2001 From: ohhamma Date: Tue, 8 Oct 2024 19:04:43 +0900 Subject: [PATCH] KL-184/deploy: apply makefile --- .github/workflows/deploy.yaml | 71 ++++++++++++++++++----------------- Makefile | 2 + compose.yaml | 1 + 3 files changed, 39 insertions(+), 35 deletions(-) diff --git a/.github/workflows/deploy.yaml b/.github/workflows/deploy.yaml index e3c8fa37..02a134e5 100644 --- a/.github/workflows/deploy.yaml +++ b/.github/workflows/deploy.yaml @@ -1,4 +1,4 @@ -name: Build and Deploy using Docker Compose +name: Build and Deploy using Makefile and Docker Compose on: push: @@ -8,28 +8,36 @@ on: workflow_dispatch: jobs: - build-deploy: + build-and-deploy: runs-on: ubuntu-latest environment: dev - strategy: - matrix: - java-version: [ 17 ] - distribution: [ "zulu" ] + steps: - - name: Checkout + - name: Checkout Repository uses: actions/checkout@v4 - - name: Set up JDK - uses: actions/setup-java@v2 + - name: Set up JDK 17 + uses: actions/setup-java@v4 + with: + java-version: '17' + distribution: 'temurin' + + - name: Set up Gradle + uses: gradle/actions/setup-gradle@v3 with: - java-version: ${{ matrix.java-version }} - distribution: ${{ matrix.distribution }} + gradle-version: 8.8 - - name: Grant execute permission for gradlew - run: chmod +x ./gradlew + - name: Create .env file + run: | + touch .env + echo "${{ secrets.ENV }}" > .env + shell: bash - - name: Build with Gradle - run: ./gradlew clean build -x test + - name: Build with Makefile + run: make build + + - name: Run tests + run: make test - name: Login to Docker Hub uses: docker/login-action@v3 @@ -37,23 +45,14 @@ jobs: username: ${{ secrets.DOCKER_USERNAME }} password: ${{ secrets.DOCKER_PASSWORD }} - - name: Build and Push Docker image - uses: docker/build-push-action@v5 - with: - context: . - file: ./Dockerfile - platforms: linux/amd64/v3 - push: true - tags: ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - - - name: Copy compose.yaml to EC2 - uses: appleboy/scp-action@master - with: - host: ${{ secrets.EC2_HOST }} - username: ${{ secrets.EC2_USERNAME }} - key: ${{ secrets.EC2_KEY }} - source: "compose.yaml" - target: "~/" + - name: Build and Push Docker Image + env: + DOCKER_USERNAME: ${{ secrets.DOCKER_USERNAME }} + DOCKER_IMAGE_NAME: ${{ secrets.DOCKER_IMAGE_NAME }} + run: | + docker compose build + docker tag klkl-server:latest $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:latest + docker push $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:latest - name: Deploy to EC2 uses: appleboy/ssh-action@master @@ -62,6 +61,8 @@ jobs: username: ${{ secrets.EC2_USERNAME }} key: ${{ secrets.EC2_KEY }} script: | - sudo docker pull ${{ secrets.DOCKER_USERNAME }}/${{ secrets.DOCKER_IMAGE_NAME }}:latest - sudo docker compose -f ~/compose.yaml -p klkl-server up -d - sudo docker image prune -a -f \ No newline at end of file + export DOCKER_USERNAME=${{ secrets.DOCKER_USERNAME }} + export DOCKER_IMAGE_NAME=${{ secrets.DOCKER_IMAGE_NAME }} + docker pull $DOCKER_USERNAME/$DOCKER_IMAGE_NAME:latest + docker compose -f ~/compose.yaml -p klkl-server up -d + docker image prune -a -f \ No newline at end of file diff --git a/Makefile b/Makefile index 7f15400c..844967e4 100644 --- a/Makefile +++ b/Makefile @@ -4,6 +4,8 @@ DOCKER_VOLUME := $(shell docker volume ls -q) up: @make build + DOCKER_USERNAME=${DOCKER_USERNAME} + DOCKER_IMAGE_NAME=${DOCKER_IMAGE_NAME} docker compose up --build -d upi: diff --git a/compose.yaml b/compose.yaml index 114b47d6..a4d87048 100644 --- a/compose.yaml +++ b/compose.yaml @@ -1,6 +1,7 @@ version: "3" services: spring: + image: ${DOCKER_USERNAME}/${DOCKER_IMAGE_NAME}:latest build: . ports: - "8080:8080"