Skip to content

Commit cbbc6f8

Browse files
authored
Merge pull request #64 from Sam-Bate-ITV/feature/alternative_image
#62: add option for specifying image
2 parents 5a87bd8 + 6131e7f commit cbbc6f8

File tree

4 files changed

+38
-9
lines changed

4 files changed

+38
-9
lines changed

README.md

+12
Original file line numberDiff line numberDiff line change
@@ -246,6 +246,18 @@ jobs:
246246

247247
**Caveat:** due to [this issue](https://github.com/docker-library/mongo/issues/211), you **cannot enable user creation AND replica sets** initially. Therefore, if you use this action to setup a replica set, please create your users through a separate script.
248248

249+
### Using a Custom Mongo Image
250+
You can utilize an alternative MongoDB docker image using the `mongodb-image` input:
251+
252+
253+
```yaml
254+
- name: Start MongoDB
255+
uses: supercharge/[email protected]
256+
with:
257+
# Here we are using an image from Amazon's ECR rather than the default image from Docker Hub
258+
mongodb-image: 'public.ecr.aws/docker/library/mongo'
259+
mongodb-version: ${{ matrix.mongodb-version }}
260+
```
249261

250262
## License
251263
MIT © [Supercharge](https://superchargejs.com)

action-types.yml

+2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
# See https://github.com/krzema12/github-actions-typing
22
inputs:
3+
mongodb-image:
4+
type: string
35
mongodb-version:
46
type: string
57
mongodb-replica-set:

action.yml

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,11 @@ branding:
66
color: 'green'
77

88
inputs:
9+
mongodb-image:
10+
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")'
11+
required: false
12+
default: 'mongo'
13+
914
mongodb-version:
1015
description: 'MongoDB version to use (default "latest")'
1116
required: false
@@ -45,6 +50,7 @@ runs:
4550
using: 'docker'
4651
image: 'Dockerfile'
4752
args:
53+
- ${{ inputs.mongodb-image }}
4854
- ${{ inputs.mongodb-version }}
4955
- ${{ inputs.mongodb-replica-set }}
5056
- ${{ inputs.mongodb-port }}

start-mongodb.sh

+18-9
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,25 @@
11
#!/bin/sh
22

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

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

16+
if [ -z "$MONGODB_IMAGE" ]; then
17+
echo ""
18+
echo "Missing MongoDB image in the [mongodb-image] input. Received value: $MONGODB_IMAGE"
19+
echo ""
20+
21+
exit 2
22+
fi
1523

1624
if [ -z "$MONGODB_VERSION" ]; then
1725
echo ""
@@ -21,6 +29,7 @@ if [ -z "$MONGODB_VERSION" ]; then
2129
exit 2
2230
fi
2331

32+
echo "::group::Using MongoDB Docker image $MONGODB_IMAGE:$MONGODB_VERSION"
2433

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

86-
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
95+
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
8796

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

106115

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

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

0 commit comments

Comments
 (0)