Skip to content

Commit d989e06

Browse files
committed
Chown workspace for non-root containers
When running jobs on Kubernetes containers instead of the Jenkins built-in node, containers running as a non-root user have problems cleaning the workspace. Run chown on jnlp so the workspace matches runAsUser/runAsGroup before forceCleanupWS. Signed-off-by: Noam Tsemah <ntsemah@nvidia.com>
1 parent 839904f commit d989e06

1 file changed

Lines changed: 15 additions & 0 deletions

File tree

src/com/mellanox/cicd/Matrix.groovy

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -900,8 +900,12 @@ def runK8(image, branchName, config, axis, steps=config.steps) {
900900
def namespace = image.namespace ?: getConfigVal(config, ['kubernetes', 'namespace'], "default")
901901
def tolerations = image.tolerations ?: getConfigVal(config, ['kubernetes', 'tolerations'], "[]")
902902
def imagePullSecrets = parseImagePullSecrets(getConfigVal(config, ['kubernetes', 'imagePullSecrets'], "[]"))
903+
def nonRoot = "${runAsUser}" != '0' || "${runAsGroup}" != '0'
904+
def fsPolicy = nonRoot ? '\n fsGroupChangePolicy: OnRootMismatch' : ''
903905
def yaml = """
904906
spec:
907+
securityContext:
908+
fsGroup: ${runAsGroup}${fsPolicy}
905909
containers:
906910
- name: ${cname}
907911
env:
@@ -913,6 +917,8 @@ spec:
913917
limits: ${limits}
914918
requests: ${requests}
915919
securityContext:
920+
runAsUser: ${runAsUser}
921+
runAsGroup: ${runAsGroup}
916922
capabilities:
917923
add: ${caps_add}
918924
tolerations: ${tolerations}
@@ -940,6 +946,15 @@ spec:
940946
retry(count: 2, conditions: [kubernetesAgent(), nonresumable()]) {
941947
node(POD_LABEL) {
942948
stage (branchName) {
949+
if (nonRoot) {
950+
container('jnlp') {
951+
sh(script: '''#!/bin/bash
952+
set -eu
953+
WS="${WORKSPACE:?}"
954+
chmod -R g+rwX "${WS}@tmp" "${WS}" 2>/dev/null || true
955+
''', label: 'k8 chmod workspace for non-root')
956+
}
957+
}
943958
container(cname) {
944959
runSteps(image, config, branchName, axis, steps, 'k8')
945960
}

0 commit comments

Comments
 (0)