From 06e3323409b4649f774e5096aeed7024e39599d1 Mon Sep 17 00:00:00 2001 From: make-github-pseudonymous-again <5165674+make-github-pseudonymous-again@users.noreply.github.com> Date: Sat, 20 Apr 2024 16:47:28 +0200 Subject: [PATCH] :construction: progress: Refactor `netcat` loop as a composite action. --- .github/actions/is-reachable.yml | 52 ++++++++++++++++++++++++++ .github/workflows/ci:build.yml | 7 ++-- .github/workflows/ci:build:compose.yml | 7 ++-- .github/workflows/ci:build:image.yml | 10 +++-- .github/workflows/ci:test:deploy.yml | 7 ++-- 5 files changed, 70 insertions(+), 13 deletions(-) create mode 100644 .github/actions/is-reachable.yml diff --git a/.github/actions/is-reachable.yml b/.github/actions/is-reachable.yml new file mode 100644 index 000000000..2617d6fc7 --- /dev/null +++ b/.github/actions/is-reachable.yml @@ -0,0 +1,52 @@ +name: is-reachable +description: Checks if a given host is reachable + +inputs: + + host: + required: true + default: 'localhost' + + port: + required: true + + timeout: + required: true + default: 5 + + max-roundtrip: + required: true + default: 5 + + polling-interval: + required: true + default: 1 + + protocol: + required: true + default: 'tcp' + + ip-version: + required: true + default: 'any' + +runs: + + using: composite + + steps: + + - name: Wait for host to be reachable + + env: + TIMEOUT: ${{ inputs.timeout }} + MAX_ROUNDTRIP: ${{ inputs.max-roundtrip }} + HOST: ${{ inputs.host }} + PORT: ${{ inputs.port }} + POLLING_INTERVAL: ${{ inputs.polling-interval }} + PROTOCOL: ${{ inputs.protocol == 'udp' ? '-u' : '' }} + IP_VERSION: ${{ inputs.ip-version == '4' ? '-4' : inputs.ip-version == '6' ? '-6' : '' }} + + run: | + timeout "$TIMEOUT" bash -c \ + 'until nc -z -v -w"${MAX_ROUNDTRIP}" ${IP_VERSION} ${PROTOCOL} "${HOST}" "${PORT}"; do sleep "${POLLING_INTERVAL}"; done' diff --git a/.github/workflows/ci:build.yml b/.github/workflows/ci:build.yml index e3046fd27..a3fb762f6 100644 --- a/.github/workflows/ci:build.yml +++ b/.github/workflows/ci:build.yml @@ -185,9 +185,10 @@ jobs: echo "pid=$!" >> "$GITHUB_OUTPUT" - name: Wait for server port to be available - run: | - timeout 60 bash -c \ - 'until nc -z -v -w5 localhost 3000 ; do sleep 1; done' + uses: .github/actions/is-reachable + with: + timeout: 60 + port: 3000 - name: Call healthcheck endpoint run: | diff --git a/.github/workflows/ci:build:compose.yml b/.github/workflows/ci:build:compose.yml index 71b619e0d..6b632fbe2 100644 --- a/.github/workflows/ci:build:compose.yml +++ b/.github/workflows/ci:build:compose.yml @@ -97,9 +97,10 @@ jobs: 'until docker inspect --format "{{json .State.Health }}" "$(docker compose ps -q patient-db)" | jq -e ".Status == \"healthy\"" ; do sleep 1; done' - name: Wait for web port to be available - run: | - timeout 60 bash -c \ - 'until nc -z -v -w5 localhost 3000 ; do sleep 1; done' + uses: .github/actions/is-reachable + with: + timeout: 60 + port: 3000 - name: Wait for web container to be healthy run: | diff --git a/.github/workflows/ci:build:image.yml b/.github/workflows/ci:build:image.yml index 99f637e96..e4711b1cd 100644 --- a/.github/workflows/ci:build:image.yml +++ b/.github/workflows/ci:build:image.yml @@ -153,10 +153,12 @@ jobs: timeout 60 bash -c \ 'until docker inspect --format "{{json .State }}" server | jq -e ".Status == \"running\"" ; do sleep 1; done' - - name: Wait for server container 3000 port to be available - run: | - timeout 60 bash -c \ - 'until nc -z -v -w5 ${{ steps.server-container-ip-address.outputs.address }} 3000 ; do sleep 1; done' + - name: Wait for server container port to be available + uses: .github/actions/is-reachable + with: + timeout: 60 + host: ${{ steps.server-container-ip-address.outputs.address }} + port: 3000 - name: Show docker containers if: always() diff --git a/.github/workflows/ci:test:deploy.yml b/.github/workflows/ci:test:deploy.yml index da12f2129..5389f79f7 100644 --- a/.github/workflows/ci:test:deploy.yml +++ b/.github/workflows/ci:test:deploy.yml @@ -57,9 +57,10 @@ jobs: 'until docker inspect --format "{{json .State.Health }}" "$(docker compose ps -q patient-db)" | jq -e ".Status == \"healthy\"" ; do sleep 1; done' - name: Wait for web port to be available - run: | - timeout 60 bash -c \ - 'until nc -z -v -w5 localhost 3000 ; do sleep 1; done' + uses: .github/actions/is-reachable + with: + timeout: 60 + port: 3000 - name: Wait for web container to be healthy run: |