Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add build tests #67

Merged
merged 7 commits into from
Feb 18, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion hack/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,21 @@

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

build_transform_jsonata_image || exit 1
err_report() {
echo "!!! Error on line $1 !!!"
exit 1
}

set -T # inherit DEBUG and RETURN trap for functions
set -C # prevent file overwrite by > &> <>
set -E # inherit -e
set -e # exit immediately on errors
set -u # exit on not assigned variables
set -o pipefail # exit on pipe failure

trap 'err_report $LINENO' ERR

build_transform_jsonata_image || exit $?

# TODO: To enable the builds in build-tests we need to disable the "push"
# build_integration_images || exit $?
Comment on lines +21 to +22
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the integration images build is trying to push to KO_DOCKER_REPO, so we can enable them later when we figure this out

57 changes: 50 additions & 7 deletions hack/common.sh
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,63 @@ set -euo pipefail

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

export TRANSFORM_JSONATA_IMAGE_WITH_TAG="${KO_DOCKER_REPO:-kind.local}/transform-jsonata:${TAG}"

[[ ! -v REPO_ROOT_DIR ]] && REPO_ROOT_DIR="$(git rev-parse --show-toplevel)"
readonly REPO_ROOT_DIR

export TRANSFORM_JSONATA_DIR="${REPO_ROOT_DIR}/transform-jsonata"


function build_transform_jsonata_image() {
local image="${KO_DOCKER_REPO}/transform-jsonata:${TAG}"

if (( ${IS_PROW:-0} )); then
docker run --privileged --rm tonistiigi/binfmt --install all binfmt
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's the multi-arch builder image?

Copy link
Member Author

@pierDipi pierDipi Feb 18, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's installing qemu packages so that docker buildx can emulate instructions for other platforms

fi

docker info
docker buildx ls

echo "Building image for arm64"
docker buildx build \
--debug \
--pull \
-t "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-arm64" \
-f "${REPO_ROOT_DIR}/transform-jsonata/Dockerfile" \
--platform "linux/arm64" \
"${REPO_ROOT_DIR}/transform-jsonata" \
|| return $?

echo "Building image for amd64"
docker buildx build \
--platform linux/amd64,linux/arm64 \
-t "${image}" \
--debug \
--pull \
-t "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-amd64" \
-f "${REPO_ROOT_DIR}/transform-jsonata/Dockerfile" \
--push \
"${REPO_ROOT_DIR}/transform-jsonata" || return $?
--platform "linux/amd64" \
"${REPO_ROOT_DIR}/transform-jsonata" \
|| return $?
}

function push_transform_jsonata_image() {
docker push "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-amd64" || return $?
docker push "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-arm64" || return $?

echo "Creating manifest ${TRANSFORM_JSONATA_IMAGE_WITH_TAG}"
docker manifest create --amend "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}" \
"${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-amd64" \
"${TRANSFORM_JSONATA_IMAGE_WITH_TAG}-arm64" || return $?

echo "Pushing manifest ${TRANSFORM_JSONATA_IMAGE_WITH_TAG}"
docker manifest push "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}" || return $?

digest=$(docker buildx imagetools inspect "${TRANSFORM_JSONATA_IMAGE_WITH_TAG}" --format "{{json .Manifest.Digest }}" | tr -d '"')
TRANSFORM_JSONATA_IMAGE="${TRANSFORM_JSONATA_IMAGE_WITH_TAG}@${digest}"
export TRANSFORM_JSONATA_IMAGE

echo "TRANSFORM_JSONATA_IMAGE=${TRANSFORM_JSONATA_IMAGE}"
}

TRANSFORM_JSONATA_IMAGE=$(docker inspect --format '{{index .RepoDigests 0}}' "${image}")
export TRANSFORM_JSONATA_IMAGE
function build_integration_images() {
"${REPO_ROOT_DIR}/mvnw" clean package -P knative -DskipTests || return $?
}
3 changes: 2 additions & 1 deletion hack/release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ function build_release() {

# KO_DOCKER_REPO and TAG are calculated in release.sh script

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

# use the function so that the exported TRANSFORM_JSONATA_IMAGE is visible after during generate_transformation_configMap
build_transform_jsonata_image || return $?
push_transform_jsonata_image || return $?

echo "Image build & push completed"

Expand Down
12 changes: 8 additions & 4 deletions test/presubmit-tests.sh
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#!/usr/bin/env bash

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

./hack/build.sh
function build_tests() {
header "Running build tests"
export IS_PROW
./hack/build.sh || fail_test "build tests failed"
}

true
main $@
33 changes: 12 additions & 21 deletions transform-jsonata/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,39 +3,30 @@ FROM registry.access.redhat.com/ubi9/nodejs-20 AS builder
# Set working directory
WORKDIR /app

# Ensure the working directory has appropriate permissions
RUN mkdir -p /app && chown -R 1001:0 /app
USER 1001

# Install pnpm globally
RUN npm install -g pnpm

# Copy package.json and pnpm-lock.yaml before installing dependencies
COPY package.json pnpm-lock.yaml ./
# Copy package.json and package-lock.json before installing dependencies
# Ensure correct permissions by using --chown flag
COPY --chown=1001:0 package.json package-lock.json ./

# Install dependencies using pnpm
RUN pnpm install --frozen-lockfile
# Install dependencies as non-root user
USER 1001
RUN npm install --frozen-lockfile

# Copy the rest of the application files
COPY . .

# Build the application (if necessary)
RUN pnpm build || echo "No build step found, skipping"
COPY --chown=1001:0 . .

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

ENV NODE_ENV=production

# Set working directory
WORKDIR /app

# Ensure the working directory has appropriate permissions
RUN mkdir -p /app && chown -R 1001:0 /app
# Copy built files and dependencies
COPY --from=builder --chown=1001:0 /app /app

# Switch to non-root user
USER 1001

# Copy built files and dependencies
COPY --from=builder /app /app
ENV NODE_ENV=production

# Expose the application port
EXPOSE 8080
Expand Down
6 changes: 3 additions & 3 deletions transform-jsonata/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,17 +15,17 @@ broker <- trigger <- (response) <-
Assuming current working directory is `transform-jsonata`

```shell
pnpm dev
npm dev

# or pnpm dev-kubevirt to inject a different example transformation
# or npm dev-kubevirt to inject a different example transformation
```

### Tracing

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

pnpm dev-zipkin
npm dev-zipkin
```

## Building
Expand Down
2 changes: 1 addition & 1 deletion transform-jsonata/jsonata.js
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ const jsonata_response_transform_file_name = process.env.JSONATA_RESPONSE_TRANSF

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

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

const oidc_token_file = process.env.OIDC_TOKEN_FILE || undefined
if (oidc_token_file && !fs.existsSync(oidc_token_file)) {
Expand Down
Loading