Skip to content

Commit d257057

Browse files
committed
feat: support network and network-alias
1 parent 7a52156 commit d257057

File tree

3 files changed

+31
-0
lines changed

3 files changed

+31
-0
lines changed

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,16 @@ inputs:
5656
required: false
5757
default: 'admin'
5858

59+
docker-network:
60+
description: 'Docker network to attach the MongoDB container to. If not provided, will try to use the default GitHub Actions network if available (github_network_<rand>).'
61+
required: false
62+
default: ''
63+
64+
docker-network-alias:
65+
description: 'Network alias for the MongoDB container when attaching to a Docker network. If not provided, will use mongodb-container-name input'
66+
required: false
67+
default: ''
68+
5969
runs:
6070
using: 'docker'
6171
image: 'Dockerfile'
@@ -70,4 +80,6 @@ runs:
7080
- ${{ inputs.mongodb-container-name }}
7181
- ${{ inputs.mongodb-key }}
7282
- ${{ inputs.mongodb-authsource }}
83+
- ${{ inputs.docker-network }}
84+
- ${{ inputs.docker-network-alias }}
7385
post-entrypoint: /stop-mongodb.sh

start-mongodb.sh

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,21 @@ MONGODB_PASSWORD=$7
1111
MONGODB_CONTAINER_NAME=$8
1212
MONGODB_KEY=$9
1313
MONGODB_AUTHSOURCE=${10}
14+
DOCKER_NETWORK=${11}
15+
DOCKER_NETWORK_ALIAS=${12:-$MONGODB_CONTAINER_NAME}
16+
17+
# If DOCKER_NETWORK not provided, try to detect the default GitHub Actions network
18+
if [ -z "$DOCKER_NETWORK" ]; then
19+
if docker network ls --format '{{.Name}}' | grep -q '^github_network$'; then
20+
DOCKER_NETWORK=github_network
21+
fi
22+
fi
23+
24+
# Build network args if a network is set
25+
NETWORK_ARGS=""
26+
if [ -n "$DOCKER_NETWORK" ]; then
27+
NETWORK_ARGS="--network $DOCKER_NETWORK --network-alias $DOCKER_NETWORK_ALIAS"
28+
fi
1429

1530
# `mongosh` is used starting from MongoDB 5.x
1631
MONGODB_CLIENT="mongosh --quiet"
@@ -87,6 +102,7 @@ if [ -z "$MONGODB_REPLICA_SET" ]; then
87102
echo ""
88103

89104
docker run --name $MONGODB_CONTAINER_NAME \
105+
$NETWORK_ARGS \
90106
--publish $MONGODB_PORT:$MONGODB_PORT \
91107
-e MONGO_INITDB_DATABASE=$MONGODB_DB \
92108
-e MONGO_INITDB_ROOT_USERNAME=$MONGODB_USERNAME \
@@ -136,6 +152,7 @@ fi
136152
# MONGO_INITDB_* envs will create the root user on first startup
137153

138154
docker run --name $MONGODB_CONTAINER_NAME \
155+
$NETWORK_ARGS \
139156
--publish $MONGODB_PORT:$MONGODB_PORT \
140157
$VOLUME_ARGS \
141158
-e MONGO_INITDB_DATABASE=$MONGODB_DB \

stop-mongodb.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@ MONGODB_PASSWORD=$7
1111
MONGODB_CONTAINER_NAME=$8
1212
MONGODB_KEY=$9
1313
MONGODB_AUTHSOURCE=${10}
14+
DOCKER_NETWORK=${11}
15+
DOCKER_NETWORK_ALIAS=${12}
1416

1517
# Best-effort cleanup, do not fail the job if cleanup fails
1618
set +e

0 commit comments

Comments
 (0)