Issue
We have a dockerized maven project where we deploy it to the kubernetes via Jenkins and Helm.So the thing that I would like to do is, pass below shell script as a command in deployment.yaml like as below to initate selenium tests while pod is creating. But somehow before I deploy the service, I need to update the variables in shell script according to the conditional statements(jenkins parameters) and then pass to update shell script to the kubernetes pod.
So, is there anyway to add if statements and update variables according to the jenkins parameters and pass through to pod with proper values ?
Shell Script
#!/bin/sh
mvn --projects {$projectName} --also-make clean test -Dcucumber.filter.tags="@${servicename} or @${tag}" -Denvironment=test -Ddhc=true -Djavax.net.ssl.trustStore=/usr/java/openjdk-14/lib/security/cacerts
Deployment.yaml
env:
{{- range $key, $value := .Values.extraEnv }}
- name: {{ $key }}
value: {{ $value | quote }}
{{- end }}
command: ["/test-script.sh"]
Jenkinsfile
parameters {
string(name: 'projectName', defaultValue: "Xx", description: 'Which project do you want to test?')
string(name: "service_name", defaultValue: 'Yy', description: 'Selenium tag for service name')
string(name: "tag", defaultValue: 'must')
//string(name:'branchName', defaultValue: "origin/development", description: 'Environment for selenium tests')}
stage('Deploy to dev'){
steps{
script{
sh """
helm upgrade --install --debugtest-service --values values.${ENV}.yaml --namespace ${namespace} --set image.tag=${env.BUILD_NUMBER} .
"""
}
}
EDIT-1
ı just added my question according to the below answer. So after running sed and update the script, how can I add a if statement to check value of service name parameter? Or can I use something like
if [ <PROJECTNAME> == "xx]
Here is the steps that I would like to do.
projectName=$1
serviceName=$2
tag=$3
if [ "$serviceName" == "xx" ]
then
echo "Tests are running "..
mvn --projects <PROJECTNAME> --also-make clean test -Dcucumber.filter.tags=""@<SERVICENAME> or @<TAG>""
else
echo 'All test scenarios are running on stage environment'
mvn --projects <PROJECTNAME> --also-make clean test -Dcucumber.filter.tags="@<SERVICENAME> or @<SERVICENAME>" -
fi
Solution
You can keep placeholders for such values which requires to be updated dynamically at build time. I'd keep the shell-script as below with placeholders instead of variables:
#!/bin/sh
mvn --projects <PROJECTNAME> --also-make clean test -Dcucumber.filter.tags="@<SERVICENAME> or @<TAG>" -Denvironment=test -Ddhc=true -Djavax.net.ssl.trustStore=/usr/java/openjdk-14/lib/security/cacerts
And then add a sed
line within the sh block
above your helm upgrade ...
command to replace the placeholders with build time values so that it can passed to the next set of actions.
sh """
sed -i \"s/<PROJECTNAME>/${projectName}/g; s/<SERVICENAME>/${service_name}/g; s/<TAG>/${tag}/g\" /path/to/test-script.sh
helm upgrade . . .
"""
EDIT-1:
Example:
if (serviceName == 'xx') {
env.TAG = serviceName
}
sh """
.... other actions
"""
Answered By - vijay