From 1461b0d9973956d87d8e6eb1ed1f54b3b0cce3a4 Mon Sep 17 00:00:00 2001 From: Yikun Jiang Date: Tue, 1 Sep 2020 15:26:54 +0800 Subject: [PATCH] Improve the exception handle (#36) - Added the debug arg to enable the detail log - Improve some exception warning --- .github/workflows/verify-on-ubuntu-org.yml | 1 + .../workflows/verify-on-ubuntu-user-cache.yml | 3 +- .github/workflows/verify-on-ubuntu-user.yml | 2 + action.yml | 4 ++ entrypoint.sh | 39 ++++++++++--------- 5 files changed, 30 insertions(+), 19 deletions(-) diff --git a/.github/workflows/verify-on-ubuntu-org.yml b/.github/workflows/verify-on-ubuntu-org.yml index e4c59f13..1d83e82e 100644 --- a/.github/workflows/verify-on-ubuntu-org.yml +++ b/.github/workflows/verify-on-ubuntu-org.yml @@ -19,3 +19,4 @@ jobs: black_list: 'KAE' white_list: 'KAE,Kunpeng,kunpengcompute.github.io' force_update: true + debug: true diff --git a/.github/workflows/verify-on-ubuntu-user-cache.yml b/.github/workflows/verify-on-ubuntu-user-cache.yml index 51461ca6..232655d8 100644 --- a/.github/workflows/verify-on-ubuntu-user-cache.yml +++ b/.github/workflows/verify-on-ubuntu-user-cache.yml @@ -28,7 +28,8 @@ jobs: dst_token: ${{ secrets.GITEE_TOKEN }} cache_path: /github/workspace/hub-mirror-cache white_list: 'hub-mirror-action, hexo-migrator-github-issue' + force_update: true - name: Print cache path run: | - ls -la /home/runner/work/hub-mirror-action/hub-mirror-action/hub-mirror-cache \ No newline at end of file + ls -la /home/runner/work/hub-mirror-action/hub-mirror-action/hub-mirror-cache diff --git a/.github/workflows/verify-on-ubuntu-user.yml b/.github/workflows/verify-on-ubuntu-user.yml index 34aa3b4e..64d654a5 100644 --- a/.github/workflows/verify-on-ubuntu-user.yml +++ b/.github/workflows/verify-on-ubuntu-user.yml @@ -15,3 +15,5 @@ jobs: dst_key: ${{ secrets.GITEE_PRIVATE_KEY }} dst_token: ${{ secrets.GITEE_TOKEN }} static_list: 'hub-mirror-action,hashes,simple-spring' + force_update: true + debug: true diff --git a/action.yml b/action.yml index e21300a7..7a13b312 100644 --- a/action.yml +++ b/action.yml @@ -38,6 +38,9 @@ inputs: force_update: description: "Force to update the destination repo, use '-f' flag do 'git push'" default: false + debug: + description: "Enable the debug flag to show detail log" + default: false runs: using: "docker" image: "Dockerfile" @@ -53,3 +56,4 @@ runs: - ${{ inputs.white_list }} - ${{ inputs.static_list }} - ${{ inputs.force_update}} + - ${{ inputs.debug}} diff --git a/entrypoint.sh b/entrypoint.sh index 8f708cad..b83e60bd 100755 --- a/entrypoint.sh +++ b/entrypoint.sh @@ -1,6 +1,11 @@ #!/bin/bash -set -x +DEBUG="${INPUT_DEBUG}" + +if [[ "$DEBUG" == "true" ]]; then + set -x +fi + mkdir -p /root/.ssh echo "${INPUT_DST_KEY}" > /root/.ssh/id_rsa chmod 600 /root/.ssh/id_rsa @@ -28,6 +33,11 @@ STATIC_LIST="${INPUT_STATIC_LIST}" FORCE_UPDATE="${INPUT_FORCE_UPDATE}" +function err_exit { + echo -e "\033[31m $1 \033[0m" + exit 1 +} + if [[ "$ACCOUNT_TYPE" == "org" ]]; then SRC_LIST_URL_SUFFIX=orgs/$SRC_ACCOUNT/repos DST_LIST_URL_SUFFIX=orgs/$DST_ACCOUNT/repos @@ -37,8 +47,7 @@ elif [[ "$ACCOUNT_TYPE" == "user" ]]; then DST_LIST_URL_SUFFIX=users/$DST_ACCOUNT/repos DST_CREATE_URL_SUFFIX=user/repos else - echo "Unknown account type, the `account_type` should be `user` or `org`" - exit 1 + err_exit "Unknown account type, the `account_type` should be `user` or `org`" fi if [[ "$SRC_TYPE" == "github" ]]; then @@ -56,8 +65,7 @@ elif [[ "$SRC_TYPE" == "gitee" ]]; then SRC_REPO_BASE_URL=https://gitee.com/ fi else - echo "Unknown src args, the `src` should be `[github|gittee]/account`" - exit 1 + err_exit "Unknown src args, the `src` should be `[github|gittee]/account`" fi if [[ -z $STATIC_LIST ]]; then @@ -73,12 +81,10 @@ elif [[ "$DST_TYPE" == "gitee" ]]; then DST_REPO_CREATE_API=https://gitee.com/api/v5/$DST_CREATE_URL_SUFFIX DST_REPO_LIST_API=https://gitee.com/api/v5/$DST_LIST_URL_SUFFIX else - echo "Unknown dst args, the `dst` should be `[github|gittee]/account`" - exit 1 + err_exit "Unknown dst args, the `dst` should be `[github|gittee]/account`" fi - -function cd_src_repo +function clone_repo { echo -e "\033[31m(0/3)\033[0m" "Downloading..." if [ ! -d "$1" ]; then @@ -87,11 +93,12 @@ function cd_src_repo cd $1 } -function add_remote_repo +function create_repo { # Auto create non-existing repo has_repo=`curl $DST_REPO_LIST_API | jq '.[] | select(.full_name=="'$DST_ACCOUNT'/'$1'").name' | wc -l` if [ $has_repo == 0 ]; then + echo "Create non-exist repo..." if [[ "$DST_TYPE" == "github" ]]; then curl -H "Authorization: token $2" --data '{"name":"'$1'"}' $DST_REPO_CREATE_API elif [[ "$DST_TYPE" == "gitee" ]]; then @@ -155,17 +162,13 @@ for repo in $SRC_REPOS if test_black_white_list $repo ; then echo -e "\n\033[31mBackup $repo ...\033[0m" - cd_src_repo $repo + clone_repo $repo || echo "clone and cd failed" - add_remote_repo $repo $DST_TOKEN + create_repo $repo $DST_TOKEN || echo "create failed" - update_repo + update_repo || echo "Update failed" - if [ $? -eq 0 ]; then - import_repo - else - echo -e "\033[31mUpdate failed.\033[0m" "" - fi + import_repo || err_exit "Push failed" cd .. fi