From 9de061f3763231d6ff03d56322c5a5e50c115b3b Mon Sep 17 00:00:00 2001 From: Zach Goldberg Date: Mon, 1 Apr 2024 12:21:30 -0700 Subject: [PATCH 1/7] feat: support force unlock action --- action.yml | 6 +++++- rootfs/scripts/lock.sh | 5 +++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/action.yml b/action.yml index 3c4f7b4..08b9f2d 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' + action: + description: 'What locking action to take, either `lock` or `unlock`' + required: false + default: 'lock' runs: using: 'docker' image: 'Dockerfile' @@ -39,6 +43,6 @@ runs: ARG_REPOSITORY: ${{ inputs.repository }} ARG_REPO_TOKEN: ${{ inputs.repo-token }} ARG_DEBUG: ${{ inputs.debug }} + ARG_ACTION: ${{ inputs.action }} entrypoint: '/scripts/lock.sh' post-entrypoint: '/scripts/unlock.sh' - diff --git a/rootfs/scripts/lock.sh b/rootfs/scripts/lock.sh index 53d1c75..22f43e3 100755 --- a/rootfs/scripts/lock.sh +++ b/rootfs/scripts/lock.sh @@ -6,6 +6,11 @@ fi SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" +if [ $ARG_ACTION = "unlock" ]; then + bash "$SCRIPT_DIR/unlock.sh" + exit +fi + source "$SCRIPT_DIR/utils.sh" echo "Cloning and checking out $ARG_REPOSITORY:$ARG_BRANCH in $ARG_CHECKOUT_LOCATION" From 0cb3d53a55081fb2a4ff47d3e410dafdf556c756 Mon Sep 17 00:00:00 2001 From: Zach Goldberg Date: Mon, 1 Apr 2024 12:27:49 -0700 Subject: [PATCH 2/7] bugfix attempt --- rootfs/scripts/lock.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rootfs/scripts/lock.sh b/rootfs/scripts/lock.sh index 22f43e3..d3dca23 100755 --- a/rootfs/scripts/lock.sh +++ b/rootfs/scripts/lock.sh @@ -7,7 +7,7 @@ fi SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" if [ $ARG_ACTION = "unlock" ]; then - bash "$SCRIPT_DIR/unlock.sh" + source "$SCRIPT_DIR/unlock.sh" exit fi From 752b1f04e3292d8f46b0c137eadddd94ab547d23 Mon Sep 17 00:00:00 2001 From: Zach Goldberg Date: Mon, 1 Apr 2024 12:32:31 -0700 Subject: [PATCH 3/7] ticket id bugfix --- rootfs/scripts/lock.sh | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/rootfs/scripts/lock.sh b/rootfs/scripts/lock.sh index d3dca23..6a420f7 100755 --- a/rootfs/scripts/lock.sh +++ b/rootfs/scripts/lock.sh @@ -6,11 +6,6 @@ fi SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" -if [ $ARG_ACTION = "unlock" ]; then - source "$SCRIPT_DIR/unlock.sh" - exit -fi - source "$SCRIPT_DIR/utils.sh" echo "Cloning and checking out $ARG_REPOSITORY:$ARG_BRANCH in $ARG_CHECKOUT_LOCATION" @@ -23,9 +18,14 @@ __repo_url="https://x-access-token:$ARG_REPO_TOKEN@$ARG_GITHUB_SERVER/$ARG_REPOS __ticket_id="$GITHUB_RUN_ID-$(date +%s)-$(( $RANDOM % 1000 ))" echo "ticket_id=$__ticket_id" >> $GITHUB_STATE -set_up_repo "$__repo_url" -enqueue $ARG_BRANCH $__mutex_queue_file $__ticket_id -wait_for_lock $ARG_BRANCH $__mutex_queue_file $__ticket_id -echo "Lock successfully acquired" +set_up_repo "$__repo_url" +if [ $ARG_ACTION = "unlock" ]; then + dequeue $ARG_BRANCH $__mutex_queue_file $__ticket_id + echo "Successfully unlocked" +else + enqueue $ARG_BRANCH $__mutex_queue_file $__ticket_id + wait_for_lock $ARG_BRANCH $__mutex_queue_file $__ticket_id + echo "Lock successfully acquired" +fi From 1f90095620c71931de53e63601e94936f1d31188 Mon Sep 17 00:00:00 2001 From: Zach Goldberg Date: Mon, 1 Apr 2024 12:39:06 -0700 Subject: [PATCH 4/7] pass in the ticket id for unlocking --- action.yml | 8 ++++++++ rootfs/scripts/lock.sh | 4 ++-- rootfs/scripts/unlock.sh | 1 - 3 files changed, 10 insertions(+), 3 deletions(-) diff --git a/action.yml b/action.yml index 08b9f2d..9040654 100644 --- a/action.yml +++ b/action.yml @@ -33,6 +33,13 @@ inputs: description: 'What locking action to take, either `lock` or `unlock`' required: false default: 'lock' + ticket_id: + description: 'If action is unlock, this is the ticket id from a prior lock' + required: false +outputs: + ticket_id: + description: 'The unique ID of this particular lock, can be used to forcibly unlock in a subsequent action call' + runs: using: 'docker' image: 'Dockerfile' @@ -44,5 +51,6 @@ runs: ARG_REPO_TOKEN: ${{ inputs.repo-token }} ARG_DEBUG: ${{ inputs.debug }} ARG_ACTION: ${{ inputs.action }} + ARG_TICKET_ID: ${{ inputs.ticket_id }} entrypoint: '/scripts/lock.sh' post-entrypoint: '/scripts/unlock.sh' diff --git a/rootfs/scripts/lock.sh b/rootfs/scripts/lock.sh index 6a420f7..4c76430 100755 --- a/rootfs/scripts/lock.sh +++ b/rootfs/scripts/lock.sh @@ -17,12 +17,12 @@ __mutex_queue_file=mutex_queue __repo_url="https://x-access-token:$ARG_REPO_TOKEN@$ARG_GITHUB_SERVER/$ARG_REPOSITORY" __ticket_id="$GITHUB_RUN_ID-$(date +%s)-$(( $RANDOM % 1000 ))" echo "ticket_id=$__ticket_id" >> $GITHUB_STATE - +echo "ticket_id=$__ticket_id" >> $GITHUB_OUTPUT set_up_repo "$__repo_url" if [ $ARG_ACTION = "unlock" ]; then - dequeue $ARG_BRANCH $__mutex_queue_file $__ticket_id + dequeue $ARG_BRANCH $__mutex_queue_file $ARG_TICKET_ID echo "Successfully unlocked" else enqueue $ARG_BRANCH $__mutex_queue_file $__ticket_id diff --git a/rootfs/scripts/unlock.sh b/rootfs/scripts/unlock.sh index 825a5a1..6ef71c2 100755 --- a/rootfs/scripts/unlock.sh +++ b/rootfs/scripts/unlock.sh @@ -19,4 +19,3 @@ set_up_repo "$__repo_url" dequeue $ARG_BRANCH $__mutex_queue_file $__ticket_id echo "Successfully unlocked" - From 2f3ea4bb0382dc3bb9ab604f2d58bb203c32e7b4 Mon Sep 17 00:00:00 2001 From: Zach Goldberg Date: Mon, 1 Apr 2024 12:43:28 -0700 Subject: [PATCH 5/7] dont barf if cant auto unlock --- rootfs/scripts/lock.sh | 4 ++-- rootfs/scripts/utils.sh | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/rootfs/scripts/lock.sh b/rootfs/scripts/lock.sh index 4c76430..2e2c6f2 100755 --- a/rootfs/scripts/lock.sh +++ b/rootfs/scripts/lock.sh @@ -16,8 +16,6 @@ cd "$ARG_CHECKOUT_LOCATION" __mutex_queue_file=mutex_queue __repo_url="https://x-access-token:$ARG_REPO_TOKEN@$ARG_GITHUB_SERVER/$ARG_REPOSITORY" __ticket_id="$GITHUB_RUN_ID-$(date +%s)-$(( $RANDOM % 1000 ))" -echo "ticket_id=$__ticket_id" >> $GITHUB_STATE -echo "ticket_id=$__ticket_id" >> $GITHUB_OUTPUT set_up_repo "$__repo_url" @@ -25,6 +23,8 @@ if [ $ARG_ACTION = "unlock" ]; then dequeue $ARG_BRANCH $__mutex_queue_file $ARG_TICKET_ID echo "Successfully unlocked" else + echo "ticket_id=$__ticket_id" >> $GITHUB_STATE + echo "ticket_id=$__ticket_id" >> $GITHUB_OUTPUT enqueue $ARG_BRANCH $__mutex_queue_file $__ticket_id wait_for_lock $ARG_BRANCH $__mutex_queue_file $__ticket_id echo "Lock successfully acquired" diff --git a/rootfs/scripts/utils.sh b/rootfs/scripts/utils.sh index 3c3a5ec..a83a6f1 100644 --- a/rootfs/scripts/utils.sh +++ b/rootfs/scripts/utils.sh @@ -112,7 +112,7 @@ dequeue() { else 1>&2 echo "[$__ticket_id] Not in queue! Mutex file:" cat $__queue_file - exit 1 + exit 0 fi git add $__queue_file From 06671c0cfced71773e115fd3b0f9d5113c95eb40 Mon Sep 17 00:00:00 2001 From: Zach Goldberg Date: Mon, 1 Apr 2024 12:47:58 -0700 Subject: [PATCH 6/7] exit successfully if queue is already released --- 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 a83a6f1..7588a8e 100644 --- a/rootfs/scripts/utils.sh +++ b/rootfs/scripts/utils.sh @@ -112,7 +112,7 @@ dequeue() { else 1>&2 echo "[$__ticket_id] Not in queue! Mutex file:" cat $__queue_file - exit 0 + return fi git add $__queue_file From 70591f216a659f2d314de044cb673f26732280e8 Mon Sep 17 00:00:00 2001 From: Zach Goldberg Date: Mon, 1 Apr 2024 12:51:11 -0700 Subject: [PATCH 7/7] better skip maybe --- rootfs/scripts/unlock.sh | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/rootfs/scripts/unlock.sh b/rootfs/scripts/unlock.sh index 6ef71c2..1f18183 100755 --- a/rootfs/scripts/unlock.sh +++ b/rootfs/scripts/unlock.sh @@ -4,6 +4,13 @@ if [ $ARG_DEBUG != "false" ]; then set -x fi +if [ $ARG_ACTION = "unlock" ]; then + # This action's purpose was to unlock, so no need + # to double-unlock in the post-processing step + echo "Skipping unlock in post-processing for action=unlock" + exit +fi + SCRIPT_DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )" source "$SCRIPT_DIR/utils.sh"