From bafe5c8a902a9c9eac0e4ea4677ac6175a56b8f3 Mon Sep 17 00:00:00 2001 From: lvsi <15239928381@163.com> Date: Tue, 5 Mar 2024 19:46:25 +0800 Subject: [PATCH] update: update template --- template/.github-ci.yml | 25 +++------- template/.gitlab-ci.yml | 24 +++------ template/.sparrow-example-ci.yml | 27 +++-------- template/cicd.yml | 83 ++++++++++++++++++++++++++++++++ 4 files changed, 102 insertions(+), 57 deletions(-) create mode 100644 template/cicd.yml diff --git a/template/.github-ci.yml b/template/.github-ci.yml index 4fbe383..0eca183 100644 --- a/template/.github-ci.yml +++ b/template/.github-ci.yml @@ -10,7 +10,10 @@ jobs: CIManager: runs-on: ubuntu-latest env: - + # ========================= System ========================= + # ========================= Project ========================= + PROJECT_CURRENT_BRANCH: "test" + PROJECT_CURRENT_ENV: "test" # ========================= CI ========================= # [stage] = pre CI_STAGE_PRE_JOB_PRE_CHECK_SWITCH: "ON" @@ -40,21 +43,5 @@ jobs: steps: - uses: actions/checkout@v1 - run: | - printf "================ Start to Boot CIManager ================\n" - git clone https://github.com/wgrape/CIManager.git - cp -an ./CIManager/.pipeline ./ - rm -rf ./CIManager - cd .pipeline - printf "================ End Boot ================\n\n" - printf "================ Debug Start ================\n\n" - show_env(){ - echo "1. cat /proc/version="$(cat /proc/version) - echo "2. pwd="$(pwd) - echo "3. ls="$(ls -a) - } - show_env - printf "================ End Debug ================\n\n" - printf "\e[36m>>>>>>>>>>> Start Pipeline\e[0m\n\n" - # you can run sudo with the -E/--preserve-env switch, sudo -E/--preserve-env ..., which retains environment variables. - sudo --preserve-env bash start.sh - printf "\e[36m<<<<<<<<<<< End Pipeline\e[0m" + git clone https://github.com/wgrape/CIManager.git && bash ./CIManager/connect.sh + sudo --preserve-env bash .pipeline/start.sh # you can run sudo with the -E/--preserve-env switch, sudo -E/--preserve-env ..., which retains environment variables. diff --git a/template/.gitlab-ci.yml b/template/.gitlab-ci.yml index 02c2f49..8e1ce5f 100644 --- a/template/.gitlab-ci.yml +++ b/template/.gitlab-ci.yml @@ -2,6 +2,10 @@ image: golang:1.17 variables: + # ========================= System ========================= + # ========================= Project ========================= + PROJECT_CURRENT_BRANCH: "test" + PROJECT_CURRENT_ENV: "test" # ========================= CI ========================= # [stage] = pre CI_STAGE_PRE_JOB_PRE_CHECK_SWITCH: "ON" @@ -35,21 +39,5 @@ CIManager: stage: CIManager script: - | - printf "================ Start to Boot CIManager ================\n" - git clone https://github.com/wgrape/CIManager.git - cp -an ./CIManager/.pipeline ./ - rm -rf ./CIManager - cd .pipeline - printf "================ End Boot ================\n\n" - printf "================ Debug Start ================\n\n" - show_env(){ - echo "1. cat /proc/version="$(cat /proc/version) - echo "2. pwd="$(pwd) - echo "3. ls="$(ls -a) - } - show_env - printf "================ End Debug ================\n\n" - printf "\e[36m>>>>>>>>>>> Start Pipeline\e[0m\n\n" - # you can run sudo with the -E/--preserve-env switch, sudo -E/--preserve-env ..., which retains environment variables. - sudo --preserve-env bash start.sh - printf "\e[36m<<<<<<<<<<< End Pipeline\e[0m" + git clone https://github.com/wgrape/CIManager.git && bash ./CIManager/connect.sh + sudo --preserve-env bash .pipeline/start.sh # you can run sudo with the -E/--preserve-env switch, sudo -E/--preserve-env ..., which retains environment variables. diff --git a/template/.sparrow-example-ci.yml b/template/.sparrow-example-ci.yml index ba9b3fe..4f37bd6 100644 --- a/template/.sparrow-example-ci.yml +++ b/template/.sparrow-example-ci.yml @@ -10,6 +10,10 @@ jobs: CIManager: runs-on: ubuntu-latest env: + # ========================= System ========================= + # ========================= Project ========================= + PROJECT_CURRENT_BRANCH: "test" + PROJECT_CURRENT_ENV: "test" # ========================= CI ========================= # [stage] = pre CI_STAGE_PRE_JOB_PRE_CHECK_SWITCH: "ON" @@ -18,8 +22,7 @@ jobs: CI_STAGE_PRE_JOB_PRE_INSTALL_CMD: | cd sparrow && bash _install.sh && bash ./sparrow startone go && cd .. mkdir -p ./sparrow/go/data/mapserver - rsync -av --exclude='../mapserver/sparrow/' ../mapserver/ "./sparrow/go/data/mapserver" - ls ./sparrow/go/data + rsync -av --exclude='sparrow/' --exclude='.git/' --exclude='.github/' --exclude='README.md' --exclude='.gitignore' --exclude='logs/' ./ "./sparrow/go/data/mapserver" # [stage] = test CI_STAGE_TEST_JOB_UNIT_TEST_SWITCH: "ON" CI_STAGE_TEST_JOB_UNIT_TEST_CMD: "cd pkg/util && go test -v ." @@ -45,21 +48,5 @@ jobs: steps: - uses: actions/checkout@v1 - run: | - printf "================ Start to Boot CIManager ================\n" - git clone https://github.com/wgrape/CIManager.git - cp -an ./CIManager/.pipeline ./ - rm -rf ./CIManager - cd .pipeline - printf "================ End Boot ================\n\n" - printf "================ Debug Start ================\n\n" - show_env(){ - echo "1. cat /proc/version="$(cat /proc/version) - echo "2. pwd="$(pwd) - echo "3. ls="$(ls -a) - } - show_env - printf "================ End Debug ================\n\n" - printf "\e[36m>>>>>>>>>>> Start Pipeline\e[0m\n\n" - # you can run sudo with the -E/--preserve-env switch, sudo -E/--preserve-env ..., which retains environment variables. - sudo --preserve-env bash start.sh - printf "\e[36m<<<<<<<<<<< End Pipeline\e[0m" + git clone https://github.com/wgrape/CIManager.git && bash ./CIManager/connect.sh + sudo --preserve-env bash .pipeline/start.sh # you can run sudo with the -E/--preserve-env switch, sudo -E/--preserve-env ..., which retains environment variables. diff --git a/template/cicd.yml b/template/cicd.yml new file mode 100644 index 0000000..ecd106f --- /dev/null +++ b/template/cicd.yml @@ -0,0 +1,83 @@ +name: github-ci +on: + push: + branches: + - main + - test + +env: + # ========================= System ========================= + # ========================= Project ========================= + PROJECT_CURRENT_BRANCH: "test" + PROJECT_CURRENT_ENV: "test" + # ========================= CI ========================= + # [stage] = pre + CI_STAGE_PRE_JOB_PRE_CHECK_SWITCH: "ON" + CI_STAGE_PRE_JOB_PRE_CHECK_CMD: "docker-compose --version" + CI_STAGE_PRE_JOB_PRE_INSTALL_SWITCH: "ON" + CI_STAGE_PRE_JOB_PRE_INSTALL_CMD: | + # 先启动Go环境 + cd sparrow && bash _install.sh && bash ./sparrow startone go && cd .. + # 把本项目拷贝到Go环境中 + mkdir -p ./sparrow/go/data/mapserver + rsync -av --exclude='sparrow/' --exclude='.git/' --exclude='.github/' --exclude='README.md' --exclude='.gitignore' --exclude='logs/' ./ "./sparrow/go/data/mapserver" + # [stage] = test + CI_STAGE_TEST_JOB_UNIT_TEST_SWITCH: "ON" + CI_STAGE_TEST_JOB_UNIT_TEST_CMD: "cd pkg/util && go test -v . && cd ../../" + CI_STAGE_TEST_JOB_CHECK_CODE_SWITCH: "ON" + CI_STAGE_TEST_JOB_CHECK_CODE_CMD: "" + # [stage] = build + CI_STAGE_BUILD_JOB_LOCAL_BUILD_SWITCH: "ON" + CI_STAGE_BUILD_JOB_LOCAL_BUILD_CMD: | + # 开始编译Go环境中的本项目 + docker exec -i sparrow_container_${PROJECT_CURRENT_ENV}_go bash -c "cd /var/data/go/mapserver; go mod download; go mod tidy; go build -buildvcs=false -o mapbin .; nohup ./mapbin -f etc/application.${PROJECT_CURRENT_ENV}.yaml > nohup.$(date +"%Y%m%d").log &" + docker exec -i sparrow_container_${PROJECT_CURRENT_ENV}_go bash -c "curl -s -X GET '127.0.0.1:2200/ping'" + # 成功编译之后把Go环境中的二进制文件拷贝到本项目目录下 + cp ./sparrow/go/data/mapserver/mapbin ./mapbin + CI_STAGE_BUILD_JOB_APIDOC_GEN_SWITCH: "ON" + CI_STAGE_BUILD_JOB_APIDOC_GEN_CMD: "" + # ========================= CD ========================= + # [stage] = deploy + CD_STAGE_DEPLOY_JOB_SSH_SWITCH: "ON" + CD_STAGE_DEPLOY_JOB_SSH_CMD: "" + CD_STAGE_DEPLOY_JOB_LAUNCH_SWITCH: "ON" + CD_STAGE_DEPLOY_JOB_LAUNCH_CMD: | + base_path="/root/dockerbase/test/sparrow/go/data/mapserver/" + cd $base_path && chmod +x ./mapbin + docker exec -i sparrow_container_${PROJECT_CURRENT_ENV}_go bash -c 'pid=$(ps aux | grep "./mapbin -f etc/application." | grep -v grep | awk "{print \$2}"); echo $pid; kill -q $pid;' + docker exec -i sparrow_container_${PROJECT_CURRENT_ENV}_go bash -c "cd /var/data/go/mapserver/ && nohup ./mapbin -f etc/application.${PROJECT_CURRENT_ENV}.yaml >> nohup.$(date +"%Y%m%d").log &" + docker exec -i sparrow_container_${PROJECT_CURRENT_ENV}_go bash -c "curl -s -X GET '127.0.0.1:2200/ping'" + # [stage] = monitor + CD_STAGE_MONITOR_JOB_API_TEST_SWITCH: "ON" + CD_STAGE_MONITOR_JOB_API_TEST_CMD: "curl -s -X GET '127.0.0.1:2200/ping'" + CD_STAGE_MONITOR_JOB_HEALTH_CHECK_SWITCH: "ON" + CD_STAGE_MONITOR_JOB_HEALTH_CHECK_CMD: "curl -s -X GET '127.0.0.1:2200/ping'" + CD_STAGE_MONITOR_JOB_HEALTH_CHECK_CMD_RES: "ok" + +jobs: + CIManager: + runs-on: ubuntu-latest + steps: + # 获取CIManager并连接, 连接成功后开始执行pipeline + - uses: actions/checkout@v1 + - run: | + git clone https://github.com/wgrape/CIManager.git && bash ./CIManager/connect.sh + sudo --preserve-env bash .pipeline/start.sh # you can run sudo with the -E/--preserve-env switch, sudo -E/--preserve-env ..., which retains environment variables. + + # 上传至服务器 + - uses: appleboy/scp-action@master + with: + host: xxx.xxx.xxx.xxx + username: username + password: password + source: "./mapbin,./etc/application.test.yaml,./.pipeline" + target: "/root/dockerbase/test/sparrow/go/data/mapserver/" + + # 运行服务器上的二进制文件 + - uses: garygrossgarten/github-action-ssh@release # https://github.com/appleboy/scp-action/tree/master + with: + command: cd /root/dockerbase/test/sparrow/go/data/mapserver/; bash ./.pipeline/start.sh "cd.stage_deploy.job_launch" + host: xxx.xxx.xxx.xxx + username: username + password: password + privateKey: ${{ secrets.PRIVATE_KEY}} \ No newline at end of file