Skip to content

Commit 49c55d8

Browse files
committed
feat: cleanup the container post action
1 parent b1791da commit 49c55d8

File tree

3 files changed

+43
-1
lines changed

3 files changed

+43
-1
lines changed

Dockerfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
FROM docker:stable
22
COPY start-mongodb.sh /start-mongodb.sh
3-
RUN chmod +x /start-mongodb.sh
3+
COPY stop-mongodb.sh /stop-mongodb.sh
4+
RUN chmod +x /start-mongodb.sh /stop-mongodb.sh
45
ENTRYPOINT ["/start-mongodb.sh"]

action.yml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,3 +70,15 @@ runs:
7070
- ${{ inputs.mongodb-container-name }}
7171
- ${{ inputs.mongodb-key }}
7272
- ${{ inputs.mongodb-authsource }}
73+
post-entrypoint: /stop-mongodb.sh
74+
post-args:
75+
- ${{ inputs.mongodb-image }}
76+
- ${{ inputs.mongodb-version }}
77+
- ${{ inputs.mongodb-replica-set }}
78+
- ${{ inputs.mongodb-port }}
79+
- ${{ inputs.mongodb-db }}
80+
- ${{ inputs.mongodb-username }}
81+
- ${{ inputs.mongodb-password }}
82+
- ${{ inputs.mongodb-container-name }}
83+
- ${{ inputs.mongodb-key }}
84+
- ${{ inputs.mongodb-authsource }}

stop-mongodb.sh

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
#!/bin/sh
2+
3+
# Keep argument positions aligned with action.yml "args" so we can reuse them in post-args
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
12+
MONGODB_KEY=$9
13+
MONGODB_AUTHSOURCE=${10}
14+
15+
# Best-effort cleanup, do not fail the job if cleanup fails
16+
set +e
17+
18+
echo "::group::Cleaning up MongoDB container [$MONGODB_CONTAINER_NAME]"
19+
20+
if docker ps -a --format '{{.Names}}' | grep -Eq "^${MONGODB_CONTAINER_NAME}$"; then
21+
docker rm -f "$MONGODB_CONTAINER_NAME" >/dev/null 2>&1 || true
22+
echo "Removed container $MONGODB_CONTAINER_NAME"
23+
else
24+
echo "Container $MONGODB_CONTAINER_NAME not found; nothing to clean."
25+
fi
26+
27+
echo "::endgroup::"
28+
29+
exit 0

0 commit comments

Comments
 (0)