Skip to content

Commit

Permalink
should delete existing container
Browse files Browse the repository at this point in the history
  • Loading branch information
marcuspoehls committed Apr 9, 2024
1 parent b827754 commit 9f2957d
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 6 deletions.
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 }}
21 changes: 15 additions & 6 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 @@ -66,12 +67,20 @@ wait_for_mongodb () {
}


# 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

0 comments on commit 9f2957d

Please sign in to comment.