I have a question regarding the execution of shared Library methods within a K8s POD/container.
I have a simple shared library example, which does, among other things, create a folder, yes, it does other things and in fact that doesn’t matter now. The fact is that it works, I have been testing Docker agents in Jenkins. https://github.com/faustobranco/devops-db/tree/master/infrastructure/pipelines/lib-utilities
But when I create it in K8, using multi-containers, as in the example pipeline below, the method does not run within the indicated container. The sh, sever call is just for testing and indicating that I am in the correct container.
@Library('devopsdb-global-lib') _
import devopsdb.utilities.Utilities
def obj_Utilities = new Utilities(this)
pipeline {
agent {
// kubernetes {
// yamlFile 'file-pod/resources/pods/file-pod.yaml'
// retries 2
// }
kubernetes {
yaml """
apiVersion: v1
kind: Pod
metadata:
labels:
some-label: "pod-template-${env.BUILD_ID}"
spec:
containers:
- name: container-1
image: registry.devops-db.internal:5000/img-jenkins-devopsdb:2.0
env:
- name: CONTAINER_NAME
value: "container-1"
volumeMounts:
- name: shared-volume
mountPath: /mnt
command:
- cat
tty: true
- name: container-2
image: ubuntu:22.04
env:
- name: CONTAINER_NAME
value: "container-2"
volumeMounts:
- name: shared-volume
mountPath: /mnt
command:
- cat
tty: true
volumes:
- name: shared-volume
emptyDir: {}
"""
}
}
options { timestamps ()
skipDefaultCheckout(true)
}
stages {
stage('Environment') {
steps {
container('container-1') {
script {
sh 'echo $CONTAINER_NAME > /mnt/shared_volume.txt'
def str_folder = "/mnt/pipelines/resources"
def str_folderCheckout = "/pip"
obj_Utilities.CreateFolders(str_folder)
sh 'sleep 10m'
obj_Utilities.SparseCheckout('[email protected]:infrastructure/pipelines/resources.git',
'master',
str_folderCheckout,
'usr-service-jenkins',
str_folder)
sh 'sleep 1s'
sh "PIP_CONFIG_FILE=${str_folder}${str_folderCheckout}/pip-devops.conf pip install devopsdb"
}
}
}
}
}
}
The result of the check inside the Container: It is clear that the folder is not created, but the file is.
Again, the issue is not creating the folder itself, that’s what sh solves, the problem is that shared doesn’t run inside the container, this prevents me, for example, from using the lib for sparse checkout.
kubectl get pods --namespace jenkins-devopsdb
NAME READY STATUS RESTARTS AGE
infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d 3/3 Running 0 61s
kubectl get pods infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d --namespace jenkins-devopsdb -o="custom-columns=NAME:.metadata.name,INIT-CONTAINERS:.spec.initContainers[*].name,CONTAINERS:.spec.containers[*].name"
NAME INIT-CONTAINERS CONTAINERS
infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d <none> container-1,container-2,jnlp
kubectl exec -i -t infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d --container container-1 --namespace jenkins-devopsdb -- /bin/bash
root@infrastructure-pipelines-tests-test-file-pod-28-hvhmc-btx-zp14d:/pipeline# ls -lah /mnt/
total 12K
drwxrwxrwx 2 root root 4.0K Jun 26 10:24 .
drwxr-xr-x 1 root root 4.0K Jun 26 10:24 ..
-rw-r--r-- 1 root root 12 Jun 26 10:24 shared_volume.txt
If I let the pipeline continue:
11:39:36 ERROR: Checkout failed
11:39:36 Also: hudson.remoting.Channel$CallSiteStackTrace: Remote call to JNLP4-connect connection from 172.21.5.156/172.21.5.156:26427
11:39:36 at hudson.remoting.Channel.attachCallSiteStackTrace(Channel.java:1787)
11:39:36 at hudson.remoting.UserRequest$ExceptionResponse.retrieve(UserRequest.java:356)
11:39:36 at hudson.remoting.Channel.call(Channel.java:1003)
11:39:36 at hudson.FilePath.act(FilePath.java:1226)
11:39:36 at hudson.FilePath.act(FilePath.java:1215)
11:39:36 at hudson.FilePath.mkdirs(FilePath.java:1406)
11:39:36 at hudson.plugins.git.GitSCM.createClient(GitSCM.java:845)
11:39:36 at hudson.plugins.git.GitSCM.checkout(GitSCM.java:1294)
11:39:36 at org.jenkinsci.plugins.workflow.steps.scm.SCMStep.checkout(SCMStep.java:136)
11:39:36 at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:101)
11:39:36 at org.jenkinsci.plugins.workflow.steps.scm.SCMStep$StepExecutionImpl.run(SCMStep.java:88)
11:39:36 at org.jenkinsci.plugins.workflow.steps.SynchronousNonBlockingStepExecution.lambda$start$0(SynchronousNonBlockingStepExecution.java:47)
11:39:36 at java.base/java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:539)
11:39:36 at java.base/java.util.concurrent.FutureTask.run(FutureTask.java:264)
11:39:36 at java.base/java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1136)
11:39:36 at java.base/java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:635)
11:39:36 at java.base/java.lang.Thread.run(Thread.java:840)
11:39:36 java.nio.file.AccessDeniedException: /mnt/pipelines