From 857a96ed26d9c2a0a3f3a79afd7d4efa91ef00a4 Mon Sep 17 00:00:00 2001 From: lvsi <15239928381@163.com> Date: Sun, 3 Mar 2024 13:24:32 +0800 Subject: [PATCH] fix: fix bash not pass variables bug( replace to source ) --- .github/workflows/cicd.yml | 10 ++- .../cd/stage_deploy/job_pull_artifact.sh | 4 +- .pipeline/cd/stage_deploy/job_ssh.sh | 4 +- .pipeline/cd/stage_monitor/job_api_test.sh | 4 +- .../cd/stage_monitor/job_health_check.sh | 6 +- .pipeline/ci/stage_build/job_apidoc_gen.sh | 4 +- .pipeline/ci/stage_build/job_local_build.sh | 4 +- .pipeline/ci/stage_pre/job_pre_check.sh | 4 +- .pipeline/ci/stage_pre/job_pre_install.sh | 4 +- .pipeline/ci/stage_test/job_check_code.sh | 4 +- .pipeline/ci/stage_test/job_unit_test.sh | 4 +- .pipeline/internal/helper.sh | 4 +- .pipeline/start.sh | 10 +-- doc/design.md | 4 +- template/.github-ci.yml | 32 +++++++-- template/.gitlab-ci.yml | 65 +++++++------------ 16 files changed, 89 insertions(+), 78 deletions(-) diff --git a/.github/workflows/cicd.yml b/.github/workflows/cicd.yml index 63716c6..2fb900c 100644 --- a/.github/workflows/cicd.yml +++ b/.github/workflows/cicd.yml @@ -11,20 +11,28 @@ jobs: # ========================= CI ========================= # [stage] = pre CI_STAGE_PRE_JOB_PRE_CHECK_SWITCH: "ON" + CI_STAGE_PRE_JOB_PRE_CHECK_CMD: "" CI_STAGE_PRE_JOB_PRE_INSTALL_SWITCH: "ON" + CI_STAGE_PRE_JOB_PRE_INSTALL_CMD: "" # [stage] = test CI_STAGE_TEST_JOB_UNIT_TEST_SWITCH: "ON" - CI_STAGE_TEST_JOB_UNIT_TEST_TRIGGER_CMD: "cd tests && bash test.sh" + CI_STAGE_TEST_JOB_UNIT_TEST_CMD: "cd tests && bash test.sh" 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: "" 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: "" # [stage] = monitor CD_STAGE_MONITOR_JOB_API_TEST_SWITCH: "ON" + CD_STAGE_MONITOR_JOB_API_TEST_CMD: "" CD_STAGE_MONITOR_JOB_HEALTH_CHECK_SWITCH: "ON" + CD_STAGE_MONITOR_JOB_HEALTH_CHECK_CMD: "" steps: - uses: actions/checkout@v1 - run: | diff --git a/.pipeline/cd/stage_deploy/job_pull_artifact.sh b/.pipeline/cd/stage_deploy/job_pull_artifact.sh index 49e247f..0324188 100644 --- a/.pipeline/cd/stage_deploy/job_pull_artifact.sh +++ b/.pipeline/cd/stage_deploy/job_pull_artifact.sh @@ -1,8 +1,8 @@ #!/bin/sh # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then - if ! eval "${global_trigger_cmd}"; then +if [ "${global_job_cmd}" != "" ]; then + if ! eval "${global_job_cmd}"; then exit 1 fi fi diff --git a/.pipeline/cd/stage_deploy/job_ssh.sh b/.pipeline/cd/stage_deploy/job_ssh.sh index 49e247f..0324188 100644 --- a/.pipeline/cd/stage_deploy/job_ssh.sh +++ b/.pipeline/cd/stage_deploy/job_ssh.sh @@ -1,8 +1,8 @@ #!/bin/sh # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then - if ! eval "${global_trigger_cmd}"; then +if [ "${global_job_cmd}" != "" ]; then + if ! eval "${global_job_cmd}"; then exit 1 fi fi diff --git a/.pipeline/cd/stage_monitor/job_api_test.sh b/.pipeline/cd/stage_monitor/job_api_test.sh index 1eaa067..2c6c41b 100644 --- a/.pipeline/cd/stage_monitor/job_api_test.sh +++ b/.pipeline/cd/stage_monitor/job_api_test.sh @@ -1,8 +1,8 @@ #!/bin/sh # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then - if ! eval "${global_trigger_cmd}"; then +if [ "${global_job_cmd}" != "" ]; then + if ! eval "${global_job_cmd}"; then exit 1 fi fi diff --git a/.pipeline/cd/stage_monitor/job_health_check.sh b/.pipeline/cd/stage_monitor/job_health_check.sh index 2d98ab3..9f7f846 100644 --- a/.pipeline/cd/stage_monitor/job_health_check.sh +++ b/.pipeline/cd/stage_monitor/job_health_check.sh @@ -1,14 +1,14 @@ #!/bin/sh # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then +if [ "${global_job_cmd}" != "" ]; then i=0 while true do ((i++)) - res=$(eval $global_trigger_cmd) - if [ "${res}" == "${global_trigger_cmd_res}" ]; then + res=$(eval $global_job_cmd) + if [ "${res}" == "${global_job_cmd_res}" ]; then echo "health check success" break fi diff --git a/.pipeline/ci/stage_build/job_apidoc_gen.sh b/.pipeline/ci/stage_build/job_apidoc_gen.sh index 49e247f..0324188 100644 --- a/.pipeline/ci/stage_build/job_apidoc_gen.sh +++ b/.pipeline/ci/stage_build/job_apidoc_gen.sh @@ -1,8 +1,8 @@ #!/bin/sh # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then - if ! eval "${global_trigger_cmd}"; then +if [ "${global_job_cmd}" != "" ]; then + if ! eval "${global_job_cmd}"; then exit 1 fi fi diff --git a/.pipeline/ci/stage_build/job_local_build.sh b/.pipeline/ci/stage_build/job_local_build.sh index 49e247f..0324188 100644 --- a/.pipeline/ci/stage_build/job_local_build.sh +++ b/.pipeline/ci/stage_build/job_local_build.sh @@ -1,8 +1,8 @@ #!/bin/sh # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then - if ! eval "${global_trigger_cmd}"; then +if [ "${global_job_cmd}" != "" ]; then + if ! eval "${global_job_cmd}"; then exit 1 fi fi diff --git a/.pipeline/ci/stage_pre/job_pre_check.sh b/.pipeline/ci/stage_pre/job_pre_check.sh index 49e247f..0324188 100644 --- a/.pipeline/ci/stage_pre/job_pre_check.sh +++ b/.pipeline/ci/stage_pre/job_pre_check.sh @@ -1,8 +1,8 @@ #!/bin/sh # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then - if ! eval "${global_trigger_cmd}"; then +if [ "${global_job_cmd}" != "" ]; then + if ! eval "${global_job_cmd}"; then exit 1 fi fi diff --git a/.pipeline/ci/stage_pre/job_pre_install.sh b/.pipeline/ci/stage_pre/job_pre_install.sh index 54da2df..93c47bf 100644 --- a/.pipeline/ci/stage_pre/job_pre_install.sh +++ b/.pipeline/ci/stage_pre/job_pre_install.sh @@ -12,8 +12,8 @@ echo "2. install curl" apt-get install curl -y # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then - if ! eval "${global_trigger_cmd}"; then +if [ "${global_job_cmd}" != "" ]; then + if ! eval "${global_job_cmd}"; then exit 1 fi fi diff --git a/.pipeline/ci/stage_test/job_check_code.sh b/.pipeline/ci/stage_test/job_check_code.sh index 49e247f..0324188 100644 --- a/.pipeline/ci/stage_test/job_check_code.sh +++ b/.pipeline/ci/stage_test/job_check_code.sh @@ -1,8 +1,8 @@ #!/bin/sh # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then - if ! eval "${global_trigger_cmd}"; then +if [ "${global_job_cmd}" != "" ]; then + if ! eval "${global_job_cmd}"; then exit 1 fi fi diff --git a/.pipeline/ci/stage_test/job_unit_test.sh b/.pipeline/ci/stage_test/job_unit_test.sh index 49e247f..0324188 100644 --- a/.pipeline/ci/stage_test/job_unit_test.sh +++ b/.pipeline/ci/stage_test/job_unit_test.sh @@ -1,8 +1,8 @@ #!/bin/sh # if trigger cmd exists, then execute it. -if [ "${global_trigger_cmd}" != "" ]; then - if ! eval "${global_trigger_cmd}"; then +if [ "${global_job_cmd}" != "" ]; then + if ! eval "${global_job_cmd}"; then exit 1 fi fi diff --git a/.pipeline/internal/helper.sh b/.pipeline/internal/helper.sh index 55d97b9..7ec487c 100644 --- a/.pipeline/internal/helper.sh +++ b/.pipeline/internal/helper.sh @@ -64,8 +64,8 @@ send_failure_notice(){ MESSAGE="【Failed】CI/CD Failed Notice global_job_name: ${global_job_name} global_job_path: ${global_job_path} - global_switch_name: ${global_switch_name} - global_trigger_cmd: ${global_trigger_cmd} + global_job_switch_name: ${global_job_switch_name} + global_job_cmd: ${global_job_cmd} —— made by CIManager " if [ "${DING_NOTICE_SWITCH}" == "on" ] && [ "${DING_ACCESS_TOKEN}" != "" ] ; then diff --git a/.pipeline/start.sh b/.pipeline/start.sh index 3b12ad8..7f22a3f 100644 --- a/.pipeline/start.sh +++ b/.pipeline/start.sh @@ -30,16 +30,16 @@ job_queue=( for job_name in "${job_queue[@]}"; do global_job_name=${job_name//./_} global_job_path="${BASE_PATH}/.pipeline/${job_name//./\/}.sh" - global_switch_name="${global_job_name^^}_SWITCH" # ^^ equals upper() - global_trigger_cmd_name="${global_job_name^^}_TRIGGER_CMD" - global_trigger_cmd=${!global_trigger_cmd_name} + global_job_switch_name="${global_job_name^^}_SWITCH" # ^^ equals upper() + global_job_cmd_name="${global_job_name^^}_CMD" + global_job_cmd=${!global_job_cmd_name} print_phase "handle job: ${global_job_name}" # check if the switch is on - switch_status=${!global_switch_name} + switch_status=${!global_job_switch_name} if [ "${switch_status}" != "ON" ]; then - print_warn "job is skipped, due to the switch(${global_switch_name}=${switch_status}) is off\n\n" + print_warn "job is skipped, due to the switch(${global_job_switch_name}=${switch_status}) is off\n\n" continue fi diff --git a/doc/design.md b/doc/design.md index b059ba3..6c92492 100644 --- a/doc/design.md +++ b/doc/design.md @@ -48,8 +48,8 @@ fi 由于可能有单独运行任务的需求,所以为了脚本可以正常执行不出错,即使在```[1,2]```中已经检查了变量,那么在任务脚本中时,仍然要写检查变量是否存在的代码,如下代码所示。 ```bash -if [ "${API_TEST_TRIGGER_CMD}" != "" ]; then - eval $API_TEST_TRIGGER_CMD +if [ "${API_TEST_CMD}" != "" ]; then + eval $API_TEST_CMD if [ $? -ne 0 ]; then FAILURE_REASON="run API Test failed" send_failure_notice diff --git a/template/.github-ci.yml b/template/.github-ci.yml index e8ee687..4fbe383 100644 --- a/template/.github-ci.yml +++ b/template/.github-ci.yml @@ -10,15 +10,33 @@ jobs: CIManager: runs-on: ubuntu-latest env: - # define directory in gitHub action - working-directory: .github/../ - # variable configuration for [check code] - CHECK_CODE_SWITCH: "on" + # ========================= CI ========================= + # [stage] = pre + CI_STAGE_PRE_JOB_PRE_CHECK_SWITCH: "ON" + CI_STAGE_PRE_JOB_PRE_CHECK_CMD: "" + CI_STAGE_PRE_JOB_PRE_INSTALL_SWITCH: "ON" + CI_STAGE_PRE_JOB_PRE_INSTALL_CMD: "" + # [stage] = test + CI_STAGE_TEST_JOB_UNIT_TEST_SWITCH: "ON" + CI_STAGE_TEST_JOB_UNIT_TEST_CMD: "cd tests && bash test.sh" + 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: "" + 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: "" + # [stage] = monitor + CD_STAGE_MONITOR_JOB_API_TEST_SWITCH: "ON" + CD_STAGE_MONITOR_JOB_API_TEST_CMD: "" + CD_STAGE_MONITOR_JOB_HEALTH_CHECK_SWITCH: "ON" + CD_STAGE_MONITOR_JOB_HEALTH_CHECK_CMD: "" - # variable configuration for [unit test] - UNIT_TEST_SWITCH: "on" - UNIT_TEST_TRIGGER_CMD: "cd tests && bash test.sh" steps: - uses: actions/checkout@v1 - run: | diff --git a/template/.gitlab-ci.yml b/template/.gitlab-ci.yml index 28a3288..02c2f49 100644 --- a/template/.gitlab-ci.yml +++ b/template/.gitlab-ci.yml @@ -1,47 +1,32 @@ # this example yml file: https://jihulab.com/WGrape/apimock-example/-/blob/main/.gitlab-ci.yml image: golang:1.17 variables: - # variable configuration for [your private gitlab host] - GITLAB_HOST: "" - GITLAB_API_TOKEN: "" - # variable configuration for [your project] - PROJECT_NAME: "apimock-example" - PROJECT_ID: 48845 - - # variable configuration for [DingDing WebHook] - DING_KEYWORD: "apimock-example" - DING_ACCESS_TOKEN: "" - DING_NOTICE_SWITCH: "off" - - # variable configuration for [check code] - CHECK_CODE_SWITCH: "on" - - # variable configuration for [unit test] - UNIT_TEST_TRIGGER_CMD: "cd mock && go test -v . && cd .. && \ - cd service && go test -v . && cd .. - " - UNIT_TEST_SWITCH: "on" - - # variable configuration for [apidoc generator] - APIDOC_TRIGGER_CMD: "cd mock && go test -v . && cd .." - APIDOC_FILE: "apidoc.md" - APIDOC_SWITCH: "off" - - # variable configuration for [local build] - LOCAL_BUILD_TRIGGER_CMD: "go mod download && go build -o project && nohup ./project &" - LOCAL_BUILD_SWITCH: "on" - - # variable configuration for [health check] - HEALTH_CHECK_TRIGGER_CMD: "curl -X GET 127.0.0.1:8000/ping" - HEALTH_CHECK_SUCCESS: "ok" - HEALTH_CHECK_SWITCH: "on" - -before_script: - - echo '====== CIManager Start Running =========' - -after_script: - - echo '====== CIManager Stopped Successfully =========' + # ========================= CI ========================= + # [stage] = pre + CI_STAGE_PRE_JOB_PRE_CHECK_SWITCH: "ON" + CI_STAGE_PRE_JOB_PRE_CHECK_CMD: "" + CI_STAGE_PRE_JOB_PRE_INSTALL_SWITCH: "ON" + CI_STAGE_PRE_JOB_PRE_INSTALL_CMD: "" + # [stage] = test + CI_STAGE_TEST_JOB_UNIT_TEST_SWITCH: "ON" + CI_STAGE_TEST_JOB_UNIT_TEST_CMD: "cd tests && bash test.sh" + 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: "" + 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: "" + # [stage] = monitor + CD_STAGE_MONITOR_JOB_API_TEST_SWITCH: "ON" + CD_STAGE_MONITOR_JOB_API_TEST_CMD: "" + CD_STAGE_MONITOR_JOB_HEALTH_CHECK_SWITCH: "ON" + CD_STAGE_MONITOR_JOB_HEALTH_CHECK_CMD: "" stages: - CIManager