Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/eficode/wait-for

./wait-for is a script to wait for another service to become available.
https://github.com/eficode/wait-for

automation bash bats ci docker script wait

Last synced: about 2 months ago
JSON representation

./wait-for is a script to wait for another service to become available.

Awesome Lists containing this project

README

        



wait-for


Wait for another service to become available

















GitHub release (latest by date)


Script file size






Docker Pulls


Docker Image Size (latest semver)



Example usage
·
Submit a PR

`./wait-for` is a script designed to synchronize services like docker containers. It is [sh](https://en.wikipedia.org/wiki/Bourne_shell) and [alpine](https://alpinelinux.org/) compatible. It was inspired by [vishnubob/wait-for-it](https://github.com/vishnubob/wait-for-it), but the core has been rewritten at [Eficode](http://eficode.com/) by [dsuni](https://github.com/dsuni) and [mrako](https://github.com/mrako).

The easiest way to get started using this tool is to include the `wait-for` file as part of your project. Then call this script as part of any automation script.

## Usage

### Locally

Download the `wait-for` file, either the latest from [`master`](https://raw.githubusercontent.com/eficode/wait-for/master/wait-for) or for a specific version check out the [Releases](https://github.com/eficode/wait-for/releases)-page.

With the file locally on your file system, you can directly invoke it.

```
./wait-for host:port|url [-t timeout] [-- command args]
-q | --quiet Do not output any status messages
-t TIMEOUT | --timeout=timeout Timeout in seconds, zero for no timeout
Defaults to 15 seconds
-v | --version Show the version of this tool
-- COMMAND ARGS Execute command with args after the test finishes
```

Alternatively, you could download the script and pipe it into `sh`:

```sh
$ wget -qO- https://raw.githubusercontent.com/eficode/wait-for/v2.2.3/wait-for | sh -s -- google.com:80 -- echo success
```

_Note: When using the latter option, make sure to pin the version by commit hash. Future releases could introduce non-backwards compatible changes and leaves you vulnerable to malicious users modifying this script in the future (as has e.g. [happened with Codecov](https://about.codecov.io/security-update/))._

### GitHub Actions

Similarly to how we piped the script into our shell covered in local usage, we can also use this in GitHub Actions, like so:

```yaml
- name: Wait for the database to start
run: wget -qO- https://raw.githubusercontent.com/eficode/wait-for/$WAIT_FOR_VERSION/wait-for | sh -s -- localhost:5132 -- echo "Database is up"
env:
WAIT_FOR_VERSION: 4df3f9262d84cab0039c07bf861045fbb3c20ab7 # v2.2.3
```

### Docker

We also publish a container to Docker Hub at [`eficode/wait-for`](https://hub.docker.com/r/eficode/wait-for), where we publish under the tag `latest` what's in `master` and tags for each release. (Only tags newer van v2.2.3 are available.) You can use the container like this:

```bash
$ docker run --rm eficode/wait-for google.com:80 -- echo success
success
```

_Note: We will refrain from changes in the container we are publishing from triggering releases, as our primary distributable remains the script itself. One exception might be urgent security fixes._

## Examples

To check if [www.eficode.com](https://www.eficode.com) is available:

```
$ ./wait-for www.eficode.com:80 -- echo "Eficode site is up"
Eficode site is up
```

To wait for database container to become available:

```yml
version: "3"

services:
db:
image: postgres:9.4

backend:
build: backend
command: sh -c './wait-for db:5432 -- npm start'
depends_on:
- db
```

To check if [https://www.eficode.com](https://www.eficode.com) is available over HTTPS:

```
$ ./wait-for https://www.eficode.com -- echo "Eficode is accessible over HTTPS"
Eficode is accessible over HTTPS
```

To wait for your API service to become available:

```yml
version: "3"

services:
api:
image: nginx

tests:
build: .
command: sh -c './wait-for http://api -- echo "The api is up! Let's use it"'
depends_on:
- api
```

## Testing

Testing is done using [bats](https://github.com/bats-core/bats-core), which we install using [npm](https://docs.npmjs.com/downloading-and-installing-node-js-and-npm).

For reproducibility, we run our tests inside Docker, such that we have control over the version of [bash]() we're testing against.

```bash
docker build --target test-env --tag wait-for .
docker run --rm -t wait-for
```

## Contributing

When creating PRs, please style your commit messages according to [conventional commit](https://www.conventionalcommits.org/en/v1.0.0/), you can use a tool like [commitizen](https://github.com/commitizen/cz-cli) to guide you. We will automatically infer the changelog from your commits. Alternatively, we can squash all commits when merging and update the commit message.

This project strongly prefers maintaining backwards compatibility, therefore some obvious "fixes" might not be accepted.

Also, please include or update the test cases whenever possible by extending `wait-for.bats`.

## Note

Make sure netcat is installed in your Dockerfile before running the command if you test over plain TCP.

```
RUN apt-get -q update && apt-get -qy install netcat
```

https://stackoverflow.com/questions/44663180/docker-why-does-wait-for-always-time-out

If you are connecting over HTTP, then you will need to have wget available.