Skip to content
Open
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
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand All @@ -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'

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

6 changes: 3 additions & 3 deletions rootfs/scripts/unlock.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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"
9 changes: 9 additions & 0 deletions rootfs/scripts/utils.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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
Expand Down