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

Should delete existing container #59

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
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
2 changes: 2 additions & 0 deletions action-types.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,5 @@ inputs:
type: string
mongodb-container-name:
type: string
should-delete-existing-container:
type: string
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,11 @@ inputs:
required: false
default: 'mongodb'

should-delete-existing-container:
description: 'Determine whether to delete an existing container defined by the "mongodb-container-name" variable (default: "no")'
required: false
default: 'no'

runs:
using: 'docker'
image: 'Dockerfile'
Expand All @@ -52,3 +57,4 @@ runs:
- ${{ inputs.mongodb-username }}
- ${{ inputs.mongodb-password }}
- ${{ inputs.mongodb-container-name }}
- ${{ inputs.should-delete-existing-container }}
26 changes: 18 additions & 8 deletions start-mongodb.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ MONGODB_DB=$4
MONGODB_USERNAME=$5
MONGODB_PASSWORD=$6
MONGODB_CONTAINER_NAME=$7
SHOULD_DELETE_EXISTING_CONTAINER=$8

# `mongosh` is used starting from MongoDB 5.x
MONGODB_CLIENT="mongosh --quiet"
Expand Down Expand Up @@ -57,20 +58,29 @@ wait_for_mongodb () {
sleep 1
TIMER=$((TIMER + 1))

if [[ $TIMER -eq 40 ]]; then
echo "MongoDB did not initialize within 40 seconds. Exiting."
if [[ $TIMER -eq 20 ]]; then
echo "MongoDB did not initialize within 20 seconds. Exiting."
exit 2
fi
done
echo "::endgroup::"
}

# check if the container already exists and remove it
## TODO: put this behind an option flag
# if [ "$(docker ps -q -f name=$MONGODB_CONTAINER_NAME)" ]; then
# echo "Removing existing container [$MONGODB_CONTAINER_NAME]"
# docker rm -f $MONGODB_CONTAINER_NAME
# fi

if [[ "$SHOULD_DELETE_EXISTING_CONTAINER" == "yes" ]] || [[ "$SHOULD_DELETE_EXISTING_CONTAINER" == "1" ]]; then
echo "::group::Deleting possibly existing Docker container named [$MONGODB_CONTAINER_NAME]"
echo " - container-name [$MONGODB_CONTAINER_NAME]"
echo ""

if [ "$(docker ps --quiet --filter name=$MONGODB_CONTAINER_NAME)" ]; then
echo "Removing existing container [$MONGODB_CONTAINER_NAME]"
docker rm --force $MONGODB_CONTAINER_NAME
else
echo "No other container with name [$MONGODB_CONTAINER_NAME] exists. Nothing to delete"
fi

echo "::endgroup::"
fi


if [ -z "$MONGODB_REPLICA_SET" ]; then
Expand Down
Loading