From 12680602c9ee42d09df62a88b8011dc934edf175 Mon Sep 17 00:00:00 2001 From: thisiswooyeol Date: Mon, 20 Nov 2023 20:45:39 +0900 Subject: [PATCH 1/5] feat: github action to deploy springboot application to ec2 server --- ...{springboot.yaml => springboot-on-pr.yaml} | 2 - .github/workflows/springboot-on-push.yaml | 48 +++++++++++++++++++ springboot/build.gradle.kts | 6 +-- 3 files changed, 51 insertions(+), 5 deletions(-) rename .github/workflows/{springboot.yaml => springboot-on-pr.yaml} (93%) create mode 100644 .github/workflows/springboot-on-push.yaml diff --git a/.github/workflows/springboot.yaml b/.github/workflows/springboot-on-pr.yaml similarity index 93% rename from .github/workflows/springboot.yaml rename to .github/workflows/springboot-on-pr.yaml index ef31a0eb..67134163 100644 --- a/.github/workflows/springboot.yaml +++ b/.github/workflows/springboot-on-pr.yaml @@ -1,8 +1,6 @@ name: Springboot CI on: - push: - branches: [ "develop", "main" ] pull_request: branches: [ "develop", "main" ] diff --git a/.github/workflows/springboot-on-push.yaml b/.github/workflows/springboot-on-push.yaml new file mode 100644 index 00000000..94f1cb73 --- /dev/null +++ b/.github/workflows/springboot-on-push.yaml @@ -0,0 +1,48 @@ +name: Springboot CI + +on: + push: + branches: [ "develop" ] + # for debug + pull_request: + branches: [ "develop" ] + +jobs: + build: + + runs-on: ubuntu-latest + defaults: + run: + shell: bash + working-directory: ./springboot + + steps: + - uses: actions/checkout@v4 + - name: set up JDK 17 + uses: actions/setup-java@v3 + with: + java-version: '17' + distribution: 'temurin' + cache: gradle + + - name: Grant execute permission for gradlew + run: chmod +x gradlew + - name: Build with Gradle + run: ./gradlew clean build + - name: Test with Gradle + run: ./gradlew test + + - name: Set up SSH + uses: webfactory/ssh-agent@v0.5.4 + with: + ssh-private-key: ${{ secrets.SPRINGBOOT_EC2_SSH_KEY }} + - name: SCP jar file to server + run: | + scp *jar ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }}:~/\ + ${{ secrets.SPRINGBOOT_TARGET_DIR }} + working-directory: ./springboot/build/libs + - name: Execute remote commands + run: | + ssh ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }}\ + "fuser -k 8080/tcp" + ssh ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }}\ diff --git a/springboot/build.gradle.kts b/springboot/build.gradle.kts index 6c174f69..02f51331 100644 --- a/springboot/build.gradle.kts +++ b/springboot/build.gradle.kts @@ -11,9 +11,9 @@ group = "com.goliath.emojihub" version = "0.0.1-SNAPSHOT" java.sourceCompatibility = JavaVersion.VERSION_17 -//java { -// sourceCompatibility = JavaVersion.VERSION_17 -//} +tasks.getByName("jar") { + enabled = false +} repositories { mavenCentral() From 21f754958d8569c0d27e319109893ff0f660a327 Mon Sep 17 00:00:00 2001 From: thisiswooyeol Date: Mon, 20 Nov 2023 20:48:36 +0900 Subject: [PATCH 2/5] fix: remove cd from execution command fix: ec2 target directory fix: fix host key verification failed <- add remote server to known hosts refactor: rename springboot action names fix: add ssh script to run springboot app & add comments --- .github/workflows/springboot-on-pr.yaml | 2 +- .github/workflows/springboot-on-push.yaml | 22 ++++++++++++++++------ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/.github/workflows/springboot-on-pr.yaml b/.github/workflows/springboot-on-pr.yaml index 67134163..737aa350 100644 --- a/.github/workflows/springboot-on-pr.yaml +++ b/.github/workflows/springboot-on-pr.yaml @@ -1,4 +1,4 @@ -name: Springboot CI +name: Springboot CI on PR on: pull_request: diff --git a/.github/workflows/springboot-on-push.yaml b/.github/workflows/springboot-on-push.yaml index 94f1cb73..f7587c21 100644 --- a/.github/workflows/springboot-on-push.yaml +++ b/.github/workflows/springboot-on-push.yaml @@ -1,4 +1,4 @@ -name: Springboot CI +name: Springboot CI on Push on: push: @@ -25,6 +25,7 @@ jobs: distribution: 'temurin' cache: gradle + # Build & Test - name: Grant execute permission for gradlew run: chmod +x gradlew - name: Build with Gradle @@ -32,17 +33,26 @@ jobs: - name: Test with Gradle run: ./gradlew test - - name: Set up SSH + # Setup SSH + - name: Set up SSH agent with private key uses: webfactory/ssh-agent@v0.5.4 with: ssh-private-key: ${{ secrets.SPRINGBOOT_EC2_SSH_KEY }} + - name: Add remote server to known hosts + run: | + mkdir -p ~/.ssh + ssh-keyscan -H ${{ secrets.SPRINGBOOT_HOST_DNS }} >> ~/.ssh/known_hosts + + # Deploy - name: SCP jar file to server run: | - scp *jar ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }}:~/\ + scp *jar ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }}:\ ${{ secrets.SPRINGBOOT_TARGET_DIR }} working-directory: ./springboot/build/libs - name: Execute remote commands run: | - ssh ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }}\ - "fuser -k 8080/tcp" - ssh ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }}\ + ssh -v ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }} \ + "fuser -k 8080/tcp || true" + ssh -v ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }} \ + "sudo nohup /usr/bin/java -jar ${{ secrets.SPRINGBOOT_TARGET_DIR }}/*.jar\ + > {{ secrets.SPRINGBOOT_TARGET_DIR }}/app.log 2>&1 &" From 3226e782087a834b751868ac0113bbae5a67febe Mon Sep 17 00:00:00 2001 From: thisiswooyeol Date: Mon, 20 Nov 2023 23:14:20 +0900 Subject: [PATCH 3/5] fix: change application running directory --- .github/workflows/springboot-on-push.yaml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/springboot-on-push.yaml b/.github/workflows/springboot-on-push.yaml index f7587c21..7636f044 100644 --- a/.github/workflows/springboot-on-push.yaml +++ b/.github/workflows/springboot-on-push.yaml @@ -52,7 +52,7 @@ jobs: - name: Execute remote commands run: | ssh -v ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }} \ - "fuser -k 8080/tcp || true" - ssh -v ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }} \ - "sudo nohup /usr/bin/java -jar ${{ secrets.SPRINGBOOT_TARGET_DIR }}/*.jar\ - > {{ secrets.SPRINGBOOT_TARGET_DIR }}/app.log 2>&1 &" + "sudo lsof -t -i tcp:8080 | sargs sudo kill -9 || true && \ + cd ${{ secrets.SPRINGBOOT_ROOT_DIR }} && pwd && \ + sudo nohup /usr/bin/java -jar ${{ secrets.SPRINGBOOT_TARGET_DIR }}/*.jar \ + > ${{ secrets.SPRINGBOOT_ROOT_DIR }}/app.log 2>&1 &" From 5636f13d96179cb2a0226518aa72e2643835fc4c Mon Sep 17 00:00:00 2001 From: Wooyeol Lee Date: Tue, 28 Nov 2023 20:21:00 +0900 Subject: [PATCH 4/5] refactor: remove duplicate gradle test fix: fix nohub continuous deployment script --- .github/workflows/android.yaml | 4 +--- .github/workflows/springboot-on-pr.yaml | 2 -- .github/workflows/springboot-on-push.yaml | 8 +++----- 3 files changed, 4 insertions(+), 10 deletions(-) diff --git a/.github/workflows/android.yaml b/.github/workflows/android.yaml index 4fccdfb8..316c79e6 100644 --- a/.github/workflows/android.yaml +++ b/.github/workflows/android.yaml @@ -29,6 +29,4 @@ jobs: - name: Add API_BASE_URL to local.properties run: echo "API_BASE_URL= \"${{ secrets.API_BASE_URL }}\"" >> local.properties - name: Build with Gradle - run: ./gradlew clean build - - name: Test with Gradle - run: ./gradlew test \ No newline at end of file + run: ./gradlew clean build \ No newline at end of file diff --git a/.github/workflows/springboot-on-pr.yaml b/.github/workflows/springboot-on-pr.yaml index 737aa350..351b6171 100644 --- a/.github/workflows/springboot-on-pr.yaml +++ b/.github/workflows/springboot-on-pr.yaml @@ -26,5 +26,3 @@ jobs: run: chmod +x gradlew - name: Build with Gradle run: ./gradlew clean build - - name: Test with Gradle - run: ./gradlew test diff --git a/.github/workflows/springboot-on-push.yaml b/.github/workflows/springboot-on-push.yaml index 7636f044..a0a5c7c9 100644 --- a/.github/workflows/springboot-on-push.yaml +++ b/.github/workflows/springboot-on-push.yaml @@ -30,8 +30,6 @@ jobs: run: chmod +x gradlew - name: Build with Gradle run: ./gradlew clean build - - name: Test with Gradle - run: ./gradlew test # Setup SSH - name: Set up SSH agent with private key @@ -52,7 +50,7 @@ jobs: - name: Execute remote commands run: | ssh -v ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }} \ - "sudo lsof -t -i tcp:8080 | sargs sudo kill -9 || true && \ - cd ${{ secrets.SPRINGBOOT_ROOT_DIR }} && pwd && \ - sudo nohup /usr/bin/java -jar ${{ secrets.SPRINGBOOT_TARGET_DIR }}/*.jar \ + "kill $(pgrep -f springboot-0.0.1-SNAPSHOT.jar) || true && \ + cd ${{ secrets.SPRINGBOOT_ROOT_DIR }} && \ + sudo nohup /usr/bin/java -jar ${{ secrets.SPRINGBOOT_TARGET_DIR }}/*.jar & \ > ${{ secrets.SPRINGBOOT_ROOT_DIR }}/app.log 2>&1 &" From 561da241d73ed9c9256527febcdfdecb2861dae3 Mon Sep 17 00:00:00 2001 From: Wooyeol Lee Date: Tue, 28 Nov 2023 20:31:11 +0900 Subject: [PATCH 5/5] fix: fix kill process as force kill fix: add test account key command for github action --- .github/workflows/springboot-on-pr.yaml | 1 + .github/workflows/springboot-on-push.yaml | 17 +++++++++++++---- android/.idea/deploymentTargetDropDown.xml | 17 ----------------- 3 files changed, 14 insertions(+), 21 deletions(-) delete mode 100644 android/.idea/deploymentTargetDropDown.xml diff --git a/.github/workflows/springboot-on-pr.yaml b/.github/workflows/springboot-on-pr.yaml index 20bd5948..253bd0ad 100644 --- a/.github/workflows/springboot-on-pr.yaml +++ b/.github/workflows/springboot-on-pr.yaml @@ -28,6 +28,7 @@ jobs: src/test/kotlin/com/goliath/emojihub/springboot/TestServiceAccountKey.json ls -al src/test/kotlin/com/goliath/emojihub/springboot/ + # Build & Test - name: Grant execute permission for gradlew run: chmod +x gradlew - name: Build with Gradle diff --git a/.github/workflows/springboot-on-push.yaml b/.github/workflows/springboot-on-push.yaml index a0a5c7c9..6ba858b3 100644 --- a/.github/workflows/springboot-on-push.yaml +++ b/.github/workflows/springboot-on-push.yaml @@ -1,4 +1,4 @@ -name: Springboot CI on Push +name: Springboot CI/CD on Push on: push: @@ -25,6 +25,12 @@ jobs: distribution: 'temurin' cache: gradle + - name: Add test account key from secrets + run: | + echo "${{ secrets.FIREBASE_TEST_ACCOUNT_KEY }}" | base64 -d > \ + src/test/kotlin/com/goliath/emojihub/springboot/TestServiceAccountKey.json + ls -al src/test/kotlin/com/goliath/emojihub/springboot/ + # Build & Test - name: Grant execute permission for gradlew run: chmod +x gradlew @@ -50,7 +56,10 @@ jobs: - name: Execute remote commands run: | ssh -v ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }} \ - "kill $(pgrep -f springboot-0.0.1-SNAPSHOT.jar) || true && \ - cd ${{ secrets.SPRINGBOOT_ROOT_DIR }} && \ - sudo nohup /usr/bin/java -jar ${{ secrets.SPRINGBOOT_TARGET_DIR }}/*.jar & \ + "echo hello" + ssh -v ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }} \ + "sudo kill -9 \$(pgrep -f springboot-0.0.1-SNAPSHOT.jar) || true" + ssh -v ${{ secrets.SPRINGBOOT_USERNAME }}@${{ secrets.SPRINGBOOT_HOST_DNS }} \ + "cd ${{ secrets.SPRINGBOOT_ROOT_DIR }} && pwd && \ + sudo nohup /usr/bin/java -jar ${{ secrets.SPRINGBOOT_TARGET_DIR }}/*.jar \ > ${{ secrets.SPRINGBOOT_ROOT_DIR }}/app.log 2>&1 &" diff --git a/android/.idea/deploymentTargetDropDown.xml b/android/.idea/deploymentTargetDropDown.xml deleted file mode 100644 index 7e0dc85b..00000000 --- a/android/.idea/deploymentTargetDropDown.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - - - - - - - - - - - - - \ No newline at end of file