Procedure of a Wharf container step
Sample .wharf-ci.yml
:
myStage:
myStep:
container:
image: mcr.microsoft.com/dotnet/sdk:latest
cmds:
- dotnet run
Then you run something like
wharf build kubernetes --namespace build
Here’s a comparison of procedures between running in Kubernetes and locally via Docker:
Kubernetes
Start pod
- name: wharf-container-12345
- emptyDir volume
- initcontainer:
- mount emptyDir at
/emptyDir
- image:
alpine/git:latest
- cmd:
git clone "${REPO}" /emptyDir/repo
- mount emptyDir at
- container:
- image:
${myStage.myStep.container.image}
- cmd:
${myStage.myStep.container.shell}
(Just to make the container sit and wait for input that we’re never going to give it) - tty:
true
- working dir:
/emptyDir/repo
- image:
- name: wharf-container-12345
Wait until initcontainer is done and container has started
Run the commands remotely
Alternative A:
Write the commands into a file and add the leading shebang. Ex:
#!${myStage.myStep.container.shell} ${myStage.myStep.container.cmds}
Get that file over to the container. For example like how
kubectl cp
does it. (This would require havingtar
installed though).Could use a ConfigMap or Secret here and set the
configMap.defaultMode
to777
.1Run the script. Use something equivalent to
kubectl exec -ti "${podName}" /path/to/script
Alternative B:
Run the commands by assuming the shell got a
-c
flag:kubectl exec -ti "${podName}" \ "${myStage.myStep.container.shell}" \ -c "${myStage.myStep.container.cmds}"
Alternative C:
Feed init container with the command and then let it place that inside the emptyDir. Example:
git clone "${REPO}" /emptyDir/repo cat <<'WHARF_CONTAINER_SCRIPT' > /emptyDir/script #!${myStage.myStep.container.shell} ${myStage.myStep.container.cmds} WHARF_CONTAINER_SCRIPT chmod +x /emptyDir/script
Run the script. Use something equivalent to
kubectl exec -ti "${podName}" /emptyDir/script
Local via Docker
Write the commands to a temporary file. Ex:
mkdir -p /tmp/wharf/build/12345 cat <<'WHARF_CONTAINER_SCRIPT' > /tmp/wharf/build/12345/script #!${myStage.myStep.container.shell} ${myStage.myStep.container.cmds} WHARF_CONTAINER_SCRIPT chmod +x /tmp/wharf/build/12345/script
Copy the repo
cp -r . /tmp/wharf/build/12345/repo
Run the command via docker
# Assuming the repo is at pwd docker run --rm -it \ --volume /tmp/wharf/build/12345:/wharf \ --workdir /wharf/repo \ --entrypoint "${myStage.myStep.container.shell}" \ "${myStage.myStep.container.image}" \ /wharf/script
Kubernetes reference. Config and Storage Resources, Volume, Projections https://kubernetes.io/docs/reference/kubernetes-api/config-and-storage-resources/volume/#Projections
↩︎