Add option to remove container when it exits

pull/15/head
Kien Pham 2023-07-25 15:36:40 -07:00
parent f63fe51625
commit 9c4929a470
No known key found for this signature in database
GPG Key ID: 499E6D16944677EC
3 changed files with 40 additions and 2 deletions

View File

@ -145,6 +145,31 @@ jobs:
- name: … - 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 ## License
MIT © [Supercharge](https://superchargejs.com) MIT © [Supercharge](https://superchargejs.com)

View File

@ -22,6 +22,11 @@ inputs:
description: "Name of the created container. Useful if you run multiple Redis containers" description: "Name of the created container. Useful if you run multiple Redis containers"
required: false required: false
default: 'redis' default: 'redis'
redis-remove-container:
description: "Remove container after container exit?"
required: false
type: boolean
default: false
runs: runs:
using: 'docker' using: 'docker'
@ -31,3 +36,4 @@ runs:
- ${{ inputs.redis-version }} - ${{ inputs.redis-version }}
- ${{ inputs.redis-port }} - ${{ inputs.redis-port }}
- ${{ inputs.redis-container-name }} - ${{ inputs.redis-container-name }}
- ${{ inputs.redis-remove-container }}

11
start-redis.sh Normal file → Executable file
View File

@ -4,6 +4,7 @@ REDIS_IMAGE=$1
REDIS_VERSION=$2 REDIS_VERSION=$2
REDIS_PORT=$3 REDIS_PORT=$3
REDIS_CONTAINER_NAME=$4 REDIS_CONTAINER_NAME=$4
REDIS_REMOVE_CONTAINER=$5
if [ -z "$REDIS_VERSION" ]; then if [ -z "$REDIS_VERSION" ]; then
echo "Missing Redis version in the [redis-version] input. Received value: $REDIS_VERSION" 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' REDIS_VERSION='latest'
fi fi
echo "Starting single-node Redis instance" DOCKER_RUN_ARGS="--name $REDIS_CONTAINER_NAME --publish $REDIS_PORT:6379 --detach $REDIS_IMAGE:$REDIS_VERSION"
docker run --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