diff --git a/action-types.yml b/action-types.yml index 467fe41..95c24a2 100644 --- a/action-types.yml +++ b/action-types.yml @@ -14,3 +14,5 @@ inputs: type: string mongodb-container-name: type: string + should-delete-existing-container: + type: string diff --git a/action.yml b/action.yml index 170e6a9..5c25a52 100644 --- a/action.yml +++ b/action.yml @@ -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' @@ -52,3 +57,4 @@ runs: - ${{ inputs.mongodb-username }} - ${{ inputs.mongodb-password }} - ${{ inputs.mongodb-container-name }} + - ${{ inputs.should-delete-existing-container }} diff --git a/start-mongodb.sh b/start-mongodb.sh index 3c75e17..02b6e02 100644 --- a/start-mongodb.sh +++ b/start-mongodb.sh @@ -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" @@ -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