|
| 1 | +name: ci |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + branches: [ master ] |
| 6 | + |
| 7 | +jobs: |
| 8 | + test: |
| 9 | + runs-on: ubuntu-latest |
| 10 | + strategy: |
| 11 | + matrix: |
| 12 | + props: |
| 13 | + - Dockerfile: Dockerfile |
| 14 | + - Dockerfile: Dockerfile-alpine |
| 15 | + - Dockerfile: Dockerfile-centos |
| 16 | + platform: |
| 17 | + - linux/amd64 |
| 18 | + - linux/arm64 |
| 19 | + env: |
| 20 | + TEST_MATCH: Welcome to nginx! |
| 21 | + steps: |
| 22 | + - |
| 23 | + name: Checkout |
| 24 | + uses: actions/checkout@v2 |
| 25 | + - |
| 26 | + name: Detect host configuration |
| 27 | + run: | |
| 28 | + # NOTE: Docker host configuration determines the networking target for integration testing |
| 29 | + v=$(mount | grep "/run/docker.sock") |
| 30 | + TARGET_HOST= |
| 31 | +
|
| 32 | + if [ -n "$v" ]; then |
| 33 | + echo "Injected docker socket detected" |
| 34 | + TARGET_HOST="host.docker.internal" |
| 35 | + elif [ -S /var/run/docker.sock ]; then |
| 36 | + TARGET_HOST="localhost" |
| 37 | + else |
| 38 | + echo "No Docker socket detected, fail" |
| 39 | + exit 1 |
| 40 | + fi |
| 41 | + echo "TARGET_HOST=${TARGET_HOST}" >> $GITHUB_ENV |
| 42 | + - |
| 43 | + # Build and execute in multiple configurations: vanilla, with env overrides, with TLS enabled |
| 44 | + name: Build and test |
| 45 | + run: | |
| 46 | + # NOTE: docker qemu and buildx setup actions create a black hole for build cache layers, avoid unless pushing externally |
| 47 | + # Setup multi-arch platforms, noop if already installed for builder |
| 48 | + docker run --privileged --rm tonistiigi/binfmt --install arm64,amd64 |
| 49 | +
|
| 50 | + TARGET_PLATFORM=${{ matrix.platform }} |
| 51 | + TARGET_DOCKERFILE=${{ matrix.props.Dockerfile }} |
| 52 | +
|
| 53 | + # Since containers may or may not be against the same docker engine, create a matrix-unique tag name for outputs |
| 54 | + TAG_NAME="docker-nginx-${TARGET_DOCKERFILE}-${TARGET_PLATFORM}" |
| 55 | + # Formats as lowercase |
| 56 | + TAG_NAME=$(echo $TAG_NAME | tr '[:upper:]' '[:lower:]') |
| 57 | + # Removes slashes |
| 58 | + TAG_NAME=$(echo $TAG_NAME | sed 's/\///') |
| 59 | +
|
| 60 | + echo $TAG_NAME |
| 61 | +
|
| 62 | + docker buildx build --platform $TARGET_PLATFORM --iidfile $TAG_NAME -t $TAG_NAME -f $TARGET_DOCKERFILE . |
| 63 | +
|
| 64 | + # NOTE: multi-arch builds may not be accessible by docker tag, instead target by ID |
| 65 | + BUILD_SHA=$(cat ./$TAG_NAME) |
| 66 | +
|
| 67 | + # Remove sha256: from tag identifier |
| 68 | + BUILD_SHA=$(echo $BUILD_SHA | sed 's/sha256\://') |
| 69 | +
|
| 70 | + # Generate self-signed certificates |
| 71 | + mkdir -p certs |
| 72 | + openssl genrsa -out ./certs/ca.key 2048 |
| 73 | + openssl req -new -key ./certs/ca.key -out ./certs/ca.csr -subj '/CN=localhost' |
| 74 | + openssl x509 -req -days 365 -in ./certs/ca.csr -signkey ./certs/ca.key -out ./certs/ca.crt |
| 75 | +
|
| 76 | + # Run various configurations of containers |
| 77 | + CONTAINER_VANILLA=$(docker run --platform $TARGET_PLATFORM --rm -p 8080 -d $BUILD_SHA) |
| 78 | + CONTAINER_ENV_FILE=$(docker run --platform $TARGET_PLATFORM --rm -p 8080 -d --env-file ./.test.env $BUILD_SHA) |
| 79 | + CONTAINER_HTTPS=$(docker run --platform $TARGET_PLATFORM --rm -p 8080 -d -e SERVER_ENABLE_HTTPS=true -v $(pwd)/certs:/etc/nginx/certs:ro $BUILD_SHA) |
| 80 | +
|
| 81 | + # Retrieve dynamically-allocated host port |
| 82 | + VANILLA_PORT=$(docker inspect --format '{{ (index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort }}' $CONTAINER_VANILLA) |
| 83 | + ENV_FILE_PORT=$(docker inspect --format '{{ (index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort }}' $CONTAINER_ENV_FILE) |
| 84 | + HTTPS_PORT=$(docker inspect --format '{{ (index (index .NetworkSettings.Ports "8080/tcp") 0).HostPort }}' $CONTAINER_HTTPS) |
| 85 | +
|
| 86 | + # Wait for containers to boot (in background) |
| 87 | + sleep 5 |
| 88 | +
|
| 89 | + TARGET_HOST=${{ env.TARGET_HOST }} |
| 90 | + echo "HOSTING ${TARGET_HOST}" |
| 91 | +
|
| 92 | + # Check for nginx test page response |
| 93 | + curl ${TARGET_HOST}:${VANILLA_PORT} | grep "${{ env.TEST_MATCH }}" |
| 94 | + curl ${TARGET_HOST}:${ENV_FILE_PORT} | grep "${{ env.TEST_MATCH }}" |
| 95 | + curl -k https://${TARGET_HOST}:${HTTPS_PORT} | grep "${{ env.TEST_MATCH }}" |
| 96 | +
|
| 97 | + # Cleanup |
| 98 | + docker kill $CONTAINER_VANILLA |
| 99 | + docker kill $CONTAINER_ENV_FILE |
| 100 | + docker kill $CONTAINER_HTTPS |
| 101 | + docker rmi $BUILD_SHA |
0 commit comments