From f5a176cc030955ae1cec43dce7e5523be5533a09 Mon Sep 17 00:00:00 2001 From: "jui-wen.chen" Date: Tue, 23 Jul 2024 15:05:31 +0200 Subject: [PATCH 1/5] add timeout and slim down msg --- action.yml | 4 ++++ rootfs/scripts/lock.sh | 2 +- rootfs/scripts/utils.sh | 9 +++++++++ 3 files changed, 14 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 3c4f7b48..53956631 100644 --- a/action.yml +++ b/action.yml @@ -29,6 +29,10 @@ inputs: description: '[Internal] The location to store the mutex repo' required: false default: '/run/gh-action-mutex/repo' + timeout: + description: 'timeout in seconds to return exist code 1' + required: false + default: '0' runs: using: 'docker' image: 'Dockerfile' diff --git a/rootfs/scripts/lock.sh b/rootfs/scripts/lock.sh index 1be1b0c2..85a18aab 100755 --- a/rootfs/scripts/lock.sh +++ b/rootfs/scripts/lock.sh @@ -27,7 +27,7 @@ cat "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$ARG_BRANCH" set_up_repo "$__repo_url" enqueue $ARG_BRANCH $__mutex_queue_file $__ticket_id -wait_for_lock $ARG_BRANCH $__mutex_queue_file $__ticket_id +wait_for_lock $ARG_BRANCH $__mutex_queue_file $__ticket_id $ARG_TIMEOUT echo "Lock successfully acquired" diff --git a/rootfs/scripts/utils.sh b/rootfs/scripts/utils.sh index 3c3a5ec0..f957fe5b 100644 --- a/rootfs/scripts/utils.sh +++ b/rootfs/scripts/utils.sh @@ -61,15 +61,18 @@ enqueue() { fi } +total_sleep_time=0 # Wait for the lock to become available # args: # $1: branch # $2: queue_file # $3: ticket_id +# $4: timeout in minuts wait_for_lock() { __branch=$1 __queue_file=$2 __ticket_id=$3 + __timeout=$4 update_branch $__branch @@ -79,6 +82,12 @@ wait_for_lock() { if [ "$cur_lock" != "$__ticket_id" ]; then echo "[$__ticket_id] Waiting for lock - Current lock assigned to [$cur_lock]" sleep 5 + total_sleep_time=$((total_sleep_time + 5)) + echo "It has been waiting for $total_sleep_time seconds zzz" + if [$__timeout -gt 0] && [$total_sleep_time -gt $__timeout]; then + echo "[$__ticket_id] Total sleep time exceeded $__timeout seconds, exiting with code 1" + exit 1 + fi wait_for_lock $@ fi else From 5b9f0c1d029c081dc59898c0dc4933479e0fac50 Mon Sep 17 00:00:00 2001 From: "jui-wen.chen" Date: Tue, 23 Jul 2024 15:15:24 +0200 Subject: [PATCH 2/5] add env --- action.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/action.yml b/action.yml index 53956631..4252354d 100644 --- a/action.yml +++ b/action.yml @@ -43,6 +43,7 @@ runs: ARG_REPOSITORY: ${{ inputs.repository }} ARG_REPO_TOKEN: ${{ inputs.repo-token }} ARG_DEBUG: ${{ inputs.debug }} + ARG_DEBUG: ${{ inputs.timeout }} entrypoint: '/scripts/lock.sh' post-entrypoint: '/scripts/unlock.sh' From b3da748cfc2b94663a10432c387251d8f92b4ce1 Mon Sep 17 00:00:00 2001 From: "jui-wen.chen" Date: Tue, 23 Jul 2024 15:16:35 +0200 Subject: [PATCH 3/5] rename --- action.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 4252354d..42c1f10a 100644 --- a/action.yml +++ b/action.yml @@ -43,7 +43,7 @@ runs: ARG_REPOSITORY: ${{ inputs.repository }} ARG_REPO_TOKEN: ${{ inputs.repo-token }} ARG_DEBUG: ${{ inputs.debug }} - ARG_DEBUG: ${{ inputs.timeout }} + ARG_TIMEOUT: ${{ inputs.timeout }} entrypoint: '/scripts/lock.sh' post-entrypoint: '/scripts/unlock.sh' From 7f5d0f0af05577de283c83fe0c4c27e9af6c43fc Mon Sep 17 00:00:00 2001 From: "jui-wen.chen" Date: Tue, 23 Jul 2024 15:23:42 +0200 Subject: [PATCH 4/5] tt --- rootfs/scripts/utils.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rootfs/scripts/utils.sh b/rootfs/scripts/utils.sh index f957fe5b..a9d81861 100644 --- a/rootfs/scripts/utils.sh +++ b/rootfs/scripts/utils.sh @@ -84,7 +84,7 @@ wait_for_lock() { sleep 5 total_sleep_time=$((total_sleep_time + 5)) echo "It has been waiting for $total_sleep_time seconds zzz" - if [$__timeout -gt 0] && [$total_sleep_time -gt $__timeout]; then + if [[ $__timeout -gt 0 && $total_sleep_time -gt $__timeout ]]; then echo "[$__ticket_id] Total sleep time exceeded $__timeout seconds, exiting with code 1" exit 1 fi From ae7623ffe057446dd01f80965218eb24cb0eeb6e Mon Sep 17 00:00:00 2001 From: "jui-wen.chen" Date: Wed, 2 Apr 2025 15:31:33 +0200 Subject: [PATCH 5/5] concatenate GITHUB_JOB to the filename --- rootfs/scripts/lock.sh | 9 ++++----- rootfs/scripts/unlock.sh | 6 +++--- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/rootfs/scripts/lock.sh b/rootfs/scripts/lock.sh index 85a18aab..393c3a6a 100755 --- a/rootfs/scripts/lock.sh +++ b/rootfs/scripts/lock.sh @@ -19,11 +19,10 @@ __ticket_id="$GITHUB_RUN_ID-$(date +%s)-$(( $RANDOM % 1000 ))" # $GITHUB_STATE does NOT exist in the post-entry, so $GITHUB_WORKSPACE is used instead to share the info between entry and post-entry # https://docs.github.com/en/actions/using-workflows/workflow-commands-for-github-actions#sending-values-to-the-pre-and-post-actions # https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runspre-entrypoint -# The file `.$GITHUB_RUN_ID-$ARG_BRANCH` is used to share the info between entry and post-entry -# As $GITHUB_RUN_ID doesn't exist in GitHub Action, -# $ARG_BRANCH is used to concatenate to the file name to avoid the conflict between jobs in the same run -echo "ticket_id=$__ticket_id" > "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$ARG_BRANCH" -cat "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$ARG_BRANCH" +# The file `.$GITHUB_RUN_ID-$GITHUB_JOB` is used to share the info between entry and post-entry +# $GITHUB_JOB is used to concatenate to the file name to avoid the conflict between jobs in the same run +echo "ticket_id=$__ticket_id" > "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$GITHUB_JOB" +cat "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$GITHUB_JOB" set_up_repo "$__repo_url" enqueue $ARG_BRANCH $__mutex_queue_file $__ticket_id diff --git a/rootfs/scripts/unlock.sh b/rootfs/scripts/unlock.sh index ba34a2ec..674cd94c 100755 --- a/rootfs/scripts/unlock.sh +++ b/rootfs/scripts/unlock.sh @@ -13,12 +13,12 @@ cd "$ARG_CHECKOUT_LOCATION" __mutex_queue_file=mutex_queue __repo_url="https://x-access-token:$ARG_REPO_TOKEN@$ARG_GITHUB_SERVER/$ARG_REPOSITORY" -cat "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$ARG_BRANCH" -export $(grep -v '^#' $GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$ARG_BRANCH | xargs -0) +cat "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$GITHUB_JOB" +export $(grep -v '^#' $GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$GITHUB_JOB | xargs -0) __ticket_id=$ticket_id set_up_repo "$__repo_url" dequeue $ARG_BRANCH $__mutex_queue_file $__ticket_id -rm -f "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$ARG_BRANCH" +rm -f "$GITHUB_WORKSPACE/.$GITHUB_RUN_ID-$GITHUB_JOB" echo "Successfully unlocked"