Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 14 additions & 13 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ Downloads and installs the Imposter mock server.
```

### 2. Start Mocks (`start-mocks`)
Starts the Imposter mock server in the background, and waits for it to be ready.
Starts the Imposter mock server in the background. The step returns only once the server is healthy, so the next step can hit it straight away.

```yaml
- uses: imposter-project/imposter-github-action/start-mocks@v1
Expand All @@ -30,32 +30,30 @@ Starts the Imposter mock server in the background, and waits for it to be ready.
engine-type: 'docker' # default: 'docker'
# Optional: Whether to recursively scan the config directory
recursive-config-scan: 'false' # default: 'false'
# Optional: Write the started mock's ID to this file, to stop it later by ID
mock-id-file: 'mock-id.txt' # default: '' (don't write an ID file)
```

> [!NOTE]
> For `version`, choose from the Imposter [releases](https://github.com/outofcoffee/imposter/releases) page.

<details>
<summary>Advanced configuration options</summary>

- `auto-restart`: Whether to automatically restart when configuration changes (default: false)
- `max-attempts`: Maximum number of attempts to check if the server is ready (default: 30)
- `retry-interval`: Interval in seconds between retry attempts (default: 1)

</details>

#### Outputs
- `base-url`: Base URL of the mock server (e.g. `http://localhost:8080`)

### 3. Stop Mocks (`stop-mocks`)
Stops all running Imposter mock servers, regardless of engine type.
Stops a specific mock by ID, or every running mock. With no inputs, it stops them all.

```yaml
- uses: imposter-project/imposter-github-action/stop-mocks@v1
with:
# Optional: Stop one specific mock by its ID
mock-id: 'abc123' # default: '' (stop all mocks)
# Optional: Stop the mock whose ID was written to this file by start-mocks
mock-id-file: 'mock-id.txt' # default: '' (stop all mocks)
```

> [!NOTE]
> The `engine-type` input is deprecated and ignored; all managed mocks are stopped regardless of engine type.
> Provide at most one of `mock-id` or `mock-id-file`. The `engine-type` input is deprecated and ignored.

## Sample Workflow

Expand Down Expand Up @@ -91,16 +89,19 @@ jobs:
engine-type: 'docker' # Optional: specify engine type
version: '1.2.3' # Optional: specify engine version
recursive-config-scan: 'true' # Optional: scan config directory recursively
mock-id-file: 'mock-id.txt' # Optional: record the mock ID to stop it later

# Your test steps here
- name: Run Tests
run: |
# The mock server is available at ${{ steps.start-mocks.outputs.base-url }}
echo "Running tests against mock server at ${{ steps.start-mocks.outputs.base-url }}"

# Stop mock server
# Stop mock server (the exact one we started)
- name: Stop Mocks
uses: imposter-project/imposter-github-action/stop-mocks@v1
with:
mock-id-file: 'mock-id.txt'
```

## Configuration
Expand Down
77 changes: 42 additions & 35 deletions start-mocks/action.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: 'Start Imposter Mocks'
description: 'Starts the Imposter mock server in the background'
description: 'Starts the Imposter mock server in the background and waits for it to be healthy'

inputs:
config-dir:
Expand All @@ -22,18 +22,22 @@ inputs:
description: 'Whether to recursively scan the config directory for mock configurations'
required: false
default: 'false'
mock-id-file:
description: 'Optional path to a file where the started mock ID will be written, for later use with the stop-mocks mock-id-file input.'
required: false
default: ''
auto-restart:
description: 'Whether to automatically restart the mock server when configuration changes'
description: 'Deprecated and ignored. The mock now runs detached, which is incompatible with auto-restart.'
required: false
default: 'false'
deprecationMessage: "The 'auto-restart' input is deprecated and no longer used; the mock now runs detached, which is incompatible with auto-restart."
max-attempts:
description: 'Maximum number of attempts to check if the server is ready'
description: 'Deprecated and ignored. The CLI now waits for the healthcheck before returning (--detach=healthy).'
required: false
default: '30'
deprecationMessage: "The 'max-attempts' input is deprecated and no longer used; the CLI now waits for the healthcheck before returning."
retry-interval:
description: 'Interval in seconds between retry attempts'
description: 'Deprecated and ignored. The CLI now waits for the healthcheck before returning (--detach=healthy).'
required: false
default: '1'
deprecationMessage: "The 'retry-interval' input is deprecated and no longer used; the CLI now waits for the healthcheck before returning."

outputs:
base-url:
Expand All @@ -43,41 +47,44 @@ outputs:
runs:
using: "composite"
steps:
- name: Warn on deprecated inputs
if: ${{ inputs.auto-restart != '' || inputs.max-attempts != '' || inputs.retry-interval != '' }}
shell: bash
run: echo "::warning title=Deprecated input::The 'auto-restart', 'max-attempts' and 'retry-interval' inputs of start-mocks are deprecated and no longer used; the mock now starts detached and the CLI waits for the healthcheck before returning."

- name: Pull Imposter engine
shell: bash
env:
ENGINE_TYPE: ${{ inputs.engine-type }}
VERSION: ${{ inputs.version }}
CONFIG_DIR: ${{ inputs.config-dir }}
run: |
echo "Pulling ${{ inputs.engine-type }} engine"
if [ ! -z "${{ inputs.version }}" ]; then
imposter engine pull --engine-type ${{ inputs.engine-type }} --version ${{ inputs.version }} --config "${{ inputs.config-dir }}/.imposter.yaml"
echo "Pulling $ENGINE_TYPE engine"
if [ -n "$VERSION" ]; then
imposter engine pull --engine-type "$ENGINE_TYPE" --version "$VERSION" --config "$CONFIG_DIR/.imposter.yaml"
else
imposter engine pull --engine-type ${{ inputs.engine-type }} --config "${{ inputs.config-dir }}/.imposter.yaml"
imposter engine pull --engine-type "$ENGINE_TYPE" --config "$CONFIG_DIR/.imposter.yaml"
fi

- name: Start Imposter server
shell: bash
env:
PORT: ${{ inputs.port }}
ENGINE_TYPE: ${{ inputs.engine-type }}
VERSION: ${{ inputs.version }}
CONFIG_DIR: ${{ inputs.config-dir }}
RECURSIVE_CONFIG_SCAN: ${{ inputs.recursive-config-scan }}
MOCK_ID_FILE: ${{ inputs.mock-id-file }}
run: |
# Build command with optional version and engine type
cmd="imposter up --port ${{ inputs.port }} --engine-type ${{ inputs.engine-type }}"
if [ ! -z "${{ inputs.version }}" ]; then
cmd="$cmd --version ${{ inputs.version }}"
# Start the mock detached; --detach=healthy returns once the healthcheck passes.
args=(up --port "$PORT" --engine-type "$ENGINE_TYPE" --detach=healthy --recursive-config-scan="$RECURSIVE_CONFIG_SCAN")
if [ -n "$VERSION" ]; then
args+=(--version "$VERSION")
fi
if [ -n "$MOCK_ID_FILE" ]; then
args+=(--id-file "$MOCK_ID_FILE")
fi
cmd="$cmd --recursive-config-scan=${{ inputs.recursive-config-scan }} --auto-restart=${{ inputs.auto-restart }} --config \"${{ inputs.config-dir }}/.imposter.yaml\" \"${{ inputs.config-dir }}\""

# Start the server
eval "$cmd" &

# Wait for server to be ready
max_attempts=${{ inputs.max-attempts }}
attempt=1
health_check_url="http://localhost:${{ inputs.port }}/system/status"
echo "Waiting for Imposter server to start at: $health_check_url"
until curl -s -o /dev/null -w "%{http_code}" "$health_check_url" | grep -q "200"; do
if [ $attempt -ge $max_attempts ]; then
echo "Timeout waiting for Imposter server to start"
exit 1
fi
echo "Waiting for Imposter server to start (attempt $attempt/$max_attempts)..."
sleep ${{ inputs.retry-interval }}
attempt=$((attempt + 1))
done
echo "Imposter server is ready"
args+=(--config "$CONFIG_DIR/.imposter.yaml" "$CONFIG_DIR")

imposter "${args[@]}"
echo "Imposter server is ready at http://localhost:$PORT"
26 changes: 24 additions & 2 deletions stop-mocks/action.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,15 @@
name: 'Stop Imposter Mocks'
description: 'Stops all running Imposter mock servers'
description: 'Stops a specific Imposter mock by ID, or all running mocks'

inputs:
mock-id:
description: 'Optional ID of a specific mock to stop. If omitted (and mock-id-file is unset), all managed mocks are stopped.'
required: false
default: ''
mock-id-file:
description: 'Optional path to a file containing the ID of the mock to stop, as written by the start-mocks mock-id-file input.'
required: false
default: ''
engine-type:
description: 'Deprecated and ignored. All managed mocks are stopped regardless of engine type.'
required: false
Expand All @@ -17,4 +25,18 @@ runs:

- name: Stop Imposter server
shell: bash
run: imposter down --all
env:
MOCK_ID: ${{ inputs.mock-id }}
MOCK_ID_FILE: ${{ inputs.mock-id-file }}
run: |
if [ -n "$MOCK_ID" ] && [ -n "$MOCK_ID_FILE" ]; then
echo "::error title=Conflicting inputs::Specify only one of 'mock-id' or 'mock-id-file', not both."
exit 1
fi
if [ -n "$MOCK_ID" ]; then
imposter down "$MOCK_ID"
elif [ -n "$MOCK_ID_FILE" ]; then
imposter down --id-file "$MOCK_ID_FILE"
else
imposter down --all
fi
Loading