Skip to content

Commit 9f269d3

Browse files
committed
chore: CI/CD 스크립트 작성
1 parent 69ae030 commit 9f269d3

10 files changed

+329
-13
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
name: yappu-world-server-lambda-cd
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Set up JDK 17
17+
uses: actions/setup-java@v4
18+
with:
19+
java-version: '17'
20+
distribution: 'corretto'
21+
cache: gradle
22+
23+
- name: Setup Gradle
24+
uses: gradle/actions/setup-gradle@v3
25+
26+
- name: Create .env file
27+
run: |
28+
mkdir -p src/main/resources
29+
echo "DISCORD_SERVER_ALERT_WEBHOOK=${{ secrets.DISCORD_SERVER_ALERT_WEBHOOK }}" > src/main/resources/.env
30+
cat src/main/resources/.env
31+
32+
- name: Build ShadowJar
33+
run: |
34+
./gradlew shadowJar
35+
36+
# Github Action 실행 서버 IP 추출
37+
- name: Get Github Actions IP
38+
id: ip
39+
uses: candidob/[email protected]
40+
41+
# AWS Credentials
42+
- name: Configure AWS Credentials
43+
uses: aws-actions/configure-aws-credentials@v4
44+
with:
45+
aws-access-key-id: ${{ secrets.AWS_BOT_ACCESS_KEY }}
46+
aws-secret-access-key: ${{ secrets.AWS_BOT_SECRET_KEY }}
47+
aws-region: ap-northeast-2
48+
49+
- name: Deploy to AWS Lambda
50+
run: |
51+
aws lambda update-function-code \
52+
--function-name sendSentryAlertsToDiscord \
53+
--zip-file fileb://build/libs/yappu-world-server-lambda-shadow.jar
54+
55+
# Discord Notification
56+
- name: CD Success Notification
57+
uses: sarisia/actions-status-discord@v1
58+
if: success()
59+
with:
60+
title: ✅ Lambda 배포 성공 ✅
61+
webhook: ${{ secrets.DISCORD_SERVER_WEBHOOK }}
62+
color: 0x00FF00
63+
username: 페페훅
64+
65+
- name: CD Failure Notification
66+
uses: sarisia/actions-status-discord@v1
67+
if: failure()
68+
with:
69+
title: ❗️Lambda 배포 실패 ❗️
70+
webhook: ${{ secrets.DISCORD_SERVER_WEBHOOK }}
71+
color: 0xFF0000
72+
username: 페페훅
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
name: yappu-world-server-lambda-ci
2+
3+
on:
4+
pull_request:
5+
branches:
6+
- main
7+
8+
jobs:
9+
build:
10+
runs-on: ubuntu-latest
11+
permissions:
12+
contents: read
13+
steps:
14+
- uses: actions/checkout@v4
15+
16+
- name: Setup JDK 17
17+
uses: actions/setup-java@v4
18+
with:
19+
java-version: '17'
20+
distribution: 'corretto'
21+
cache: gradle
22+
23+
- name: Setup Gradle
24+
uses: gradle/actions/setup-gradle@v3
25+
26+
- name: Create .env file
27+
run: |
28+
mkdir -p src/main/resources
29+
echo "DISCORD_SERVER_ALERT_WEBHOOK=${{ secrets.DISCORD_SERVER_TEST_WEBHOOK }}" > src/main/resources/.env
30+
cat src/main/resources/.env
31+
32+
- name: Compile and run test
33+
run: |
34+
./gradlew check
35+
36+
# Discord Notification
37+
- name: CI Success Notification
38+
uses: sarisia/actions-status-discord@v1
39+
if: success()
40+
with:
41+
title: ✅ Lambda CI 성공 ✅
42+
webhook: ${{ secrets.DISCORD_SERVER_WEBHOOK }}
43+
color: 0x00FF00
44+
username: 페페훅
45+
46+
- name: CI Failure Notification
47+
uses: sarisia/actions-status-discord@v1
48+
if: failure()
49+
with:
50+
title: ❗️Lambda CI 실패 ❗️
51+
webhook: ${{ secrets.DISCORD_SERVER_WEBHOOK }}
52+
color: 0xFF0000
53+
username: 페페훅

.gitignore

+1
Original file line numberDiff line numberDiff line change
@@ -359,3 +359,4 @@ gradle-app.setting
359359
# End of https://www.toptal.com/developers/gitignore/api/intellij,intellij+iml,intellij+all,kotlin,gradle,linux,macos,windows
360360

361361
src/main/resources/.env
362+
.secrets

build.gradle.kts

+14-4
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,21 @@ dependencies {
2121
implementation("io.github.cdimascio:dotenv-kotlin:6.5.0")
2222

2323
testImplementation(kotlin("test"))
24+
testImplementation("io.mockk:mockk:1.13.14")
2425
}
2526

26-
tasks.test {
27-
useJUnitPlatform()
28-
}
2927
kotlin {
3028
jvmToolchain(17)
31-
}
29+
}
30+
31+
tasks {
32+
test {
33+
useJUnitPlatform()
34+
}
35+
36+
shadowJar {
37+
archiveBaseName.set("yappu-world-server-lambda") // JAR 기본 이름
38+
archiveVersion.set("") // 버전 제거
39+
archiveClassifier.set("shadow") // 기본 bootJar와 구분
40+
}
41+
}

settings.gradle.kts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
plugins {
22
id("org.gradle.toolchains.foojay-resolver-convention") version "0.8.0"
33
}
4-
rootProject.name = "yappu-world-sentry-lambda"
4+
rootProject.name = "yappu-world-server-lambda"
55

src/main/kotlin/discord/DiscordClient.kt

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,14 +12,14 @@ class DiscordClient {
1212
suspend fun send(message: DiscordMessage): DiscordWebhookResponse {
1313
val dotenv = Dotenv.load()
1414
val response = HttpClient(CIO).use { client ->
15-
client.post(dotenv["DISCORD_WEBHOOK"]) {
15+
client.post(dotenv["DISCORD_SERVER_ALERT_WEBHOOK"]) {
1616
contentType(ContentType.Application.Json)
1717
setBody(jacksonObjectMapper().writeValueAsString(message))
1818
}
1919
}
2020

2121
return DiscordWebhookResponse(
22-
response.status,
22+
response.status.value,
2323
response.body<String>()
2424
)
2525
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
package co.discord
22

3-
import io.ktor.http.*
4-
53
data class DiscordWebhookResponse(
6-
val status: HttpStatusCode,
4+
val status: Int,
75
val response: String
86
)

src/main/kotlin/handler/SentryDiscordWebhookHandler.kt

+5-3
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ import com.amazonaws.services.lambda.runtime.events.APIGatewayProxyResponseEvent
1010
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
1111
import kotlinx.coroutines.runBlocking
1212

13-
class SentryDiscordWebhookHandler : RequestHandler<Any, APIGatewayProxyResponseEvent> {
13+
class SentryDiscordWebhookHandler(
14+
private val discordClient: DiscordClient = DiscordClient()
15+
) : RequestHandler<Any, APIGatewayProxyResponseEvent> {
1416

1517
override fun handleRequest(input: Any, context: Context): APIGatewayProxyResponseEvent {
1618
val event = parseEvent(input)
@@ -31,7 +33,7 @@ class SentryDiscordWebhookHandler : RequestHandler<Any, APIGatewayProxyResponseE
3133
)
3234
)
3335

34-
return runBlocking { DiscordClient().send(message) }
36+
return runBlocking { discordClient.send(message) }
3537
.let {
3638
APIGatewayProxyResponseEvent()
3739
.withStatusCode(200)
@@ -47,4 +49,4 @@ class SentryDiscordWebhookHandler : RequestHandler<Any, APIGatewayProxyResponseE
4749
}
4850
return data["event"] as Map<*, *>
4951
}
50-
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
package handler
2+
3+
import co.discord.DiscordClient
4+
import co.handler.SentryDiscordWebhookHandler
5+
import com.amazonaws.services.lambda.runtime.Context
6+
import com.fasterxml.jackson.module.kotlin.jacksonObjectMapper
7+
import io.mockk.mockk
8+
import org.junit.jupiter.api.Test
9+
import support.fixture.SentryDiscordWebhookHandlerFixture
10+
import kotlin.test.assertTrue
11+
12+
class SentryDiscordWebhookHandlerTest {
13+
14+
val mockContext = mockk<Context>()
15+
16+
// 실행 결과는 test-webhook channel을 확인하세요
17+
@Test
18+
fun `데이터를 파싱하여 디스코드에 메세지를 보낸다`() {
19+
val responseEvent = SentryDiscordWebhookHandler(DiscordClient()).handleRequest(
20+
SentryDiscordWebhookHandlerFixture.body,
21+
mockContext
22+
)
23+
24+
val parsedResponseBody = jacksonObjectMapper().readValue(responseEvent.body, Map::class.java)
25+
assertTrue { (parsedResponseBody["status"].toString()).startsWith("2") }
26+
}
27+
}

0 commit comments

Comments
 (0)