Skip to content

Commit

Permalink
ci: #16 Github Actions를 이용한 자동 검증 및 자동 배포 추가 (#17)
Browse files Browse the repository at this point in the history
* ci: CD 스크립트 추가

* chore: JaCoCo 설정 추가

* chore: SonarCloud 설정 추가

* ci: CI 스크립트 추가

* ci: jacocoTestCoverage를 수행하도록 수정

* ci: SonarCloud 설정 추가

* ci: 서브모듈 설정 추가

* ci: 테스트를 위해 sonarcloud 설정 제거

* ci: 테스트를 위한 리소스 파일 추가

* ci: SonarCloud 설정 추가
  • Loading branch information
JJ503 authored Dec 12, 2023
1 parent c1799eb commit 55fb24a
Show file tree
Hide file tree
Showing 6 changed files with 263 additions and 1 deletion.
48 changes: 48 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: CI - JaCoCo & SonarCloud

on:
pull_request:
branches:
- main
- develop
types: [opened, reopened, synchronize]

jobs:
build:
runs-on: ubuntu-22.04
steps:
- name: checkout branch
uses: actions/checkout@v3

- name: set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'corretto'

- name: cache SonarCloud packages
uses: actions/cache@v3
with:
path: ~/.sonar/cache
key: ${{ runner.os }}-sonar
restore-keys: ${{ runner.os }}-sonar

- name: cache gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: chmod gradle
run: chmod +x ./

- name: build and analyze
run: |
./gradlew build jacocoTestCoverage sonar
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
SONAR_TOKEN: ${{ secrets.SONAR_TOKEN }}
68 changes: 68 additions & 0 deletions .github/workflows/dev-cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
name: Dev Server CD

on:
workflow_dispatch:
push:
branches:
- develop

jobs:
build:
runs-on: ubuntu-22.04
steps:
- name: checkout branch
uses: actions/checkout@v3
with:
token: ${{ secrets.GITHUB_TOKEN }}
submodules: true

- name: set up JDK 17
uses: actions/setup-java@v3
with:
java-version: '17'
distribution: 'corretto'

- name: cache gradle
uses: actions/cache@v3
with:
path: |
~/.gradle/caches
~/.gradle/wrapper
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*', '**/gradle-wrapper.properties') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: chmod gradle
run: chmod +x ./

- name: build application
run: ./gradlew bootJar

- name: scp to dev server
uses: appleboy/scp-action@master
with:
username: ubuntu
host: ${{ secrets.DEV_PUBLIC_IP }}
key: ${{ secrets.DEV_PRIVATE_KEY }}
source: "./build/libs/*.jar"
target: "/home/ubuntu"
strip_components: 2

- name: deploy application
uses: appleboy/ssh-action@master
with:
username: ubuntu
host: ${{ secrets.DEV_PUBLIC_IP }}
key: ${{ secrets.DEV_PRIVATE_KEY }}
script: |
sudo /home/ubuntu/deploy.sh
- name: send result to slack
if: always()
uses: 8398a7/action-slack@v3
with:
status: ${{ job.status }}
author_name: Github Action CD
fields: repo,message,commit,author,action,eventName,ref,workflow,job,took
env:
SLACK_WEBHOOK_URL: ${{ secrets.SLACK_WEBHOOK_URL }}
87 changes: 86 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ plugins {
id 'org.springframework.boot' version '3.1.4'
id 'io.spring.dependency-management' version '1.1.3'
id 'org.asciidoctor.jvm.convert' version '3.3.2'
id 'jacoco'
id 'org.sonarqube' version '4.2.1.3168'
}

configurations {
Expand Down Expand Up @@ -70,7 +72,7 @@ asciidoctor.doFirst {
delete file('src/main/resources/static/docs')
}

task copyDocument(type: Copy) {
tasks.register('copyDocument', Copy) {
dependsOn asciidoctor
from file("build/docs/asciidoc")
into file("src/main/resources/static/docs")
Expand All @@ -79,3 +81,86 @@ task copyDocument(type: Copy) {
build {
dependsOn copyDocument
}

jacoco {
toolVersion = '0.8.9'
}

jacocoTestReport {
reports {
html.required = true
xml.required = true
csv.required = false
}

def Qdomains = []

for(qPattern in '**/QA' .. '**/QZ'){
Qdomains.add(qPattern + '*')
}

afterEvaluate {
classDirectories.setFrom(files(classDirectories.files.collect {
fileTree(dir: it,
exclude: [
'**/*Application*',
'**/common/**',
'**/configuration/**',
] + Qdomains)
}))
}
}

jacocoTestCoverageVerification {
def Qdomains = []

for (qPattern in '*.QA'..'*.QZ') {
Qdomains.add(qPattern + '*')
}

violationRules {
rule {
element = 'METHOD'

limit {
counter = 'BRANCH'
value = 'COVEREDRATIO'
minimum = 0.70
}

limit {
counter = 'LINE'
value = 'COVEREDRATIO'
minimum = 0.70
}

excludes = [
'*.*Application*',
'*.configuration.*',
'*.common.*',
'*.exception.*',
] + Qdomains
}
}
}

tasks.register('jacocoTestCoverage', Test) {
group 'verification'
description 'Runs the unit tests with coverage'

dependsOn(':test',
':jacocoTestReport',
':jacocoTestCoverageVerification')

tasks['jacocoTestReport'].mustRunAfter(tasks['test'])
tasks['jacocoTestCoverageVerification'].mustRunAfter(tasks['jacocoTestReport'])
}

sonar {
properties {
property 'sonar.host.url', 'https://sonarcloud.io'
property 'sonar.organization', 'blooming-backend'
property 'sonar.projectKey', 'Goal-Tracker_blooming-backend'
property 'sonar.coverage.jacoco.xmlReportPaths', 'build/reports/jacoco/test/jacocoTestReport.xml'
}
}
1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.addLombokGeneratedAnnotation = true
33 changes: 33 additions & 0 deletions src/test/java/com/backend/blooming/user/domain/UserTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package com.backend.blooming.user.domain;

import org.junit.jupiter.api.DisplayNameGeneration;
import org.junit.jupiter.api.DisplayNameGenerator;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.SoftAssertions.assertSoftly;

@DisplayNameGeneration(DisplayNameGenerator.ReplaceUnderscores.class)
@SuppressWarnings("NonAsciiCharacters")
class UserTest {

@Test
void 사용자를_생성한다() {
// when
final User actual = User.builder()
.oauthId("12345")
.email("[email protected]")
.name("사용자")
.color("#12345")
.statusMessage("상태 메시지")
.build();

// then
assertSoftly(softAssertions -> {
softAssertions.assertThat(actual.getOauthId()).isEqualTo("12345");
softAssertions.assertThat(actual.getEmail()).isEqualTo("[email protected]");
softAssertions.assertThat(actual.getName()).isEqualTo("사용자");
softAssertions.assertThat(actual.getColor()).isEqualTo("#12345");
softAssertions.assertThat(actual.getStatusMessage()).isEqualTo("상태 메시지");
});
}
}
27 changes: 27 additions & 0 deletions src/test/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
spring:
datasource:
url: jdbc:h2:mem:testdb
username: sa
driver-class-name: org.h2.Driver

jpa:
hibernate:
ddl-auto: create-drop
properties:
hibernate:
format_sql: true
dialect: org.hibernate.dialect.H2Dialect
show_sql: true
use_sql_comments: true
open-in-view: false

oauth:
client:
kakao:
user-info-uri: https://kapi.kakao.com/v2/user/me

jwt:
access-token-key: testtesttesttesttesttesttesttesttesttestesttesttesttesttest
refresh-token-key: testtesttesttesttesttesttesttesttesttestesttesttesttesttest
access-token-expire-hour: 12
refresh-token-expire-hour: 1440

0 comments on commit 55fb24a

Please sign in to comment.