Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test PR 25 #27

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,17 @@ 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'
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'
Expand All @@ -39,6 +50,7 @@ runs:
ARG_REPOSITORY: ${{ inputs.repository }}
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'

15 changes: 10 additions & 5 deletions rootfs/scripts/lock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,16 @@ 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

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"

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"
fi
8 changes: 7 additions & 1 deletion rootfs/scripts/unlock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -19,4 +26,3 @@ set_up_repo "$__repo_url"
dequeue $ARG_BRANCH $__mutex_queue_file $__ticket_id

echo "Successfully unlocked"

2 changes: 1 addition & 1 deletion rootfs/scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ dequeue() {
else
1>&2 echo "[$__ticket_id] Not in queue! Mutex file:"
cat $__queue_file
exit 1
return
fi

git add $__queue_file
Expand Down
Loading