Skip to content

Commit

Permalink
Run a smoke test on the container
Browse files Browse the repository at this point in the history
  • Loading branch information
thewilkybarkid committed Dec 4, 2023
1 parent 45a480c commit de8290f
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
1 change: 1 addition & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
/.github/
/dist/
/node_modules/
/scripts/
63 changes: 63 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@ concurrency:
cancel-in-progress: ${{ github.ref != 'refs/heads/main' }}

jobs:
build-image:
name: 'Build image'
runs-on: ubuntu-22.04
timeout-minutes: 30
outputs:
image: ${{ steps.build.outputs.imageid }}

steps:
- name: 'Checkout code'
uses: actions/checkout@v4.1.1

- name: 'Set up Docker Build'
uses: docker/setup-buildx-action@v3.0.0

- name: 'Build image'
id: build
uses: docker/build-push-action@v5.1.0
with:
outputs: type=docker,dest=/tmp/image.tar
cache-from: type=gha,ignore-error=true
cache-to: type=gha,mode=max,ignore-error=true
target: prod

- name: 'Upload build'
uses: actions/upload-artifact@v3.1.3
with:
name: image
path: /tmp/image.tar

format:
name: 'Format'
runs-on: ubuntu-22.04
Expand Down Expand Up @@ -56,6 +85,40 @@ jobs:
- name: 'Run the linter'
run: npx eslint . --max-warnings 0

test-smoke:
name: 'Smoke test'
runs-on: ubuntu-22.04
timeout-minutes: 30
needs:
- build-image

services:
redis:
image: redis
options: >-
--health-cmd "redis-cli ping"
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- name: 'Checkout code'
uses: actions/checkout@v4.1.1

- name: 'Download image'
uses: actions/download-artifact@v3.0.2
with:
name: image
path: /tmp

- name: 'Load image'
run: docker load --input /tmp/image.tar

- name: 'Run smoke test'
run: scripts/smoke-test.sh ${{ needs.build-image.outputs.image }}
env:
REDIS_PORT: 6379

typecheck:
name: 'Typecheck'
runs-on: ubuntu-22.04
Expand Down
11 changes: 10 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
.PHONY: build check fix format lint start start-services typecheck
.PHONY: build build-image check fix format lint smoke-test start start-services typecheck

IMAGE_TAG=prereview-coar-notify

node_modules: package.json package-lock.json
npm install
Expand All @@ -7,6 +9,9 @@ node_modules: package.json package-lock.json
build: node_modules
npx tsc --project tsconfig.build.json

build-image:
docker build --target prod --tag ${IMAGE_TAG} .

check: format lint typecheck

fix: node_modules
Expand All @@ -22,6 +27,10 @@ lint: node_modules
typecheck: node_modules
npx tsc --noEmit

smoke-test: SHELL := /usr/bin/env bash
smoke-test: build-image start-services
REDIS_PORT=$(shell docker compose port redis 6379 | awk -F":" '{print $$2}') scripts/smoke-test.sh ${IMAGE_TAG}

start: node_modules start-services
REDIS_URI=redis://$(shell docker compose port redis 6379) npx tsx --require dotenv/config src

Expand Down
35 changes: 35 additions & 0 deletions scripts/smoke-test.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/usr/bin/env bash
set -e

if [ "$#" -ne 1 ]; then
echo "Test that a container image sees a 'healthy' state"
echo
echo "Usage: $0 IMAGE"
echo "Example: $0 sha256:fe8a5ea609e0f377bf4d6aa96f9dd9743caa7048a1c93c9632661f774e20f308"
exit 1
fi

function finish() {
echo "Stopping the container"
docker stop "$container"
echo "Logs from the container"
docker logs "$container"
echo "Removing the container"
docker rm "$container"
}

trap finish EXIT

echo "Starting the container"
container=$(docker run --env SLACK_ACCESS_TOKEN=token --env REDIS_URI="redis://host.docker.internal:$REDIS_PORT" --add-host=host.docker.internal:host-gateway --detach "$1")

timeout --foreground 20 bash << EOT
while true; do
current=\$(docker inspect "${container}" | jq --raw-output '.[0].State.Health.Status')
echo "${container} is in state: \${current}"
if [ "\$current" == "healthy" ]; then
break
fi
sleep 1
done
EOT

0 comments on commit de8290f

Please sign in to comment.