Prior to using Kaniko, I was doing builds on a Gitlab Shell Executor on a dedicated VM. In that mode I was able to extract build artifacts from a "build and test" target prior to generating the final "application" target.
docker build \
-t ${DOCKER_TAG_NAME}:${VERSION} \
-t ${DOCKER_TAG_NAME}:latest \
--build-arg VERSION=${VERSION} \
--target build-and-test \
--file buildTestPublish.Dockerfile .
# To copy files we must create a container. We cannot copy from an image. The container doesn't start though.
echo "Creating container ${DOCKER_TAG_NAME}:${VERSION} for file copying."
id=$(docker create ${DOCKER_TAG_NAME}:${VERSION})
echo "Copying unit-tests out of container to /unit-tests"
#Docker cp source paths must be from the root.
rm -rf unit-tests | exit 0
mkdir unit-tests
docker cp ${id}:/app_build/The.Solution.Name/The.Solution.Name.HttpClient.Test/unit-tests/junit.xml unit-tests/junit-HttpClient.xml
docker cp ${id}:/app_build/The.Solution.Name/The.Solution.Name.Repository.Test/unit-tests/junit.xml unit-tests/junit-Repository.xml
docker cp ${id}:/app_build/The.Solution.Name/The.Solution.Name.Service.Test/unit-tests/junit.xml unit-tests/junit-Service.xml
docker cp ${id}:/app_build/The.Solution.Name/The.Solution.Name.Web.Test/unit-tests/junit.xml unit-tests/junit-Web.xml
echo "Removing container ${DOCKER_TAG_NAME}:${VERSION}."
docker rm -v ${id}
Now that I am building on OpenShift with Kaniko I don't seem to have a way to get artifacts out of the built image. I can't mount a volume to the kaniko image because Gitlab doesn't support running an image with parameters. I have to run the debug image and script the executor like:
build:
stage: build
<<: *runners
<<: *except_master_and_prodfix
variables:
#Provides the base registry path to which /cache will be appended.
AWS_ACCESS_KEY_ID: $DEV_AWS_ACCESS_KEY_ID
AWS_SECRET_ACCESS_KEY: $DEV_AWS_SECRET_ACCESS_KEY
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [ "sh" ]
artifacts:
paths:
- artifacts/
script:
# We cannot git merge from master here because busybox used in Kaniko does not have git nor does it have
# a package installer.
# Docker command is not available in Kaniko image so we have to create the .docker/config.json file manually.
- echo "$DOCKER_AUTH_CONFIG" > /kaniko/.docker/config.json
- mkdir -p artifacts
#This builds but does not push to the registry
- /kaniko/executor
--context $CI_PROJECT_DIR
--no-push
--skip-unused-stages=true
--cache=true
--cache-repo=${CI_REGISTRY_IMAGE}/cache
--log-timestamp=true
--log-format=text
--target build-and-test
If there isn't currently a way to "mount" a file path into the executor it would be helpful to be able to do so, such that generated artifacts can be copied to the mounted path (ex. /artifacts) and they would be written to the file system captured by Gitlab.
I've run into this same issue, needing to extract the results of junit tests for reporting. This would be extremely useful.
Same thing here. Currently I have to develop ugly hacks to build the project into a local tar file, then extracting artifacts to it, instead of simply telling kaniko to store artifacts in a directory that stays available after the build has finished.
Most helpful comment
I've run into this same issue, needing to extract the results of junit tests for reporting. This would be extremely useful.