Skip to content

Commit

Permalink
Switch to use harness-docker as a parent harness (#31)
Browse files Browse the repository at this point in the history
While workspace only supports the harnessLayers atm and not auto
configuration of them on `ws create`, it'd still be a big help to avoid
all the duplication and upgrades needed to files harness-docker also
manages.

One change though here is to use a multi-stage build instead of
intermediate layers for building an nginx image for pipeline/production
  • Loading branch information
andytson-inviqa authored Jul 25, 2023
1 parent 54f8fd5 commit 0b4292e
Show file tree
Hide file tree
Showing 83 changed files with 612 additions and 2,021 deletions.
43 changes: 43 additions & 0 deletions .ci/Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
pipeline {
agent none
environment {
MY127WS_KEY = credentials('base-my127ws-key-20190523')
}
options {
buildDiscarder(logRotator(daysToKeepStr: '30'))
}
triggers { cron(env.BRANCH_NAME ==~ /^\d+\.\d+\.x$/ ? 'H H(2-6) * * 1' : '') }
stages {
stage('BuildAndTest') {
parallel {
stage('Test (mode=static)') {
agent { label "linux-amd64-preview" }
steps { sh './.ci/test static' }
post {
always {
cleanWs()
}
}
}
stage('Test (mode=dynamic)') {
agent { label "linux-amd64-preview" }
steps { sh './.ci/test dynamic' }
post {
always {
cleanWs()
}
}
}
stage('Test (mode=dynamic, sync=mutagen)') {
agent { label "linux-amd64-preview" }
steps { sh './.ci/test dynamic mutagen' }
post {
always {
cleanWs()
}
}
}
}
}
}
}
24 changes: 24 additions & 0 deletions .ci/quality
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
set -e -o pipefail

ANALYSE_PATH="${1-}"

if ! docker image ls -a --format '{{ print .Repository ":" .Tag }}' | grep -q koalaman/shellcheck-alpine:stable; then
docker pull koalaman/shellcheck-alpine:stable
fi
if ! docker image ls -a --format '{{ print .Repository ":" .Tag }}' | grep -q hadolint/hadolint:latest; then
docker pull hadolint/hadolint:latest-alpine
fi

run_shellcheck()
{
docker run --rm --volume "$(pwd):/app" koalaman/shellcheck-alpine:stable /bin/sh -c "find '/app/$ANALYSE_PATH' -type f ! -path '*/.git/*' ! -path '/app/tmp-test/*' ! -name '*.twig' -and \( -name "*.sh" -or -perm -0111 \) -print -exec shellcheck --exclude=SC1008,SC1091 {} +"
}

run_hadolint()
{
docker run --rm --volume "$(pwd):/app" hadolint/hadolint:latest-alpine /bin/sh -c "find '/app/$ANALYSE_PATH' -type f -name '*Dockerfile' ! -path '*/.git/*' ! -path '/app/tmp-test/*' ! -path '/app/test/*' ! -name '*.orig' -print -exec hadolint --ignore DL3003 --ignore DL3008 --ignore DL3016 --ignore DL3018 --ignore SC1091 --ignore DL3002 {} +"
}

run_shellcheck
run_hadolint
2 changes: 1 addition & 1 deletion .ci/sample-dynamic/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"scripts": {
"dev": "while true; do sleep 10000; done",
"start": "while true; do sleep 10000; done",
"build": "exit 0",
"build": "mkdir build && echo 'hello world' > build/index.html",
"test": "exit 0"
}
}
8 changes: 2 additions & 6 deletions .ci/sample-dynamic/workspace.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
workspace('ci-node-spa-sample-dynamic'):
description: generated local workspace for ci-node-spa-sample.
harness: inviqa/node-spa
harnessLayers:
- https://github.com/inviqa/harness-docker/archive/refs/tags/0.2.0-alpha1.tar.gz

attribute('app.build'): dynamic

attribute('aws.bucket'): null
attribute('app.repository'): null
attribute('aws.id'): null
attribute('aws.key'): null
2 changes: 1 addition & 1 deletion .ci/sample-static/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
"scripts": {
"dev": "while true; do sleep 10000; done",
"start": "while true; do sleep 10000; done",
"build": "mkdir build",
"build": "mkdir build && echo 'hello world' > build/index.html",
"test": "exit 0"
}
}
8 changes: 2 additions & 6 deletions .ci/sample-static/workspace.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,6 @@
workspace('ci-node-spa-sample-static'):
description: generated local workspace for ci-node-spa-sample.
harness: inviqa/node-spa
harnessLayers:
- https://github.com/inviqa/harness-docker/archive/refs/tags/0.2.0-alpha1.tar.gz

attribute('app.build'): static

attribute('aws.bucket'): null
attribute('app.repository'): null
attribute('aws.id'): null
attribute('aws.key'): null
164 changes: 164 additions & 0 deletions .ci/test
Original file line number Diff line number Diff line change
@@ -0,0 +1,164 @@
#!/bin/bash

set -e
set -x

export path_test=".ci/tmp-test"

export TEST_MODE="${TEST_MODE:-full}"
export REUSE_EXISTING_WORKSPACE="${REUSE_EXISTING_WORKSPACE:-no}"
export TEARDOWN_ENVIRONMENT="${TEARDOWN_ENVIRONMENT:-yes}"

function main()
{
local mode="$1"
local sync="${2:-}"

local sync_path=""
if [[ -n "$sync" ]]; then
sync_path="-${sync}"
fi
export path_test=".ci/tmp-test-${mode}${sync_path}"

if [[ "$TEARDOWN_ENVIRONMENT" == "yes" ]]; then
trap 'clean' EXIT
fi

if [[ "$mode" == "static" ]]; then
export MY127WS_ENV=pipeline
fi

if [[ "$REUSE_EXISTING_WORKSPACE" != "yes" ]]; then
# Test default mode of static or dynamic harnesses
# For dynamic this means a mountpoint for the application code
# or mutagen
setup "$mode"
if [[ "$sync" == "" ]]; then
setup_dynamic_mountpoint
elif [[ "$sync" == "mutagen" ]]; then
setup_dynamic_mutagen
fi
prepare_environment
fi

if [[ "$TEST_MODE" == "quality" || "$TEST_MODE" == "full" ]]; then
test_harness_quality "$mode" "$sync"
fi
if [[ "$TEST_MODE" == "acceptance" || "$TEST_MODE" == "full" ]]; then
test_harness_acceptance "$mode" "$sync"
fi
if [[ "$TEARDOWN_ENVIRONMENT" == "yes" ]]; then
clean
fi
}

function test_harness_quality()
{
.ci/quality "${path_test}/.my127ws"
}

function test_harness_acceptance()
(
local mode="$1"
local sync="$2"

install_environment
run_project_quality_tests
check_environment_started "$mode"
run_project_acceptance_tests
restart_environment
check_environment_started "$mode"
)

function setup()
(
local mode="$1"

if [ -d "${path_test}" ]; then
rm -rf "${path_test}"
fi

cp -ap ".ci/sample-${mode}" "${path_test}"

# download parent harness layers
pushd "${path_test}" >/dev/null
ws harness download
popd >/dev/null

rsync --archive --exclude="**/.git" --exclude="**/.ci" ./ "${path_test}/.my127ws/"
)

function prepare_environment()
(
cd "${path_test}"
ws harness prepare
)

function install_environment()
(
cd "${path_test}"
ws install
)

function check_environment_started()
(
local mode="$1"

cd "${path_test}"

project="ci-sample-${mode}"
if [[ "$mode" == "static" ]]; then
project="$(git log -n 1 --pretty=format:'%H')"
fi

if [ "$(docker-compose -p "$project" ps | grep -v "lighthouse" | grep -c Exit)" -gt 0 ]; then
echo 'Some containers failed to start'
docker-compose -p "$project" ps
return 1
fi
)

function run_project_quality_tests()
(
cd "${path_test}"

ws helm kubeval --cleanup app
)

function run_project_acceptance_tests()
(
cd "${path_test}"
)

function restart_environment()
(
cd "${path_test}"
ws disable
ws enable
)

function setup_dynamic_mountpoint()
(
cd "${path_test}"
echo "attribute('host.os'): linux" > workspace.override.yml
)

function setup_dynamic_mutagen()
(
cd "${path_test}"
echo "attribute('host.os'): darwin" > workspace.override.yml
echo "attribute('mutagen'): yes" >> workspace.override.yml
)

function clean()
{
if [ -d "${path_test}/.my127ws" ]; then
(cd "$path_test" && ws destroy) || (docker ps -a && return 1)
fi

if [ -d "$path_test" ]; then
rm -rf "$path_test"
fi
}

main "$@"
54 changes: 0 additions & 54 deletions Jenkinsfile

This file was deleted.

16 changes: 14 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,30 @@ git commit -m "Initial commit"
```
1. Store the `workspace.override.yml` contents in a suitable location (such as LastPass).

[Workspace]: https://github.com/my127/workspace
[Workspace]: https://github.com/my127/workspace v0.3.1+


This harness is currently unopinionated, in that it doesn't set up an initial packages.json,
nor currently linting configuration. Some of this may change in the future though.

Once `ws create` has exited, it will be expected to fail to start due to there being no packages.json.

While workspace v0.3 doesn't support parent harnesses, using it you will also need to edit `workspace.yml` to add the docker harness layer:

```patch
workspace('...'):
description: generated local workspace for ...
harnessLayers:
+ - inviqa/docker:v0.2.0
- inviqa/node-spa:v0.2.0
```

Then run `ws install` to finish installation.

You can use create-react-app to set up a React app:

```
[docker-compose run --rm node] npx create-react-app .
[docker-compose run --rm app] npx create-react-app .
ws enable
```

Expand Down
Loading

0 comments on commit 0b4292e

Please sign in to comment.