Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: cd 작성 #22

Merged
merged 5 commits into from
Jul 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .github/workflows/cd.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
name: Java CI/CD Pipeline with Gradle and Docker

on:
push:
branches: [ "main" ]

permissions:
contents: read

jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Get Github Actions IP
id: ip
uses: haythem/[email protected]

- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: ap-northeast-2

- name: Add Github Actions IP to Security group
run: |
aws ec2 authorize-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32

- name: checkout
uses: actions/checkout@v3

- name: Set up JDK 21
uses: actions/setup-java@v3
with:
java-version: '21'
distribution: 'temurin'

## create application-database.yaml -> TODO: DB 연동할 때 활성화, submodule 사용
# - name: make application-database.yaml
# run: |
# ## create application-database.yaml
# cd ./src/main/resources
#
# # application-database.yaml 파일 생성
# touch ./application-database.yaml
#
# # GitHub-Actions 에서 설정한 값을 application-database.yaml 파일에 쓰기
# echo "${{ secrets.DATABASE }}" >> ./application-database.yaml
# shell: bash

- name: Build with Gradle
run: ./gradlew bootJar

- name: web docker build and push
run: |
docker login -u ${{ secrets.DOCKER_USERNAME }} -p ${{ secrets.DOCKER_PASSWORD }}
docker build -t ${{ secrets.DOCKER_REPO }}/fiesta-web .
docker push ${{ secrets.DOCKER_REPO }}/fiesta-web

## docker compose up
- name: executing remote ssh commands using password
uses: appleboy/ssh-action@master
with:
host: ${{ secrets.HOST }}
username: ubuntu
key: ${{ secrets.KEY }}
script: |
sudo docker rm -f $(docker ps -qa)
sudo docker pull ${{ secrets.DOCKER_REPO }}/fiesta-web
docker-compose up -d
docker image prune -f

- name: Remove Github Actions IP From Security Group
run: |
aws ec2 revoke-security-group-ingress --group-id ${{ secrets.AWS_SG_ID }} --protocol tcp --port 22 --cidr ${{ steps.ip.outputs.ipv4 }}/32
48 changes: 48 additions & 0 deletions .github/workflows/pr-ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
name: 'PR CI'

on:
pull_request:
branches: [ "main" ]

permissions: write-all

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

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

- name: Cache Gradle packages
uses: actions/cache@v3
with:
path: ~/.gradle/caches
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle') }}
restore-keys: ${{ runner.os }}-gradle

- name: Grant execute permission for gradlew
run: chmod +x gradlew

- name: Test with Gradle
run: ./gradlew --info test

- name: Publish Unit Test Results
uses: EnricoMi/publish-unit-test-result-action@v1
if: ${{ always() }}
with:
files: 'build/test-results/**/*.xml'

- name: Publish Test Report
uses: mikepenz/action-junit-report@v4
if: success() || failure() # always run even if the previous step fails
with:
report_paths: '**/build/test-results/test/TEST-*.xml'
4 changes: 4 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
FROM openjdk:21-jdk-slim
ARG JAR_FILE=build/libs/*.jar
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-jar","/app.jar"]
72 changes: 70 additions & 2 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
id 'java'
id 'org.springframework.boot' version '3.3.1'
id 'io.spring.dependency-management' version '1.1.5'
id 'jacoco'
}

group = 'com.odiga'
Expand Down Expand Up @@ -48,7 +49,7 @@ dependencies {
annotationProcessor "com.querydsl:querydsl-apt:${dependencyManagement.importedProperties['querydsl.version']}:jakarta"
annotationProcessor "jakarta.annotation:jakarta.annotation-api"
annotationProcessor "jakarta.persistence:jakarta.persistence-api"

// Swagger
implementation 'org.springdoc:springdoc-openapi-starter-webmvc-ui:2.0.4'

Expand All @@ -58,6 +59,7 @@ dependencies {

tasks.named('test') {
useJUnitPlatform()
finalizedBy 'jacocoTestReport'
}

// Querydsl 설정부
Expand All @@ -70,10 +72,76 @@ tasks.withType(JavaCompile) {

// java source set 에 querydsl QClass 위치 추가
sourceSets {
main.java.srcDirs += [ generated ]
main.java.srcDirs += [generated]
}

// gradle clean 시에 QClass 디렉토리 삭제
clean {
delete file(generated)
}

// jacoco
jacoco {
toolVersion '0.8.10'
}

jacocoTestReport {
reports {
xml.required.set(true)
csv.required.set(false)
html.required.set(true)
}

def Qdomains = []

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


afterEvaluate {
classDirectories.setFrom(
files(classDirectories.files.collect {
fileTree(dir: it, excludes: [
'**/*Application*',
'**/dto/**',
'**/common/**',
"**/repository/*",
'**/Sample*'
])
})
)
}

finalizedBy 'jacocoTestCoverageVerification'
}

jacocoTestCoverageVerification {
def Qdomains = []

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

violationRules {
rule {
enabled = true
element = 'CLASS'

// 모든 클래스 각각 라인 커버리지 70% 만족시 빌드 성공
limit {
counter = 'LINE'
value = 'COVEREDRATIO'
minimum = 0.70
}

excludes = [
'*.*Application',
'*.dto.*',
'*.common.*',
'*.repository.*',
'com.example.demo.Sample'
] + Qdomains
}
}
}
1 change: 1 addition & 0 deletions lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
lombok.addLombokGeneratedAnnotation = true
Loading