Skip to content

Commit

Permalink
Merge pull request #59 from f-lab-edu/release-1.0.6
Browse files Browse the repository at this point in the history
Release 1.0.6
  • Loading branch information
koo995 authored Sep 22, 2024
2 parents 85aaa8d + e03a186 commit 6c5a069
Show file tree
Hide file tree
Showing 3 changed files with 226 additions and 0 deletions.
78 changes: 78 additions & 0 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
pipeline {
agent any
environment {
API_ACCESS_KEY = credentials('ncp-api-access-key')
API_SECRET_KEY = credentials('ncp-api-secret-key')
}

stages {
stage('Prepare') {
steps {
echo 'Preparing...'
git branch: 'main', url: 'https://github.com/f-lab-edu/nutri-diary.git'
}

post {
success {
echo 'Preparation completed successfully!'
}
failure {
echo 'Preparation failed!'
}
}
}

stage('Build') {
steps {
sh './gradlew clean build'
}

post {
success {
echo 'Build completed successfully!'
}
failure {
echo 'Build failed!'
}
}
}

stage('Upload') {
steps {
sh './script/upload.sh'
}

post {
success {
echo 'Upload completed successfully!'
}
failure {
echo 'Upload failed!'
}
}

stage('Deploy') {
steps {
sh './script/deploy.sh'
}

post {
success {
echo 'Deploy completed successfully!'
}
failure {
echo 'Deploy failed!'
}
}
}
}

post {
success {
echo 'Build, Test, and Deploy completed successfully!'
}
failure {
echo 'Build or Deploy failed!'
}
}
}
140 changes: 140 additions & 0 deletions script/deploy.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
#!/bin/bash

# Naver Cloud Platform API 기본 설정
SOURCEDEPLOY_API_URL="https://vpcsourcedeploy.apigw.ntruss.com"
PROJECT_NAME="nutridiary"

# 헤더 설정 (API 인증)
get_auth_headers() {
local method="$1"
local uri="$2"
local api_timestamp=$(perl -MTime::HiRes -e 'printf("%d\n", Time::HiRes::time()*1000)')
local signature=$(generate_signature "${method}" "${uri}" "${api_timestamp}")

# 헤더를 명시적으로 배열로 관리하여 curl에 전달
headers=(
-H "x-ncp-apigw-timestamp: ${api_timestamp}"
-H "x-ncp-iam-access-key: ${API_ACCESS_KEY}"
-H "x-ncp-apigw-signature-v2: ${signature}"
)
}

# Signature 생성 함수 (API 호출에 필요)
generate_signature() {
local method="$1"
local uri="$2"
local time_stamp="$3"
local nl=$'\\n'

SIG="${method}"' '"${uri}"${nl}
SIG+="${time_stamp}"${nl}
SIG+="${API_ACCESS_KEY}"

SIGNATURE=$(echo -n -e "${SIG}"|iconv -t utf8 |openssl dgst -sha256 -hmac ${API_SECRET_KEY} -binary|openssl enc -base64)
echo "${SIGNATURE}"
}

# 1. 프로젝트 ID 가져오기
get_project_id() {
local project_name="$1"
local uri="/api/v1/project?projectName=${project_name}"

# 헤더 준비
get_auth_headers "GET" "${uri}"

# 프로젝트 목록 가져오기
response=$(curl -s -X GET "${SOURCEDEPLOY_API_URL}${uri}" "${headers[@]}")

# 프로젝트 ID 파싱
project_id=$(echo "${response}" | jq -r '.result.projectList[0].id')

# 에러 처리: project_id가 없을 경우 에러 메시지 출력
if [[ -z "${project_id}" || "${project_id}" == "null" ]]; then
echo "Error: 프로젝트 ID를 찾을 수 없습니다."
exit 1
fi

# project_id 리턴
echo "${project_id}"
}

# 2. 스테이지 아이디 가져오기 (프로젝트 ID 필요)
get_stage_id() {
local project_id="$1"
local uri="/api/v1/project/${project_id}/stage"

# 헤더 준비
get_auth_headers "GET" "${uri}"

# 스테이지 목록 가져오기
response=$(curl -s -X GET "${SOURCEDEPLOY_API_URL}${uri}" "${headers[@]}")

# 스테이지 ID 파싱
stage_id=$(echo "${response}" | jq -r '.result.stageList[0].id')

# 에러 처리: stage_id가 없을 경우 에러 메시지 출력
if [[ -z "${stage_id}" || "${stage_id}" == "null" ]]; then
echo "Error: 스테이지 ID를 찾을 수 없습니다."
exit 1
fi

# stage_id 리턴
echo "${stage_id}"
}

# 3. 시나리오 아이디 가져오기
get_scenario_id() {
local project_id="$1"
local stage_id="$2"
local uri="/api/v1/project/${project_id}/stage/${stage_id}/scenario"

# 헤더 준비
get_auth_headers "GET" "${uri}"

# 시나리오 목록 가져오기
response=$(curl -s -X GET "${SOURCEDEPLOY_API_URL}${uri}" "${headers[@]}")

# 시나리오 ID 파싱
scenario_id=$(echo "${response}" | jq -r '.result.scenarioList[0].id')

# 에러 처리: scenario_id가 없을 경우 에러 메시지 출력
if [[ -z "${scenario_id}" || "${scenario_id}" == "null" ]]; then
echo "Error: 시나리오 ID를 찾을 수 없습니다."
exit 1
fi

# scenario_id 리턴
echo "${scenario_id}"
}

# 4. 배포 시작 요청
start_deploy() {
local project_id="$1"
local stage_id="$2"
local scenario_id="$3"
local uri="/api/v1/project/${project_id}/stage/${stage_id}/scenario/${scenario_id}/deploy"

# 헤더 준비
get_auth_headers "POST" "${uri}"

response=$(curl -s -X POST "${SOURCEDEPLOY_API_URL}${uri}" "${headers[@]}")

# 응답에서 historyId 추출
history_id=$(echo "${response}" | jq -r '.result.historyId')

# 에러 처리: history_id가 없을 경우 에러 메시지 출력
if [[ -z "${history_id}" || "${history_id}" == "null" ]]; then
echo "Error: 배포 요청에 실패했습니다."
exit 1
fi

# 배포 성공
echo "배포가 시작되었습니다. History ID: ${history_id}"
}

# 실행 흐름
PROJECT_ID=$(get_project_id "${PROJECT_NAME}")
STAGE_ID=$(get_stage_id "${PROJECT_ID}")
SCENARIO_ID=$(get_scenario_id "${PROJECT_ID}" "${STAGE_ID}")
## 배포 시작
start_deploy "${PROJECT_ID}" "${STAGE_ID}" "${SCENARIO_ID}"
8 changes: 8 additions & 0 deletions script/upload.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
#!/bin/bash
zip -r nutridiary.zip build/libs/nutridiary-0.0.1-SNAPSHOT.jar
aws configure set aws_access_key_id ${API_ACCESS_KEY}
aws configure set aws_secret_access_key ${API_SECRET_KEY}
aws --endpoint-url=https://kr.object.ncloudstorage.com s3 rm s3://nutridiary-object-storage/nutridiary.zip
aws --endpoint-url=https://kr.object.ncloudstorage.com s3 cp nutridiary.zip s3://nutridiary-object-storage/nutridiary.zip
echo 'deploy... to... object storage complete..'
echo 'start... source deploy script..'

0 comments on commit 6c5a069

Please sign in to comment.