From b098d1070a1c0f00caa637263c0a1292053d46d7 Mon Sep 17 00:00:00 2001 From: Mirko Mollik Date: Sun, 1 Sep 2024 18:51:05 +0200 Subject: [PATCH] fix: test env files with containers in cicd Signed-off-by: Mirko Mollik --- .github/workflows/ci.yml | 8 +++++- apps/demo/Dockerfile | 4 +++ apps/holder-app/Dockerfile | 3 +++ apps/issuer-frontend/Dockerfile | 3 +++ apps/verifier-frontend/Dockerfile | 3 +++ deploys/demo/docker-compose.yml | 6 +++++ deploys/holder/docker-compose.yml | 7 ++++- deploys/issuer/docker-compose.yml | 6 +++++ deploys/keycloak/docker-compose.yml | 12 ++++++--- deploys/test.sh | 41 +++++++++++++++++++++++++++++ deploys/verifier/docker-compose.yml | 6 +++++ docs/Dockerfile | 4 +++ 12 files changed, 97 insertions(+), 6 deletions(-) create mode 100755 deploys/test.sh diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index eba7d0bc..46bbf4ad 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -47,7 +47,7 @@ jobs: - name: Add entry to /etc/hosts run: echo "127.0.0.1 host.testcontainers.internal" | sudo tee -a /etc/hosts - - name: Lint, test, build, e2e + - name: Lint, test, container, e2e run: INPUT_GITHUB_TOKEN=${{ secrets.GITHUB_TOKEN }} pnpm exec nx affected -t lint test container e2e # comment out since the current e2e tests do not produce any artifacts # - name: Upload coverage @@ -76,6 +76,12 @@ jobs: path: tmp/logs retention-days: 30 + # validate if the .env.example files in the deploys folder are up to date and that the containers can be health checked and started + - name: Validate deploy environment + run: + cd deploys + ./test.sh + - name: Check if testcontainer logs exist id: check_testcontainer_logs run: echo "exists=$(if [ -d tmp/logs ]; then echo true; else echo false; fi)" >> $GITHUB_ENV diff --git a/apps/demo/Dockerfile b/apps/demo/Dockerfile index d4b64c50..6aa891d1 100644 --- a/apps/demo/Dockerfile +++ b/apps/demo/Dockerfile @@ -1,4 +1,8 @@ FROM docker.io/nginx:stable-alpine + +# Install wget +RUN apk --no-cache add wget + COPY dist/apps/demo/* /usr/share/nginx/html/ RUN echo "server {" > /etc/nginx/conf.d/default.conf && \ echo " listen 80;" >> /etc/nginx/conf.d/default.conf && \ diff --git a/apps/holder-app/Dockerfile b/apps/holder-app/Dockerfile index 8b2bafec..29e71cfb 100644 --- a/apps/holder-app/Dockerfile +++ b/apps/holder-app/Dockerfile @@ -1,5 +1,8 @@ FROM docker.io/nginx:stable-alpine +# Install wget +RUN apk --no-cache add wget + # Copy application files and the startup script with permissions COPY dist/apps/holder-app/* /usr/share/nginx/html/ COPY --chmod=755 apps/holder-app/startup.sh /usr/local/bin/startup.sh diff --git a/apps/issuer-frontend/Dockerfile b/apps/issuer-frontend/Dockerfile index 8bad8680..e5718618 100644 --- a/apps/issuer-frontend/Dockerfile +++ b/apps/issuer-frontend/Dockerfile @@ -1,5 +1,8 @@ FROM docker.io/nginx:stable-alpine +# Install wget +RUN apk --no-cache add wget + # Copy application files and the startup script with permissions COPY dist/apps/issuer-frontend/* /usr/share/nginx/html/ COPY --chmod=755 apps/issuer-frontend/startup.sh /usr/local/bin/startup.sh diff --git a/apps/verifier-frontend/Dockerfile b/apps/verifier-frontend/Dockerfile index 9e492ccb..a995b035 100644 --- a/apps/verifier-frontend/Dockerfile +++ b/apps/verifier-frontend/Dockerfile @@ -1,5 +1,8 @@ FROM docker.io/nginx:stable-alpine +# Install wget +RUN apk --no-cache add wget + # Copy application files and the startup script with permissions COPY dist/apps/verifier-frontend/* /usr/share/nginx/html/ COPY --chmod=755 apps/verifier-frontend/startup.sh /usr/local/bin/startup.sh diff --git a/deploys/demo/docker-compose.yml b/deploys/demo/docker-compose.yml index f86ab5db..ab4556c8 100644 --- a/deploys/demo/docker-compose.yml +++ b/deploys/demo/docker-compose.yml @@ -5,6 +5,12 @@ services: - ./config/config.json:/usr/share/nginx/html/assets/config.json ports: - $PORT:80 + healthcheck: + test: ['CMD', 'wget', '--spider', '-q', 'http://127.0.0.1/index.html'] + interval: 10s + timeout: 10s + retries: 3 + start_period: 5s volumes: verifier: diff --git a/deploys/holder/docker-compose.yml b/deploys/holder/docker-compose.yml index a02d2e60..705a5f2b 100644 --- a/deploys/holder/docker-compose.yml +++ b/deploys/holder/docker-compose.yml @@ -27,7 +27,6 @@ services: - OIDC_PUBLIC_CLIENT_ID=${OIDC_PUBLIC_CLIENT_ID} - OIDC_ADMIN_CLIENT_ID=${OIDC_ADMIN_CLIENT_ID} - OIDC_ADMIN_CLIENT_SECRET=${OIDC_ADMIN_CLIENT_SECRET} - - OIDC_PUBLIC_CLIENT_ID=${OIDC_PUBLIC_CLIENT_ID} - DB_HOST=${DB_HOST} - DB_PORT=${DB_PORT} - DB_USERNAME=${DB_USERNAME} @@ -53,6 +52,12 @@ services: - OIDC_ALLOW_HTTP=${OIDC_ALLOW_HTTP} ports: - $FRONTEND_PORT:80 + healthcheck: + test: ['CMD', 'wget', '--spider', '-q', 'http://127.0.0.1/index.html'] + interval: 10s + timeout: 10s + retries: 3 + start_period: 5s volumes: db: diff --git a/deploys/issuer/docker-compose.yml b/deploys/issuer/docker-compose.yml index 668caacb..0f716efd 100644 --- a/deploys/issuer/docker-compose.yml +++ b/deploys/issuer/docker-compose.yml @@ -39,6 +39,12 @@ services: - OIDC_CLIENT_SECRET=$FRONTEND_OIDC_CLIENT_SECRET ports: - $FRONTEND_PORT:80 + healthcheck: + test: ['CMD', 'wget', '--spider', '-q', 'http://127.0.0.1/index.html'] + interval: 10s + timeout: 10s + retries: 3 + start_period: 5s db: restart: unless-stopped diff --git a/deploys/keycloak/docker-compose.yml b/deploys/keycloak/docker-compose.yml index 66c63ab0..eb0e65c2 100644 --- a/deploys/keycloak/docker-compose.yml +++ b/deploys/keycloak/docker-compose.yml @@ -5,7 +5,7 @@ services: healthcheck: test: ['CMD', 'pg_isready', '-q', '-d', 'postgres', '-U', '$KC_POSTGRES_USER'] - timeout: 45s + timeout: 10s interval: 10s retries: 10 volumes: @@ -53,11 +53,15 @@ services: networks: - keycloak-net healthcheck: - test: ['CMD', 'curl', '-f', 'http://localhost:8080/health'] - interval: 30s + test: + [ + 'CMD-SHELL', + "exec 3<>/dev/tcp/127.0.0.1/8080;echo -e \"GET /health/ready HTTP/1.1\r\nhost: http://localhost\r\nConnection: close\r\n\r\n\" >&3;grep \"HTTP/1.1 200 OK\" <&3", + ] + interval: 10s timeout: 10s retries: 3 - start_period: 2m + start_period: 10s networks: keycloak-net: diff --git a/deploys/test.sh b/deploys/test.sh new file mode 100755 index 00000000..e7ef874f --- /dev/null +++ b/deploys/test.sh @@ -0,0 +1,41 @@ +#!/bin/bash +# this script is used to test the .env.example files and if the containers can start healthy with the provided configuration. It does not execute tests. +folders=("keycloak" "holder" "issuer" "verifier") + +for folder in "${folders[@]}"; do + echo "Processing folder: $folder" + cd $folder + cp .env.example .env + docker compose up -d + echo "Waiting for containers to be healthy in $folder..." + + check_health() { + containers=$(docker compose ps -q) + for container in $containers; do + health_status=$(docker inspect --format='{{.State.Health.Status}}' $container 2>/dev/null || echo "unhealthy") + if [ "$health_status" != "healthy" ]; then + return 1 + fi + done + return 0 + } + + for i in {1..4}; do + if check_health; then + echo "All containers in $folder are healthy." + docker compose down + break + fi + echo "Waiting for containers to be healthy in $folder... ($i/3)" + sleep 5 + done + + if ! check_health; then + echo "Containers in $folder did not become healthy in time." + exit 1 + fi + + cd .. +done + +echo "All containers in all folders are healthy." diff --git a/deploys/verifier/docker-compose.yml b/deploys/verifier/docker-compose.yml index 6d54d9fe..2b4cf321 100644 --- a/deploys/verifier/docker-compose.yml +++ b/deploys/verifier/docker-compose.yml @@ -38,6 +38,12 @@ services: - OIDC_CLIENT_SECRET=$FRONTEND_OIDC_CLIENT_SECRET ports: - $FRONTEND_PORT:80 + healthcheck: + test: ['CMD', 'wget', '--spider', '-q', 'http://127.0.0.1/index.html'] + interval: 10s + timeout: 10s + retries: 3 + start_period: 5s db: restart: unless-stopped diff --git a/docs/Dockerfile b/docs/Dockerfile index d5123e5e..8f555c88 100644 --- a/docs/Dockerfile +++ b/docs/Dockerfile @@ -1,4 +1,8 @@ FROM docker.io/nginx:stable-alpine + +# Install wget +RUN apk --no-cache add wget + COPY site/ /usr/share/nginx/html/ EXPOSE 80 CMD ["nginx", "-g", "daemon off;"]