Jenkinsfile how to grep file name in variable - grep

I have file in different server and that name will change.
File: testfile-1.2-12345.sh ,"12345" is going to change.
How I get chancing text to variable?
In server machine this works and it's prints 12345:
ls ~/test/testfile* | awk -F'[-.s]' '{print $5}'
But when I do it from jenkins it wont work:
def versio = sh "ssh user#${ip} ls ~/test/testfile* | awk -F'[-.s]' '{print \$5}'"
It prints "12345" but if I try to print ${versio} it shows null.

Your command is correct. But in pipeline you need to specify returnStdout:true. Detailed documentation is here.
def versio = sh returnStdout: true, script: 'ssh user#${ip} ls ~/test/testfile* | awk -F\'[-.s]\' \'{print \\$5}\''

Related

how to retrieve value of 'Position' from SHOW MASTER STATUS in mariadb

How can I retrieve just the 'position' value from the 'SHOW MASTER STATUS' query expression in MariaDB(running in docker container) from outside the docker container ?
Something like, I have the following :-
+-------------------+----------+--------------+------------------+
| File | Position | Binlog_Do_DB | Binlog_Ignore_DB |
+-------------------+----------+--------------+------------------+
| master-bin.000003 | 375 | | |
+-------------------+----------+--------------+------------------+
and need to get only value of position using bash script ?
Please help as soon as possible.
When MariaDB running inside docker container and want to retrieve File and Position Value from "SHOW MASTER STATUS" in MariaDB/MySQL from outside the docker container, use the following command in bash :-
for File
docker exec -it <mariadb_container_name> bash -c "mysql -u<username> -p<password> -Ne 'show master status' | awk '{print $1}' | cut -f1"
for Position
docker exec -it <mariadb_container_name> bash -c "mysql -u<username> -p<password> -Ne 'show master status' | awk '{print $2}' | cut -f2"

How to trim a set of numbers from a string in Jenkins?

How to trim set of numbers from a sting in Jenkins. This is what I am trying but some how there is one mistake not able to fix it.
def Build_Num = sh(script: "echo ${BUILD_PATH} | awk 'BEGIN {FS="_"};{print $NF}' | sed 's/[A-Za-z]*//g'", returnStdout: true).trim()
BUILD_PATH=ABC/EFGH/ABCD_1.2.3456.78912/
Result should be like this: 1.2.3456.78912
Directly in shell I am trying to achieve with below command but not able to do it in Jenkins:
echo ABC/EFGH/ABCD_1.2.3456.78912/ | awk 'BEGIN {FS="_"};{print $NF}' | sed 's/[A-Za-z]*//g'
You need to escape your $ character for your awk argument, otherwise Groovy will perform string interpolation.
Of course, we do not want to escape the $ for ${BUILD_PATH}, because in that case we do want string interpolation, we just don't want string interpolation for $NF.
Try this:
def Build_Num = (sh script: """echo ${BUILD_PATH} | awk 'BEGIN {FS="_"};{print \$NF}' | sed 's/[A-Za-z]*//g'""", returnStdout: true).trim()

extract version from package.json with bash inside jenkins pipeline

Script in package.json:
"scripts": {
"version": "echo $npm_package_version"
},
One of the stage in Jenkins pipeline:
stage('Build'){
sh 'npm install'
def packageVersion = sh 'npm run version'
echo $packageVersion
sh 'VERSION=${packageVersion} npm run build'
}
I got version from npm script output, but following line
echo $packageVersion
return null
Is packageVersion value not correctly assigned?
Edited:
when using Pipeline Utility Steps with following code
stage('Build'){
def packageJSON = readJSON file: 'package.json'
def packageJSONVersion = packageJSON.version
echo packageJSONVersion
sh 'VERSION=${packageJSONVersion}_${BUILD_NUMBER}_${BRANCH_NAME} npm run build'
}
I get
[Pipeline] echo
1.1.0
[Pipeline] sh
[...] Running shell script + VERSION=_16_SOME_BRANCH npm run build
So I am able to extract version, but still cannot pass it when running script
After your edit using readJSON, you now get string interpolation wrong. Variables within single quotes are not replaced in Groovy, only within double quotes.
sh 'VERSION=${packageJSONVersion}_${BUILD_NUMBER}_${BRANCH_NAME} npm run build'
must be
sh "VERSION=${packageJSONVersion}_${BUILD_NUMBER}_${BRANCH_NAME} npm run build"
The sh step by default returns nothing, so packageVersion should be null.
To return the output of the executed command, use it like this:
sh(script: 'npm run version', returnStdout: true)
This variant of sh returns the output instead of printing it.
Actually, I am wondering, why echo $packageVersion doesn't fail with an error, as this variable is not defined, but should be echo packageVersion.
For my it this:
sh(script: "grep \"version\" package.json | cut -d '\"' -f4 | tr -d '[[:space:]]'", returnStdout: true)
This worked for me:
Full version number:
PACKAGE_VERSION = sh returnStdout: true, script: '''grep 'version' package.json | cut -d '"' -f4 | tr '\n' '\0''''
echo "Current package version: $PACKAGE_VERSION"
$ > 1.2.3
Major version only:
PACKAGE_VERSION = sh returnStdout: true, script: '''grep 'version' package.json | cut -d '"' -f4 | cut -d '.' -f1 | tr '\n' '\0''''
echo "Current package Major version: $PACKAGE_VERSION"
$ > 1
stage('Read JSON') {
steps {
script {
def packageJson = readJSON file: 'package.json'
def packageVersion = packageJSON.version
echo "${packageJSONVersion}"
}
}
}
Below snippet worked for me: Credits to #Ferenc Takacs
version = sh(returnStdout: true, script: "grep 'version' package.json | cut -d '"' -f4 | tr '\n' '\0'")
This command will take exact property version: ... in package.json and can work on both Mac and Linux. The other solutions using grep will not give you correct answer in case of you have > 1 version keyword in your package.json (it'll return all of them instead of just the one you want)
awk -F'"' '/"version": ".+"/{ print $4; exit; }' package.json
To access an npm environment variable outside the scope of a run-script, parse the variable with bash:
$ npm run env | grep npm_package_version | cut -d '=' -f 2
Author: https://remarkablemark.org/blog/2018/08/14/package-json-version/

Gradle task type:Exec cannot export env var

Im trying to expose a service that is on my local to a docker network. In order to do so, i need to export the result of this command:
ip route show | grep docker0 | awk '{print \$9}'
Here is what my gradle task looks like
task exportEnvVariables( type:Exec ) {
executable "sh"
args "-c", "export", "HOST_IP=\$(ip route show | grep docker0 | awk '{print \$9}')"
/* Have also tried:
commandLine 'export HOST_IP=$(ip route show | grep docker0 | awk \'{print $9}\')' */
println System.getenv( "HOST_IP" )
}
Your current approach cannot work, simply because you try to read the environment variable before you export it.
Whatever you put inside a task closure is executed during configuration phase (e.g. your println statement), while the actual task action is executed during execution phase.
You can use a doLast closure to execute a statement during execution phase:
task exportEnvVariables( type:Exec ) {
executable "sh"
args "-c", "export", "HOST_IP=\$(ip route show | grep docker0 | awk '{print \$9}')"
/* Have also tried: commandLine 'export HOST_IP=$(ip route show | grep docker0 | awk \'{print $9}\')' */
doLast {
println System.getenv( "HOST_IP" )
}
}

Jenkins pipeline sh does not seem to respect pipe in shell command

I am using a Jenkinsfile in a pipeline on version 2.32.2.
For various reasons I want to extract the version string from the pom. I was hoping I wouldn't have to add the maven help plugin and use evaluate.
I quickly came up with a little sed expression to get it out of the pom which uses pipes and works on the commandline in the jenkins workspace on the executor.
$ sed -n '/<version>/,/<version/p' pom.xml | head -1 | sed 's/[[:blank:]]*<\/*version>//g'
1.0.0-SNAPSHOT
It could probably be optimized, but I want to understand why the pipeline seems to be failing on piped sh commands. I've played with various string formats and am currently using a dollar slashy string.
The pipeline step looks like the following to allow for easy output of the command string:
script {
def ver_script = $/sed -n '/<version>/,/<version/p' pom.xml | head -1 | sed 's/[[:blank:]]*<\/*version>//g'/$
echo "${ver_script}"
POM_VERSION = sh(script: "${ver_script}", returnStdout: true)
echo "${POM_VERSION}"
}
When run in the jenkins pipeline I get the following console output where it seems to be separating the piped commands into separate commands:
[Pipeline] script
[Pipeline] {
[Pipeline] echo
sed -n '/<version>/,/<version/p' pom.xml | head -1 | sed 's/[[:blank:]]*<\/*version>//g'
[Pipeline] sh
[FRA-198-versioned-artifacts-44SD6DBQOGOI54UEF7NYE4ECARE7RMF7VQYXDPBVFOHS5CMSTFLA] Running shell script
+ sed -n /<version>/,/<version/p pom.xml
+ head -1
+ sed s/[[:blank:]]*<\/*version>//g
sed: couldn't write 89 items to stdout: Broken pipe
[Pipeline] }
[Pipeline] // script
Any guidance out there on how to properly use piped commands in a jenkinsfile ?
I finally put some thought into it and realized that pipe subshells are probably causing the issue. I know some of the evils of eval but I ended up wrappping this in an eval:
script {
def ver_script = $/eval "sed -n '/<version>/,/<version/p' pom.xml | head -1 | sed 's/[[:blank:]]*<\/*version>//g'"/$
echo "${ver_script}"
POM_VERSION = sh(script: "${ver_script}", returnStdout: true)
echo "${POM_VERSION}"
}
I know this kind of late answer, but whoever you who needs the solution without eval you can use /bin/bash -c "script" to make pipe works
script {
POM_VERSION = sh(script: "/bin/bash -c 'sed -n \'/<version>/,/<version/p\' pom.xml | head -1 | sed \'s/[[:blank:]]*<\/*version>//g\'\''", returnStdout: true)
echo "${POM_VERSION}"
}
The only problem with this method is hellish escape yet this way the subshell of pipe will be handled by our boy /bin/bash -c
If your environment allows it, I've found a simple solution to this problem to be to place your script containing pipes into a file, and then run that with sh, like so:
script.sh:
#!/bin/sh
kubectl exec --container bla -i $(kubectl get pods | awk '/foo-/{ print $1 }') -- php /code/dostuff
Jenkinsfile:
stage('Run script with pipes') {
steps {
sh "./script.sh"
}
}
The pipeline-utility-steps plugin nowadays includes a readMavenPom step, which allows to access the version as follows:
version = readMavenPom.getVersion()
So nothing detailed above worked for me using the scripted Jenkinsfile syntax with Groovy. I was able to get it working, however. The type of quotations you use are important. In the example below, I am trying to fetch the latest git tag from GitHub.
...
stage("Get latest git tag") {
if (env.CHANGE_BRANCH == 'master') {
sh 'git fetch --tags'
TAGGED_COMMIT = sh(script: 'git rev-list --branches=master --tags --max-count=1', returnStdout: true).trim()
LATEST_TAG = sh(script: 'git describe --abbrev=0 --tags ${TAGGED_COMMIT}', returnStdout: true).trim()
VERSION_NUMBER = sh(script: "echo ${LATEST_TAG} | cut -d 'v' -f 2", returnStdout: true).trim()
echo "VERSION_NUMBER: ${VERSION_NUMBER}"
sh 'echo "VERSION_NUMBER: ${VERSION_NUMBER}"'
}
}
...
Notice how the shell execution to assign LATEST_TAG works as expected (assigning the variable to v2.1.0). If we were to try the same thing (with single quotes) to assign VERSION_NUMBER, it would NOT work - the pipe messes everything up. Instead, we wrap the script in double quotes.
The first echo prints VERSION_NUMBER: 2.1.0 but the second prints VERSION_NUMBER:. If you want VERSION_NUMBER to be available in the shell commands, you have to assign the output of the shell command to env.VERSION_NUMBER as shown below:
...
stage("Get latest git tag") {
if (env.CHANGE_BRANCH == 'master') {
sh 'git fetch --tags'
TAGGED_COMMIT = sh(script: 'git rev-list --branches=master --tags --max-count=1', returnStdout: true).trim()
LATEST_TAG = sh(script: 'git describe --abbrev=0 --tags ${TAGGED_COMMIT}', returnStdout: true).trim()
env.VERSION_NUMBER = sh(script: "echo ${LATEST_TAG} | cut -d 'v' -f 2", returnStdout: true).trim()
echo "VERSION_NUMBER: ${VERSION_NUMBER}"
sh 'echo "VERSION_NUMBER: ${VERSION_NUMBER}"'
}
}
...
The first echo prints VERSION_NUMBER: 2.1.0 and the second prints VERSION_NUMBER: 2.1.0.
I am also struggling with the usage of pipe inside my jenkins pipeline but as a side note, if you want a simple way to extract the version of a maven pom, here's a very clean one I found in another post and that I'm using :
stage('Preparation') {
version = getVersion()
print "version : " + version
}
def getVersion() {
def matcher = readFile('pom.xml') =~ '<version>(.+)</version>'
matcher ? matcher[0][1] : null
}
gives you :
[Pipeline] echo
releaseVersion : 0.1.24
[Pipeline] sh

Resources