1

I am currently working on creating a Jenkins job with Kubernetes and Karate to display a few test results using Cucumber reports. I am having difficulties trying to obtain environment variables passed into the deployment file through a configmap, as follows:

Deployment:

apiVersion: apps/v1
kind: Deployment
metadata:
  name: jenkins-deployment
spec:
  
  selector:
    matchLabels:
      app: jenkins
  replicas: 2 # tells deployment to run 2 pods matching the template
  template:
    metadata:
      labels:
        app: jenkins
    spec:
      containers:
        - name: jenkins
          image: url/customjenkinsimage:tag
          ports:
            - containerPort: 8080
          envFrom:
            - configMapRef:
                name: jenkins-configmap

ConfigMap:

kind: ConfigMap 
apiVersion: v1 
metadata:
  name: jenkins-configmap 
data:
  # pass data as environment variables 
    URL_TO_TEST: "http://www.examplewebsite.com"

I looked this up and saw that some people mentioned using karate.properties['VAR_NAME'] inside the karate-config.js file and passing that through the config, but this resulted in my jenkins output telling me that the 'url' part in Given url host1 doesn't exist. I've tried process.env as well (which of course doesn't work but it was worth a shot)

I was wondering if there is any way to access this environment variable in the karate-config.js (or even if I pass a json object in the configmap) since I would need it to build a url to pass to the feature for testing.

my karate-config.js:

function fn() {  

    var config = {
        host1: karate.properties['URL_TO_TEST']
    };

    return config;
  }

All of these files get put into the /var/jenkins_home/jobs/karate_job/workspace/ folder to run using a Shell command: cd /var/jenkins_home/jobs/karate_job/workspace/ ; java -jar karate.jar test.feature.

Any help would be appreciated, Thanks.

  • I think `java.lang.System.getenv('PATH')` answers your question, let me know if not – Peter Thomas Mar 20 '21 at 02:33
  • 1
    Thank you, this works. I used https://www.tutorialspoint.com/java/lang/system_getenv_string.htm for further clarification, for my use case I had to replace PATH with my environment variable name. I fixed the env variable name in my post. – Basil Zuberi Mar 22 '21 at 13:59

0 Answers0