Sam_Ste
Sam_Ste

Reputation: 464

Jenkins writeYaml before pipelines starts and read in agent.kubernetes.yamlFile

I have a pipeline that needs a modified yaml file for different environments. For that I read the template, overwrite the parameter and save it again before the pipeline { ... } part starts.

node {
  stage('Adjust serviceAccountName to env') {
    checkout scm
    def valuesYaml = readYaml (file: 'build_nodes.yaml')
    valuesYaml.spec.serviceAccountName = 'user-test'
    sh 'rm -f build_nodes_new.yaml'
    writeYaml file: 'build_nodes_new.yaml', data: valuesYaml
  }
}

Once I want to load the file the problem is that it can't be found:

pipeline {
  environment {
    ENV_VAR=....
  }
  agent {
    kubernetes {
      label 'some_label'
      yamlFile 'build_nodes_new.yaml'
    }
  }
  stages {
    stage('Assume Role') { ... }

Throws an error:

java.io.FileNotFoundException: URL: /rest/api/1.0/projects/PROJECT/repos/backend/browse/build_nodes_new.yaml?at=feature%2Fmy-branch-name&start=0&limit=500

Do I have to save the yaml file somewhere else? If I ls -la it is displayed.

Upvotes: 0

Views: 1062

Answers (2)

user12548239
user12548239

Reputation: 53

I had similar issue and below worked for me. thanks @sam_ste

def get_yaml() {
  node {
    sh 'env'
    echo GERRIT_PATCHSET_REVISION
    echo "${GERRIT_PATCHSET_REVISION}" 
    return """
apiVersion: v1
kind: Pod
metadata:
  labels:
    some-label: some-label-value
spec:
  containers:
  - name: simplekube
    image: dhub.net/jenkins/simplekube:${GERRIT_PATCHSET_REVISION}
    command:
    - cat
    tty: true
    securityContext:
      runAsUser: 0
    """
  }
}

Upvotes: 0

Mzzl
Mzzl

Reputation: 4126

This is because you wrote the yaml file on a regular node, and then try to read it from a container in k8s. It's like they're on different machines. In fact, they very likely are. You could pass the contents as a string to the k8s node, or you could write it to a filesystem that the k8s pod can mount

Upvotes: 1

Related Questions