Skip to content

Commit

Permalink
chore: 部署体验优化 TencentBlueKing#4193
Browse files Browse the repository at this point in the history
backport部署脚本到release-1.3
  • Loading branch information
vuuv committed Jun 7, 2021
1 parent 581a30f commit 1c052e5
Show file tree
Hide file tree
Showing 6 changed files with 166 additions and 73 deletions.
74 changes: 17 additions & 57 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -78,71 +78,32 @@ jobs:
key: ${{ runner.os }}-gradle-${{ hashFiles('**/*.gradle*') }}
restore-keys: |
${{ runner.os }}-gradle-
- name: Gradle Build
uses: eskatos/gradle-command-action@v1
with:
gradle-version: 4.8.1
build-root-directory: src/backend/ci/core
arguments: clean test build -DmavenRepoUrl="http://maven.aliyun.com/nexus/content/groups/public/"

- name: Gradle Build Backend Service
working-directory: src/backend/ci
run: ./gradlew clean test build :core:worker:worker-agent:shadowJar -DmavenRepoUrl="https://maven.aliyun.com/nexus/content/groups/public/"

- name: Create artifact - Step1:init
run: |
mkdir -p bkci/frontend
mkdir -p bkci/gateway
mkdir -p bkci/agent-package
mkdir -p bkci/agent-package/script/linux
mkdir -p bkci/agent-package/upgrade
mkdir -p bkci/agent-package/jar
- name: Create artifact - Step2:Microservices
run: |
cp -rf support-files/agent-package/script/* bkci/agent-package/script/
cp -r src/gateway/* bkci/gateway/
cd src/backend/ci/release/
mv -v worker-agent.jar $GITHUB_WORKSPACE/support-files/agent-package/jar/
bootlist=`ls boot-*.jar`
for bootfile in $bootlist
do
servicename=`echo $bootfile | sed 's/boot-\(.*\).jar/\1/g'`
mkdir -p $GITHUB_WORKSPACE/bkci/$servicename/
cp -v boot-$servicename.jar $GITHUB_WORKSPACE/bkci/$servicename/
done
mkdir -p $GITHUB_WORKSPACE/bkci/agentless/
cp -v boot-dockerhost.jar $GITHUB_WORKSPACE/bkci/agentless/boot-agentless.jar
true # 暂无init需求, 保留此步骤.
- name: Create artifact - Step3:Agent
- name: Create artifact - Step2:get Agent
uses: actions/download-artifact@v1
with:
name: agent
path: bkci/agent-package/upgrade/
- name: Create artifact - Step4:Frontend
path: src/agent/bin/

- name: Create artifact - Step3:get Frontend
uses: actions/download-artifact@v1
with:
name: frontend
path: bkci/frontend/

- name: Create artifact - Step5:generate boot-microservice.sh
run: ./generate_boot.sh
working-directory: scripts/
path: src/frontend/frontend

- name: Create artifact - Step6:support-files
- name: Create artifact - Step4:make package
id: create-artifact
run: |
cp -rf support-files/ bkci/
cp -rf support-files/agent-package bkci/
rm -vf scripts/generate_boot.sh
cp -rf scripts/ bkci/
rm -rf bkci/support-files/agent-package
cp -f bkci/frontend/console/frontend#console#index.html bkci/support-files/templates/frontend#console#index.html
cp -f bkci/frontend/pipeline/frontend#pipeline#index.html bkci/support-files/templates/frontend#pipeline#index.html
cp -f bkci/frontend/assets_bundle.json bkci/support-files/templates/project#assets_bundle.json
echo "::set-output name=version::$(basename $GITHUB_REF)"
basename $GITHUB_REF > bkci/VERSION
version="$(basename $GITHUB_REF)"
echo "::set-output name=version::$version"
ci_ms_wip="sign,monitoring" ci_pkg_dir=/dev/shm/ci ./scripts/packager-ci.sh "$version" bkci-slim.tar.gz
- name: Create Release
id: create_release
Expand All @@ -154,14 +115,13 @@ jobs:
release_name: bk-ci ${{ github.ref }}
draft: true
prerelease: true

- run: tar czvf bkci.tar.gz bkci

- name: Upload Release Asset
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: ./bkci.tar.gz
asset_name: bkci.tar.gz
asset_path: ./bkci-slim.tar.gz
asset_name: bkci-slim.tar.gz
asset_content_type: application/gzip
7 changes: 4 additions & 3 deletions scripts/bk-ci-install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -99,13 +99,14 @@ install_ci__ms_common (){
# 检查安装java
install_java || return $?
# 增量复制.
rsync -ra "$BK_CI_SRC_DIR/$MS_NAME/" "$BK_CI_HOME/$MS_NAME"
for f in agent-package jars-public jars-private scripts VERSION; do
[ -e "$BK_CI_SRC_DIR/$f" ] || continue
echo "install $BK_CI_SRC_DIR/$f to $BK_CI_HOME."
rsync -ra "$BK_CI_SRC_DIR/" "$BK_CI_HOME"
rsync -ra "$BK_CI_SRC_DIR/${f%/}" "$BK_CI_HOME"
done
# 保持微服务目录的强一致性.
rsync -ra --del "$BK_CI_SRC_DIR/$MS_NAME/" "$BK_CI_HOME/$MS_NAME"
# 保持微服务部分子目录的强一致性.
rsync -ra --del "$BK_CI_SRC_DIR/$MS_NAME/lib" "$BK_CI_SRC_DIR/$MS_NAME/com" "$BK_CI_HOME/$MS_NAME"
}

install_ci_dockerhost (){
Expand Down
2 changes: 1 addition & 1 deletion scripts/bk-ci-reg-systemd.sh
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ for MS_NAME in "$@"; do
}
done
if [ "${#invalid_proj}" -gt 1 ]; then
echo "ERROR: invalid proj: $invalid_proj."
echo "ERROR: invalid proj: ${invalid_proj:1}." # 去掉开头的逗号.
exit 15
fi

Expand Down
29 changes: 18 additions & 11 deletions scripts/bk-ci-start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -83,23 +83,26 @@ detect_main (){

# 检查服务启动成功. health接口为格式化后的, 要求整行匹配.
check_springboot_up (){
curl -m 1 -sf "http://127.0.0.1:$API_PORT/management/health" 2>/dev/null | grep -qx ' "status" : "UP",'
local port="$1"
curl -m 1 -sf "http://127.0.0.1:$port/management/health" 2>/dev/null | grep -qx ' "status" : "UP",'
}
# 等待服务启动成功.
wait_springboot_up (){
local pid="$1" port="$2" msg="app is up. ^_^"
local wait_count=40 wait_sec=3 # 等待app启动, 否则认为失败触发systemd的自动重启.
SECONDS=0
until check_springboot_up; do
echo "wait_springboot_up: $wait_count: sleep ${wait_sec}s.";
until check_springboot_up "$port"; do
echo "wait_springboot_up $port: $wait_count: sleep ${wait_sec}s.";
sleep "$wait_sec";
let wait_count-- || {
echo "wait_springboot_up: unable to confirm app status from http://127.0.0.1:$API_PORT/management/health"
echo "see $PWD/logs/bootstrap.log and $PWD/logs/$MS_NAME.log for details."
return 14
}
let wait_count-- || { msg="wait timeout"; break; }
check_pid_alive "$pid" || { msg="java is dead"; break; }
done
echo "wait_springboot_up: $wait_count: app is up. ^_^"
return 0
echo "wait_springboot_up $port: $wait_count: $msg."
if check_springboot_up "$port"; then
return 0
else
return 14
fi
}

# 启动微服务.
Expand All @@ -126,7 +129,11 @@ start_ci__springboot (){
echo "$java_pid" > "$pid_file" || return 24
echo "java pid is $java_pid."
# 此处阻塞.
wait_springboot_up
if ! wait_springboot_up "$java_pid" "$API_PORT"; then
echo "wait_springboot_up: unable to confirm app status from http://127.0.0.1:$API_PORT/management/health"
echo "see $PWD/logs/bootstrap.log and $PWD/logs/$MS_NAME.log for details."
return 14
fi
}

# 模拟systemd时的预设环境: 加载env, 切换启动目录及用户.
Expand Down
2 changes: 1 addition & 1 deletion scripts/bk-ci-upgrade.sh
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ check_ci_service_up (){
done
return $e
}
wait=${BKI_UPGRADE_WAIT:-60}
wait=${BKI_UPGRADE_WAIT:-30}
until check_ci_service_up; do
sleep 1
echo -n "${#pending_services[@]} "
Expand Down
125 changes: 125 additions & 0 deletions scripts/packager-ci.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
#!/bin/bash
# 生成bk-ci安装包.
# 收集编译产物, 生成所需的安装包目录, 然后打包.
set -eu
trap "on_ERR;" ERR
on_ERR (){
local fn=$0 ret=$? lineno=${BASH_LINENO:-$LINENO}
echo >&2 "ERROR $fn exit with $ret at line $lineno: $(sed -n ${lineno}p $0)."
}

my_path="$(readlink -f "$0")"
my_dir=${my_path%/*}
[ -d "$my_dir" ] || { echo >&2 "ERROR: my_dir is NOT an existed dir: $my_dir."; return 3; }
cmd_collect_ci_ms_name="$my_dir/bk-ci-collect-ms-name.sh"
cmd_ci_slim="$my_dir/bk-ci-slim.sh"

collect_frontend (){
echo "collect_frontend"
mkdir -p "$ci_pkg_dir/frontend" "$ci_pkg_dir/support-files/templates"
cp -a "$ci_bin_frontend_dir/." "$ci_pkg_dir/frontend"
echo "collect page templates."
find "$ci_pkg_dir/frontend" -name "frontend#*" -exec mv -v {} "$ci_pkg_dir/support-files/templates" \;
}

collect_goagent (){
mkdir -p "$1"
cp -rv "$ci_bin_goagent_dir/"* "$1"
}
collect_workeragent (){
mkdir -p "$1"
cp -rv "$ci_bin_agentjar_dir/"worker-agent.jar "$1"
}
collect_agentpackage (){
echo "collect_agentpackage"
cp -r "$ci_code_dir/support-files/agent-package" "$ci_pkg_dir"
collect_workeragent "$ci_pkg_dir/agent-package/jar"
collect_goagent "$ci_pkg_dir/agent-package/upgrade"
}

collect_backend (){
echo "collect_backend"
# 收集fatjar, slim化.
svcs=$(ls "$ci_bin_msjar_dir" | sed -n 's/boot-\(.*\).jar/\1/p')
for ms in $svcs; do
"$cmd_ci_slim" "$ms" "$ci_bin_msjar_dir/boot-$ms.jar" "$ci_pkg_dir"
done
# 清理ci_ms_wip
ci_template_dir="$ci_pkg_dir/support-files/templates"
shopt -s nullglob
echo "remove ms wip: $ci_ms_wip."
for ms in ${ci_ms_wip//,/ }; do
if [ -d "${ci_pkg_dir:-ci_pkg_dir}/${ms:-ms}/" ]; then
rm -rvf "${ci_pkg_dir:-ci_pkg_dir}/${ms:-ms}/"
fi
for f in "$ci_template_dir/$ms#"* "$ci_template_dir/"*"#$ms#"*; do
rm -vf "$ci_template_dir/$f"
done
done
shopt -u nullglob
}

collect_dirs (){
local e=0
for d in "$@"; do
if [ -d "$d" ]; then
cp -r "$ci_code_dir/$d" "$ci_pkg_dir"
else
echo "WARING: skip dir does not exist: $d."
let ++e
fi
done
return "$e"
}

collect_gateway (){
cp -r "$ci_code_dir/src/gateway" "$ci_pkg_dir"
}

prepare_agentless (){
echo "generate agentless."
cp -r "$ci_pkg_dir/dockerhost" "$ci_pkg_dir/agentless"
}

packager_ci (){
mkdir -p "$ci_pkg_dir"
echo "ci_code_dir is $ci_code_dir."
echo "ci_pkg_dir is $ci_pkg_dir."
collect_dirs "support-files" "scripts" "src/gateway"
rm -rf "$ci_pkg_dir"/support-files/agent-package
collect_dirs "docs" || true # 可选.
collect_agentpackage
collect_backend
collect_frontend
prepare_agentless
echo "gen version:"
echo "$VERSION" | tee "$ci_pkg_dir/VERSION"
echo "BK_CI_VERSION=\"$VERSION\"" | tee -a "$ci_pkg_dir/scripts/bkenv.properties"
echo "generate $ci_pkg_path from $ci_pkg_dir."
(cd "$ci_pkg_dir/.."; tar acf "$ci_pkg_path" "$(basename "$ci_pkg_dir")"; )
ls -l "$ci_pkg_path"
}

if [ $# -lt 2 ]; then
echo "Usage: $0 VERSION CI_PKG_PATH CI_CODE_DIR -- make install package"
echo " VERSION version string. example: v1.X.X-desc"
echo " CI_PKG_PATH generated install package path."
echo " CI_CODE_DIR source code with compiled binaries."
echo "Example: $0 v1.5.0-RELEASE workspace/bkci-slim.tar.gz workspace/bk-ci-v1.5.0"
echo "ENV:"
echo " ci_pkg_dir temp dir contains ci package files, should end with /ci/."
echo " ci_ms_wip ms still wip, should be removed if exist. comma separated."
exit 0
else
VERSION="$1"
ci_pkg_path="$(readlink -f "$2")"
ci_code_dir="${3:-${my_dir%/*}}" # 默认为本脚本的上层目录.
ci_pkg_dir="${ci_pkg_dir:-$ci_code_dir/ci}" # 默认为代码目录下的ci目录.
ci_ms_wip="${ci_ms_wip:-sign}" # sign需要重构优化, 暂不能用.
# 编译后的目录, 其他目录为code_dir的相对路径, 不提供修改.
ci_bin_frontend_dir="${ci_bin_frontend_dir:-$ci_code_dir/src/frontend/frontend/}"
ci_bin_goagent_dir=${ci_bin_goagent_dir:-$ci_code_dir/src/agent/bin/}
ci_bin_agentjar_dir=${ci_bin_agentjar_dir:-$ci_code_dir/src/backend/ci/release/}
ci_bin_msjar_dir=${ci_bin_msjar_dir:-$ci_code_dir/src/backend/ci/release/}
packager_ci
fi

0 comments on commit 1c052e5

Please sign in to comment.