From 9c4929a4702a620bd5a3e590bd576ae1b91b8d74 Mon Sep 17 00:00:00 2001 From: Kien Pham Date: Tue, 25 Jul 2023 15:36:40 -0700 Subject: [PATCH] Add option to remove container when it exits --- README.md | 25 +++++++++++++++++++++++++ action.yml | 6 ++++++ start-redis.sh | 11 +++++++++-- 3 files changed, 40 insertions(+), 2 deletions(-) mode change 100644 => 100755 start-redis.sh diff --git a/README.md b/README.md index 543b99c..446b4f5 100644 --- a/README.md +++ b/README.md @@ -145,6 +145,31 @@ jobs: - name: … ``` +### Remove container when exit +Starting v1.6.0, when running this action on a self-hosted runner, it's helpful to remove the container so its name won't conflict: + +```yaml +name: Run tests + +on: [push] + +jobs: + build: + runs-on: ubuntu-latest + strategy: + matrix: + redis-version: [4, 5, 6] + + steps: + - name: Start Redis + uses: supercharge/redis-github-action@1.6.0 + with: + redis-version: ${{ matrix.redis-version }} + redis-remove-container: true # false by default + + - name: … +``` + ## License MIT © [Supercharge](https://superchargejs.com) diff --git a/action.yml b/action.yml index 2b9bf7a..907777e 100644 --- a/action.yml +++ b/action.yml @@ -22,6 +22,11 @@ inputs: description: "Name of the created container. Useful if you run multiple Redis containers" required: false default: 'redis' + redis-remove-container: + description: "Remove container after container exit?" + required: false + type: boolean + default: false runs: using: 'docker' @@ -31,3 +36,4 @@ runs: - ${{ inputs.redis-version }} - ${{ inputs.redis-port }} - ${{ inputs.redis-container-name }} + - ${{ inputs.redis-remove-container }} diff --git a/start-redis.sh b/start-redis.sh old mode 100644 new mode 100755 index 7ddfcb8..fcafacc --- a/start-redis.sh +++ b/start-redis.sh @@ -4,6 +4,7 @@ REDIS_IMAGE=$1 REDIS_VERSION=$2 REDIS_PORT=$3 REDIS_CONTAINER_NAME=$4 +REDIS_REMOVE_CONTAINER=$5 if [ -z "$REDIS_VERSION" ]; then echo "Missing Redis version in the [redis-version] input. Received value: $REDIS_VERSION" @@ -11,5 +12,11 @@ if [ -z "$REDIS_VERSION" ]; then REDIS_VERSION='latest' fi -echo "Starting single-node Redis instance" -docker run --name $REDIS_CONTAINER_NAME --publish $REDIS_PORT:6379 --detach $REDIS_IMAGE:$REDIS_VERSION +DOCKER_RUN_ARGS="--name $REDIS_CONTAINER_NAME --publish $REDIS_PORT:6379 --detach $REDIS_IMAGE:$REDIS_VERSION" + +if [ "$REDIS_REMOVE_CONTAINER" == "true" ] ; then + DOCKER_RUN_ARGS+=" --rm" +fi + +echo "Starting single-node Redis instance: $DOCKER_RUN_ARGS" +docker run $DOCKER_RUN_ARGS