Skip to content

Commit 74d7ca2

Browse files
authored
Add build tests (#67)
* Add build tests Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com> * Fix dockerfile Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com> * Manual multi arch builds Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com> * Build works Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com> * Move to npm CLI Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com> * Back to buildx Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com> * Run binfmt installation Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com> --------- Signed-off-by: Pierangelo Di Pilato <pierdipi@redhat.com>
1 parent 828babd commit 74d7ca2

File tree

10 files changed

+1989
-1327
lines changed

10 files changed

+1989
-1327
lines changed

hack/build.sh

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,21 @@
22

33
source "$(dirname $0)/common.sh"
44

5-
build_transform_jsonata_image || exit 1
5+
err_report() {
6+
echo "!!! Error on line $1 !!!"
7+
exit 1
8+
}
9+
10+
set -T # inherit DEBUG and RETURN trap for functions
11+
set -C # prevent file overwrite by > &> <>
12+
set -E # inherit -e
13+
set -e # exit immediately on errors
14+
set -u # exit on not assigned variables
15+
set -o pipefail # exit on pipe failure
16+
17+
trap 'err_report $LINENO' ERR
18+
19+
build_transform_jsonata_image || exit $?
20+
21+
# TODO: To enable the builds in build-tests we need to disable the "push"
22+
# build_integration_images || exit $?

hack/common.sh

Lines changed: 50 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,63 @@ set -euo pipefail
44

55
export TAG=${TAG:-$(git rev-parse HEAD)}
66

7+
export TRANSFORM_JSONATA_IMAGE_WITH_TAG="${KO_DOCKER_REPO:-kind.local}/transform-jsonata:${TAG}"
8+
79
[[ ! -v REPO_ROOT_DIR ]] && REPO_ROOT_DIR="$(git rev-parse --show-toplevel)"
810
readonly REPO_ROOT_DIR
911

12+
export TRANSFORM_JSONATA_DIR="${REPO_ROOT_DIR}/transform-jsonata"
13+
14+
1015
function build_transform_jsonata_image() {
11-
local image="${KO_DOCKER_REPO}/transform-jsonata:${TAG}"
1216

17+
if (( ${IS_PROW:-0} )); then
18+
docker run --privileged --rm tonistiigi/binfmt --install all binfmt
19+
fi
20+
21+
docker info
22+
docker buildx ls
23+
24+
echo "Building image for arm64"
25+
docker buildx build \
26+
--debug \
27+
--pull \
28+
-t "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-arm64" \
29+
-f "${REPO_ROOT_DIR}/transform-jsonata/Dockerfile" \
30+
--platform "linux/arm64" \
31+
"${REPO_ROOT_DIR}/transform-jsonata" \
32+
|| return $?
33+
34+
echo "Building image for amd64"
1335
docker buildx build \
14-
--platform linux/amd64,linux/arm64 \
15-
-t "${image}" \
36+
--debug \
37+
--pull \
38+
-t "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-amd64" \
1639
-f "${REPO_ROOT_DIR}/transform-jsonata/Dockerfile" \
17-
--push \
18-
"${REPO_ROOT_DIR}/transform-jsonata" || return $?
40+
--platform "linux/amd64" \
41+
"${REPO_ROOT_DIR}/transform-jsonata" \
42+
|| return $?
43+
}
44+
45+
function push_transform_jsonata_image() {
46+
docker push "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-amd64" || return $?
47+
docker push "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-arm64" || return $?
48+
49+
echo "Creating manifest ${TRANSFORM_JSONATA_IMAGE_WITH_TAG}"
50+
docker manifest create --amend "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}" \
51+
"${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-amd64" \
52+
"${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-arm64" || return $?
1953

54+
echo "Pushing manifest ${TRANSFORM_JSONATA_IMAGE_WITH_TAG}"
55+
docker manifest push "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}" || return $?
56+
57+
digest=$(docker buildx imagetools inspect "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}" --format "{{json .Manifest.Digest }}" | tr -d '"')
58+
TRANSFORM_JSONATA_IMAGE="${TRANSFORM_JSONATA_IMAGE_WITH_TAG}@${digest}"
59+
export TRANSFORM_JSONATA_IMAGE
60+
61+
echo "TRANSFORM_JSONATA_IMAGE=${TRANSFORM_JSONATA_IMAGE}"
62+
}
2063

21-
TRANSFORM_JSONATA_IMAGE=$(docker inspect --format '{{index .RepoDigests 0}}' "${image}")
22-
export TRANSFORM_JSONATA_IMAGE
64+
function build_integration_images() {
65+
"${REPO_ROOT_DIR}/mvnw" clean package -P knative -DskipTests || return $?
2366
}

hack/release.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,11 @@ function build_release() {
2727

2828
# KO_DOCKER_REPO and TAG are calculated in release.sh script
2929

30-
./mvnw clean package -P knative -DskipTests || return $?
30+
build_integration_images || return $?
3131

3232
# use the function so that the exported TRANSFORM_JSONATA_IMAGE is visible after during generate_transformation_configMap
3333
build_transform_jsonata_image || return $?
34+
push_transform_jsonata_image || return $?
3435

3536
echo "Image build & push completed"
3637

test/presubmit-tests.sh

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,12 @@
11
#!/usr/bin/env bash
22

3-
# TODO a placeholder script to CI automation.
4-
# Can be used to execute integration tests in the future.
3+
# shellcheck disable=SC1090
4+
source "$(go run knative.dev/hack/cmd/script presubmit-tests.sh)"
55

6-
./hack/build.sh
6+
function build_tests() {
7+
header "Running build tests"
8+
export IS_PROW
9+
./hack/build.sh || fail_test "build tests failed"
10+
}
711

8-
true
12+
main $@

transform-jsonata/Dockerfile

Lines changed: 12 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,39 +3,30 @@ FROM registry.access.redhat.com/ubi9/nodejs-20 AS builder
33
# Set working directory
44
WORKDIR /app
55

6-
# Ensure the working directory has appropriate permissions
7-
RUN mkdir -p /app && chown -R 1001:0 /app
8-
USER 1001
9-
10-
# Install pnpm globally
11-
RUN npm install -g pnpm
12-
13-
# Copy package.json and pnpm-lock.yaml before installing dependencies
14-
COPY package.json pnpm-lock.yaml ./
6+
# Copy package.json and package-lock.json before installing dependencies
7+
# Ensure correct permissions by using --chown flag
8+
COPY --chown=1001:0 package.json package-lock.json ./
159

16-
# Install dependencies using pnpm
17-
RUN pnpm install --frozen-lockfile
10+
# Install dependencies as non-root user
11+
USER 1001
12+
RUN npm install --frozen-lockfile
1813

1914
# Copy the rest of the application files
20-
COPY . .
21-
22-
# Build the application (if necessary)
23-
RUN pnpm build || echo "No build step found, skipping"
15+
COPY --chown=1001:0 . .
2416

2517
# Use a minimal base image for runtime
2618
FROM registry.access.redhat.com/ubi9/nodejs-20
2719

28-
ENV NODE_ENV=production
29-
3020
# Set working directory
3121
WORKDIR /app
3222

33-
# Ensure the working directory has appropriate permissions
34-
RUN mkdir -p /app && chown -R 1001:0 /app
23+
# Copy built files and dependencies
24+
COPY --from=builder --chown=1001:0 /app /app
25+
26+
# Switch to non-root user
3527
USER 1001
3628

37-
# Copy built files and dependencies
38-
COPY --from=builder /app /app
29+
ENV NODE_ENV=production
3930

4031
# Expose the application port
4132
EXPOSE 8080

transform-jsonata/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,17 +15,17 @@ broker <- trigger <- (response) <-
1515
Assuming current working directory is `transform-jsonata`
1616

1717
```shell
18-
pnpm dev
18+
npm dev
1919

20-
# or pnpm dev-kubevirt to inject a different example transformation
20+
# or npm dev-kubevirt to inject a different example transformation
2121
```
2222

2323
### Tracing
2424

2525
```shell
2626
docker run --rm -d -p 9411:9411 openzipkin/zipkin
2727

28-
pnpm dev-zipkin
28+
npm dev-zipkin
2929
```
3030

3131
## Building

transform-jsonata/jsonata.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ const jsonata_response_transform_file_name = process.env.JSONATA_RESPONSE_TRANSF
4040

4141
const jsonata_discard_response_body = process.env.JSONATA_DISCARD_RESPONSE_BODY === "true" || false;
4242

43-
const jsonata_config_tracing = JSON.parse(process.env.JSONATA_CONFIG_TRACING || '{}')
43+
const jsonata_config_tracing = JSON.parse(process.env.K_TRACING_CONFIG || '{}')
4444

4545
const oidc_token_file = process.env.OIDC_TOKEN_FILE || undefined
4646
if (oidc_token_file && !fs.existsSync(oidc_token_file)) {

0 commit comments

Comments
 (0)