Skip to content

Commit

Permalink
Dockerfile: make base-image an ARG
Browse files Browse the repository at this point in the history
  • Loading branch information
JonJagger committed Jan 25, 2025
1 parent a6be045 commit 46d7c3d
Show file tree
Hide file tree
Showing 14 changed files with 72 additions and 24 deletions.
13 changes: 9 additions & 4 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
FROM cyberdojo/docker-base:d6830c0
ARG BASE_IMAGE
FROM ${BASE_IMAGE}
LABEL maintainer=jon@jaggersoft.com

# ARGs are reset after FROM See https://github.com/moby/moby/issues/34129
ARG BASE_IMAGE
ENV BASE_IMAGE=${BASE_IMAGE}

ARG COMMIT_SHA
ENV SHA=${COMMIT_SHA}

RUN gem install --no-document 'concurrent-ruby'

WORKDIR /runner
COPY source/server/ .

ARG COMMIT_SHA
ENV SHA=${COMMIT_SHA}

USER root
HEALTHCHECK --interval=1s --timeout=1s --retries=5 --start-period=5s CMD /runner/config/healthcheck.sh
ENTRYPOINT ["/sbin/tini", "-g", "--"]
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ API
* [GET alive](docs/api.md#get-alive)
* [GET ready](docs/api.md#get-ready)
* [GET sha](docs/api.md#get-sha)
* [GET base_image](docs/api.md#get-base-image)

***

Expand Down
6 changes: 3 additions & 3 deletions bin/build_image.sh
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,13 @@ build_image()
check_args "$@"
local -r type="${1}"
exit_non_zero_unless_installed docker
export $(echo_versioner_env_vars)
export $(echo_env_vars)
containers_down
remove_old_images

docker compose build --build-arg COMMIT_SHA="${COMMIT_SHA}" server
docker compose build server
if [ "${type}" == 'client' ]; then
docker compose build --build-arg COMMIT_SHA="${COMMIT_SHA}" client
docker compose build client
fi

local -r image_name="${CYBER_DOJO_RUNNER_IMAGE}:${CYBER_DOJO_RUNNER_TAG}"
Expand Down
2 changes: 1 addition & 1 deletion bin/check_coverage.sh
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ check_args()
check_coverage()
{
check_args "$@"
export $(echo_versioner_env_vars)
export $(echo_env_vars)

local -r TYPE="${1}" # {server|client}
local -r TEST_LOG=test.log
Expand Down
2 changes: 1 addition & 1 deletion bin/demo.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ export ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
readonly DEMO_FILENAME="/tmp/runner_demo.html"

source "${ROOT_DIR}/bin/lib.sh"
export $(echo_versioner_env_vars)
export $(echo_env_vars)
docker compose --progress=plain up --wait --wait-timeout=10 client
docker exec -it test_runner_client ruby /runner/demo.rb > "${DEMO_FILENAME}"
open "file://${DEMO_FILENAME}"
20 changes: 17 additions & 3 deletions bin/lib.sh
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,25 @@ containers_down()
docker compose down --remove-orphans --volumes
}

echo_versioner_env_vars()
echo_base_image()
{
local -r sha="$(cd "${ROOT_DIR}" && git rev-parse HEAD)"
echo COMMIT_SHA="${sha}"
local -r json="$(curl --fail --silent --request GET https://beta.cyber-dojo.org/runner/base_image)"
echo "${json}" | jq -r '.base_image'
}

echo_env_vars()
{
# --build-arg ...
if [[ ! -v CYBER_DOJO_RUNNER_BASE_IMAGE ]] ; then
echo CYBER_DOJO_RUNNER_BASE_IMAGE=cyberdojo/docker-base:d6830c0
#echo CYBER_DOJO_RUNNER_BASE_IMAGE="$(echo_base_image)"
fi
if [[ ! -v COMMIT_SHA ]] ; then
local -r sha="$(cd "${ROOT_DIR}" && git rev-parse HEAD)"
echo COMMIT_SHA="${sha}"
fi

# From versioner ...
docker run --rm cyberdojo/versioner

echo CYBER_DOJO_RUNNER_SHA="${sha}"
Expand Down
2 changes: 1 addition & 1 deletion bin/run_tests.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ source "${ROOT_DIR}/bin/lib.sh"
source "${ROOT_DIR}/bin/create_test_data_manifests_file.sh"
source "${ROOT_DIR}/bin/setup_dependent_images.sh"

export $(echo_versioner_env_vars)
export $(echo_env_vars)

show_help()
{
Expand Down
2 changes: 1 addition & 1 deletion bin/snyk_container_scan.sh
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ set -Eeu

export ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"
source "${ROOT_DIR}/bin/lib.sh"
export $(echo_versioner_env_vars)
export $(echo_env_vars)

readonly IMAGE_NAME="${CYBER_DOJO_RUNNER_IMAGE}:${CYBER_DOJO_RUNNER_TAG}"
readonly SARIF_FILENAME=${SARIF_FILENAME:-snyk.container.scan.json}
Expand Down
8 changes: 6 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ services:
client:
build:
context: source/client
args: [ COMMIT_SHA ]
args:
- COMMIT_SHA
- BASE_IMAGE=${CYBER_DOJO_RUNNER_BASE_IMAGE}
image: ${CYBER_DOJO_RUNNER_CLIENT_IMAGE}:${CYBER_DOJO_RUNNER_TAG}
user: ${CYBER_DOJO_RUNNER_CLIENT_USER}
container_name: ${CYBER_DOJO_RUNNER_CLIENT_CONTAINER_NAME}
Expand All @@ -21,7 +23,9 @@ services:
server:
build:
context: .
args: [ COMMIT_SHA ]
args:
- COMMIT_SHA
- BASE_IMAGE=${CYBER_DOJO_RUNNER_BASE_IMAGE}
image: ${CYBER_DOJO_RUNNER_IMAGE}:${CYBER_DOJO_RUNNER_TAG}
user: ${CYBER_DOJO_RUNNER_SERVER_USER}
container_name: ${CYBER_DOJO_RUNNER_SERVER_CONTAINER_NAME}
Expand Down
14 changes: 14 additions & 0 deletions docs/api.md
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,20 @@ The 40 character git commit sha used to create the Docker image.
{"sha":"41d7e6068ab75716e4c7b9262a3a44323b4d1448"}
```

- - - -
## GET base_image
The base-image used in the Dockerfile's FROM statement.
- [JSON-in](#json-in) parameters
* none
- returns the [JSON-out](#json-out) result, keyed on `"base_image"`
* eg `"cyberdojo/docker-base:d6830c0"`
- example
```bash
$ curl --silent --request GET https://${DOMAIN}:${PORT}/base_image
{"base_image":"cyberdojo/docker-base:d6830c0"}
```


- - - -
## JSON in
- All methods pass any arguments as a json hash in the http request body.
Expand Down
11 changes: 8 additions & 3 deletions source/client/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
FROM cyberdojo/docker-base:d6830c0
ARG BASE_IMAGE
FROM ${BASE_IMAGE}
LABEL maintainer=jon@jaggersoft.com

WORKDIR /runner
COPY . .
# ARGs are reset after FROM See https://github.com/moby/moby/issues/34129
ARG BASE_IMAGE
ENV BASE_IMAGE=${BASE_IMAGE}

ARG COMMIT_SHA
ENV SHA=${COMMIT_SHA}

WORKDIR /runner
COPY . .

USER nobody
HEALTHCHECK --interval=1s --timeout=1s --retries=5 --start-period=5s CMD /runner/config/healthcheck.sh
ENTRYPOINT ["/sbin/tini", "-g", "--"]
Expand Down
3 changes: 2 additions & 1 deletion source/server/dispatcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@ def initialize(context)
def call(path, body)
args = parse_json_args(body)
case path
when '/sha' then ['sha', prober.sha(**args)]
when '/alive' then ['alive?', prober.alive?(**args)]
when '/ready' then ['ready?', prober.ready?(**args)]
when '/sha' then ['sha', prober.sha(**args)]
when '/base_image' then ['base_image', prober.base_image(**args)]
when '/pull_image' then ['pull_image', puller.pull_image(**args)]
when '/run_cyber_dojo_sh' then ['run_cyber_dojo_sh', runner.run_cyber_dojo_sh(**args)]
else
Expand Down
4 changes: 4 additions & 0 deletions source/server/prober.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,8 @@ def ready?
def sha
ENV.fetch('SHA', nil)
end

def base_image
ENV.fetch('BASE_IMAGE', nil)
end
end
8 changes: 4 additions & 4 deletions test/server/check_test_metrics.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ def table_data
[ 'test.branches.total', test_cov['branches']['total' ], '<=', 0 ],
[ 'test.branches.missed', test_cov['branches']['missed'], '<=', 0 ],
[ nil ],
[ 'code.lines.total', code_cov['lines' ]['total' ], '<=', 549 ],
[ 'code.lines.missed', code_cov['lines' ]['missed'], '<=', 1 ],
[ 'code.branches.total', code_cov['branches']['total' ], '<=', 66 ],
[ 'code.branches.missed', code_cov['branches']['missed'], '<=', 2 ],
[ 'code.lines.total', code_cov['lines' ]['total' ], '<=', 552 ],
[ 'code.lines.missed', code_cov['lines' ]['missed'], '<=', 3 ],
[ 'code.branches.total', code_cov['branches']['total' ], '<=', 67 ],
[ 'code.branches.missed', code_cov['branches']['missed'], '<=', 3 ],
]
end

Expand Down

0 comments on commit 46d7c3d

Please sign in to comment.