Skip to content

Commit

Permalink
fix: fix bash not pass variables bug( replace to source )
Browse files Browse the repository at this point in the history
  • Loading branch information
WGrape committed Mar 3, 2024
1 parent f958e8a commit 857a96e
Show file tree
Hide file tree
Showing 16 changed files with 89 additions and 78 deletions.
10 changes: 9 additions & 1 deletion .github/workflows/cicd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
4 changes: 2 additions & 2 deletions .pipeline/cd/stage_deploy/job_pull_artifact.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions .pipeline/cd/stage_deploy/job_ssh.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions .pipeline/cd/stage_monitor/job_api_test.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
6 changes: 3 additions & 3 deletions .pipeline/cd/stage_monitor/job_health_check.sh
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions .pipeline/ci/stage_build/job_apidoc_gen.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions .pipeline/ci/stage_build/job_local_build.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions .pipeline/ci/stage_pre/job_pre_check.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions .pipeline/ci/stage_pre/job_pre_install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
4 changes: 2 additions & 2 deletions .pipeline/ci/stage_test/job_check_code.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions .pipeline/ci/stage_test/job_unit_test.sh
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions .pipeline/internal/helper.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 5 additions & 5 deletions .pipeline/start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
4 changes: 2 additions & 2 deletions doc/design.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
32 changes: 25 additions & 7 deletions template/.github-ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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: |
Expand Down
65 changes: 25 additions & 40 deletions template/.gitlab-ci.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down

0 comments on commit 857a96e

Please sign in to comment.