From 69ae7a0f9e831428fc3f0d623de87ca052c8e986 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/action.yml | 52 +++++++++++++++++++++++++ .github/workflows/ci:build.yml | 8 ++-- .github/workflows/ci:build:compose.yml | 7 ++-- .github/workflows/ci:build:image.yml | 11 ++++-- .github/workflows/ci:test:deploy.yml | 7 ++-- 5 files changed, 72 insertions(+), 13 deletions(-) create mode 100644 .github/actions/is-reachable/action.yml diff --git a/.github/actions/is-reachable/action.yml b/.github/actions/is-reachable/action.yml new file mode 100644 index 000000000..6d271bb16 --- /dev/null +++ b/.github/actions/is-reachable/action.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_FLAGS: ${{ inputs.protocol == 'udp' && '-u' || '' }} + IP_VERSION_FLAGS: ${{ inputs.ip-version == '4' && '-4' || (inputs.ip-version == '6' && '-6' || '') }} + + run: | + timeout "$TIMEOUT" bash -c \ + 'until nc -z -v -w"${MAX_ROUNDTRIP}" ${IP_VERSION_FLAGS} ${PROTOCOL_FLAGS} "${HOST}" "${PORT}"; do sleep "${POLLING_INTERVAL}"; done' diff --git a/.github/workflows/ci:build.yml b/.github/workflows/ci:build.yml index 6ad064f70..fcd46c817 100644 --- a/.github/workflows/ci:build.yml +++ b/.github/workflows/ci:build.yml @@ -90,6 +90,7 @@ jobs: sparse-checkout: | scripts/healthcheck.cjs scripts/assert-replica-set.js + ./.github/actions/is-reachable/action.yml sparse-checkout-cone-mode: false - name: Load build 💽 @@ -185,9 +186,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..1fb1f92e0 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 6080c4e59..df054258e 100644 --- a/.github/workflows/ci:build:image.yml +++ b/.github/workflows/ci:build:image.yml @@ -85,6 +85,7 @@ jobs: with: sparse-checkout: | scripts/assert-replica-set.js + ./.github/actions/is-reachable/action.yml sparse-checkout-cone-mode: false - name: Get server image URL @@ -162,10 +163,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..bc97f0242 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: |