diff --git a/action.yml b/action.yml index 3c4f7b4..42c1f10 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' @@ -39,6 +43,7 @@ runs: ARG_REPOSITORY: ${{ inputs.repository }} ARG_REPO_TOKEN: ${{ inputs.repo-token }} ARG_DEBUG: ${{ inputs.debug }} + ARG_TIMEOUT: ${{ inputs.timeout }} entrypoint: '/scripts/lock.sh' post-entrypoint: '/scripts/unlock.sh' diff --git a/rootfs/scripts/lock.sh b/rootfs/scripts/lock.sh index 1be1b0c..393c3a6 100755 --- a/rootfs/scripts/lock.sh +++ b/rootfs/scripts/lock.sh @@ -19,15 +19,14 @@ __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 -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/unlock.sh b/rootfs/scripts/unlock.sh index ba34a2e..674cd94 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" diff --git a/rootfs/scripts/utils.sh b/rootfs/scripts/utils.sh index 3c3a5ec..a9d8186 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