Skip to content

Commit

Permalink
#62: add option for specifying image
Browse files Browse the repository at this point in the history
  • Loading branch information
Sam-Bate-ITV committed Oct 25, 2024
1 parent 5a87bd8 commit 2ae9a45
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 9 deletions.
1 change: 1 addition & 0 deletions .github/workflows/main.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,5 @@ jobs:
- name: Start MongoDB Server
uses: ./
with:
mongodb-image: 'public.ecr.aws/docker/library/mongo'
mongodb-version: ${{ matrix.mongodb-version }}
1 change: 1 addition & 0 deletions .github/workflows/test-auth.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ jobs:
- name: Start MongoDB Server v${{ matrix.mongodb-version }}
uses: ./
with:
mongodb-image: 'public.ecr.aws/docker/library/mongo'
mongodb-version: ${{ matrix.mongodb-version }}
mongodb-db: ${{ matrix.mongodb-db }}
mongodb-username: ${{ matrix.mongodb-username }}
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-replica-set.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- name: Start MongoDB Server v${{ matrix.mongodb-version }}
uses: ./
with:
mongodb-image: 'public.ecr.aws/docker/library/mongo'
mongodb-version: ${{ matrix.mongodb-version }}
mongodb-replica-set: mongodb-test-rs

Expand Down
1 change: 1 addition & 0 deletions .github/workflows/test-single-instance.yml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ jobs:
- name: Start MongoDB Server v${{ matrix.mongodb-version }}
uses: ./
with:
mongodb-image: 'public.ecr.aws/docker/library/mongo'
mongodb-version: ${{ matrix.mongodb-version }}

- name: Use Node.js ${{ matrix.node-version }}
Expand Down
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,8 @@ jobs:
- name: Start MongoDB
uses: supercharge/[email protected]
with:
# Here we are using an image from Amazon's ECR rather than the default image from Docker Hub
mongodb-image: 'public.ecr.aws/docker/library/mongo'
mongodb-version: ${{ matrix.mongodb-version }}

- run: npm install
Expand Down
2 changes: 2 additions & 0 deletions action-types.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# See https://github.com/krzema12/github-actions-typing
inputs:
mongodb-image:
type: string
mongodb-version:
type: string
mongodb-replica-set:
Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,11 @@ branding:
color: 'green'

inputs:
mongodb-image:
description: 'MongoDB image to use (defaults to using "mongo" from Docker Hub but you could also use an image from another repository such as Amazons "public.ecr.aws/docker/library/mongo")'
required: false
default: 'mongo'

mongodb-version:
description: 'MongoDB version to use (default "latest")'
required: false
Expand Down Expand Up @@ -45,6 +50,7 @@ runs:
using: 'docker'
image: 'Dockerfile'
args:
- ${{ inputs.mongodb-image }}
- ${{ inputs.mongodb-version }}
- ${{ inputs.mongodb-replica-set }}
- ${{ inputs.mongodb-port }}
Expand Down
27 changes: 18 additions & 9 deletions start-mongodb.sh
Original file line number Diff line number Diff line change
@@ -1,17 +1,25 @@
#!/bin/sh

# Map input values from the GitHub Actions workflow to shell variables
MONGODB_VERSION=$1
MONGODB_REPLICA_SET=$2
MONGODB_PORT=$3
MONGODB_DB=$4
MONGODB_USERNAME=$5
MONGODB_PASSWORD=$6
MONGODB_CONTAINER_NAME=$7
MONGODB_IMAGE=$1
MONGODB_VERSION=$2
MONGODB_REPLICA_SET=$3
MONGODB_PORT=$4
MONGODB_DB=$5
MONGODB_USERNAME=$6
MONGODB_PASSWORD=$7
MONGODB_CONTAINER_NAME=$8

# `mongosh` is used starting from MongoDB 5.x
MONGODB_CLIENT="mongosh --quiet"

if [ -z "$MONGODB_IMAGE" ]; then
echo ""
echo "Missing MongoDB image in the [mongodb-image] input. Received value: $MONGODB_IMAGE"
echo ""

exit 2
fi

if [ -z "$MONGODB_VERSION" ]; then
echo ""
Expand All @@ -21,6 +29,7 @@ if [ -z "$MONGODB_VERSION" ]; then
exit 2
fi

echo "::group::Using mogo image $MONGODB_IMAGE:$MONGODB_VERSION"

echo "::group::Selecting correct MongoDB client"
if [ "`echo $MONGODB_VERSION | cut -c 1`" -le "4" ]; then
Expand Down Expand Up @@ -83,7 +92,7 @@ if [ -z "$MONGODB_REPLICA_SET" ]; then
echo " - container-name [$MONGODB_CONTAINER_NAME]"
echo ""

docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT -e MONGO_INITDB_DATABASE=$MONGODB_DB -e MONGO_INITDB_ROOT_USERNAME=$MONGODB_USERNAME -e MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD --detach mongo:$MONGODB_VERSION --port $MONGODB_PORT
docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT -e MONGO_INITDB_DATABASE=$MONGODB_DB -e MONGO_INITDB_ROOT_USERNAME=$MONGODB_USERNAME -e MONGO_INITDB_ROOT_PASSWORD=$MONGODB_PASSWORD --detach $MONGODB_IMAGE:$MONGODB_VERSION --port $MONGODB_PORT

if [ $? -ne 0 ]; then
echo "Error starting MongoDB Docker container"
Expand All @@ -104,7 +113,7 @@ echo " - replica set [$MONGODB_REPLICA_SET]"
echo ""


docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT --detach mongo:$MONGODB_VERSION --port $MONGODB_PORT --replSet $MONGODB_REPLICA_SET
docker run --name $MONGODB_CONTAINER_NAME --publish $MONGODB_PORT:$MONGODB_PORT --detach $MONGODB_IMAGE:$MONGODB_VERSION --port $MONGODB_PORT --replSet $MONGODB_REPLICA_SET

if [ $? -ne 0 ]; then
echo "Error starting MongoDB Docker container"
Expand Down

0 comments on commit 2ae9a45

Please sign in to comment.