Shell : Retrieve a string after a character and replace it with another - shell

Within my Jenkins Job ,
i'm using an environment variable : $SVN_URL where:
$SVN_URL=http://svn.local:8080/svn/project1/trunk
i'm executing in a further step this shell command :
svn copy http://svn.local:8080/svn/project1/trunk http://svn.local:8080/svn/project1/tags/v$RELEASE_VERSION \
-m "Tagging the v$RELEASE_VERSION release"
this command can be replaced by this (using my $SVN_URL):
svn copy $SVN_URL http://svn.local:8080/svn/project1/tags/v$RELEASE_VERSION \
-m "Tagging the v$RELEASE_VERSION release"
But i still not able to optimise it for the second part which is :
http://svn.local:8080/svn/project1/tags/v$RELEASE_VERSION
So i wann use my $SVN_URL for it.
So to resume :
My $SVN_URL contains : http://svn.local:8080/svn/project1/trunk/
i wanna retrieve from it the part : /trunk/
add it the part : /tag/
Suggestions ?

i used simply "sed" and that worked fine
svn copy $SVN_URL "$(echo $SVN_URL| sed 's/trunk/tags/')"/v$RELEASE_VERSION \
-m "Tagging the v$RELEASE_VERSION release"

Related

Assigning output from az artifacts universal download to variable

I have a question. Is it possible to assign output from az aritfacts universal download to variable?
I have a Jenkins job where I have script in shell like this:
az artifacts universal download \
--organization "sampleorganization" \
--project "sampleproject" \
--scope project \
--feed "sample-artifacts" \
--name $PACKAGE \
--version $VERSION \
--debug \
--path .
Then I would like transport the file to artifactory with this:
curl -v -u $ARTIFACTORY_USER:$ARTIFACTORY_PASS -X PUT https://artifactory.com/my/test/repo/my_test_file_{VERSION}
I ran the job but noticed that I passed to artifactory empty file. It created my_test_file_{VERSION} but it had 0 mb. As far as I understand I just created empty file with curl. So I would like to pass the output from az download to artifactory repo. Is it possible? How can I do this?
I understand that I need to assign file output to variable and pass it to the curl like:
$MyVariableToPass = az artifacts universal download output
And then pass this var to curl.
Is it possible? How can I pass files between Jenkins which triggers shell job to artifactory?
Also I am not using any plugin right now.
Please help.
The possible solution is to use a VM as the agent of the Jenkins, and then install the Azure CLI inside the VM. You can run the task in that node. To set a variable with the value of the CLI command, for example, the output looks like this:
{
"name": "test_name",
....
}
Then you can set the variable like this:
name=$(az ...... --query name -o tsv)
This is in the Linux system. If it's in the Windows, you can set it like this:
$name = $(az ...... --query name -o tsv)
And as I know, the command to download the file won't output the content of the file. So if you want to set the content of the file as a variable, it's not suitable.

How to pass a value from JSON to a command in shell script at Docker? [duplicate]

This question already has answers here:
Command not found error in Bash variable assignment
(5 answers)
Closed 3 years ago.
I have a dockerfile to create an image with Ubuntu 16.04. Inside the docker file:
# dotnet tool command
RUN apt-get install dotnet-sdk-2.2 -y
# for dot net tool
ENV PATH="${PATH}:/root/.dotnet/tools"
# jq command to read json file
RUN apt-get install jq -y
ADD xxx /xxx
# Copy the deploy-tool.json file
ADD deploy-tool.json /xxx/deploy-tool.json
# Copy the main sh script to xxx
ADD main-migrate.sh /xxx/main-migrate.sh
# Run the main sh script to run script in each xxx/*/bla..bla.sh
RUN chmod +x /xxx/main-migrate.sh
RUN /permasense/main-migrate.sh
my deploy-tool.json is as followed:
{
"name": "xxx.DEPLOY",
"version": "1.2.3-dev.29"
}
here is main-migrate.sh
name = $(jq '.name' /xxx/deploy-tool.json)
nugetFileVersion = $(jq '.version' /xxx/deploy-tool.json)
# TODO how to pass value from JSON to the command below
# install dot net nuget
dotnet tool install -g $name --version "$nugetFileVersion" --add-source /xxx/
I have the xxx.DEPLOY.nupkg in xxx folder.
When dockerfile runs main-migrate.sh, I got error message complaining that the name and nugetFileVersion cannot be found.
How do I pass name & nugetFileVersion from the jq command to the dotnet tool install above as shown in main-migrate.sh?
Thank you
The idea is right, but the problem is with your main-migrate.sh script in the assignment statements. Shell assignments don't take spaces around the = symbol. It should have been
name=$(jq '.name' /xxx/deploy-tool.json)
# ^^^ no spaces
nugetFileVersion=$(jq '.version' /xxx/deploy-tool.json)
# ^^^ no spaces

How to run jq from gitbash in windows?

I have gitbash in Windows. I am trying to run jq but its giving me error.
$ ./jq-win64.exe
jq
parse error: Invalid numeric literal at line 2, column 0
Intention: I want to use jq to parse json.
Easiest solution and always latest version:
run this curl in your gitbash:
curl -L -o /usr/bin/jq.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe
or manually save the jq-win64.exe in link above as jq.exe to your /usr/bin (which is in your git bash installation folder)
(if you are behind a proxy add the -x proxyhost:port)
Using jq-win64.exe from github.com/stedolan/jq/releases, I get
vonc#voncav MINGW64 /d/prgs/dl
$ ./jq-win64.exe --version
jq-1.6
vonc#voncav MINGW64 /d/prgs/dl
$ echo '{"foo": 0}' | ./jq-win64.exe .
{
"foo": 0
}
So it does work, but it then depends on the json document you are parsing with it.
If that json document is not well-formed, that would generate the error you see.
In your bash session, you can define (or add to your ~/.bashrc) an alias:
alias jq=/path/to/jq-win64.exe
That way, you don't need to use ./jq, but directly jq.
$ echo '{"foo": 0}' | jq
In my case:
vonc#voncav:/$ alias jq=/mnt/d/dwnl/jq-win64.exe
vonc#voncav:/$ echo '{"foo": 0}' | jq
{
"foo": 0
}
I hate answers that say you need another to use another tool to download, but using https://chocolatey.org/ made this incredibly simple for me.
From an elevated command-prompt, run:
choco install jq
jq magically works from bash going forward.
Instead of using chocolatey (with elevated rights) you could also use scoop.sh:
scoop install jq
Below steps worked for me in git bash on windows 10
curl -L -o jq-win64.exe https://github.com/stedolan/jq/releases/latest/download/jq-win64.exe
export PATH=$PATH:"/C/Users/devops/Downloads/jq-win64.exe
jq --version
jq-1.6
For windows Powershell && cmd
Download executable -> jq 1+ executables for 64-bit
Create folder in Program Files -> JQ
Copy .exe into program folder
Set environment variable to that folder
Restart terminal
Set alias
Set-Alias -Name jq -Value .\jq-win64.exe
Run command
echo '{"foo": 0}' | jq
Output
{
"foo": 0
}
enjoy!
I just downloaded the binary to %HOMEPATH%/bin/jq-win64 and it worked right away via jq
In my case, I have fixed it in Windows10 while practicing a scenario of "Techworld with Nana" course, as per the below steps:
1) open "cmd" prompt (Run as administrator)
2) curl -L -o /usr/bin/jq-win64.exe
Download it here
3) You can add C:\Program Files\Git\usr\bin path to your environment variables. If you follow this step, ignore the below steps.
[OR]
3) Go to the directory where jq is downloaded: C:\Program Files\Git\usr\bin and copy the file to C:\ drive
4) Add the path C:\ to "path" environment variable. This PC-->Properties-->Advanced system settings --> Environment Variables -->Edit-->New. Then add the path (where you copied jq.exe, i.e., C:) and Save it.
5) Now open a new "cmd" prompt, jq --version

docker build command add 'C:/Program Files/Git' to the path passed as build argument when executed in MINGW bash on Windows

I have following Dockerfile:
FROM ubuntu:16.04
ARG path1=def_path1
RUN mkdir ${path1}
When I build this Dockerfile using following command:
docker build --build-arg path1=/home/dragan -t build_arg_ex .
I get following error when I execute it in MINGW bash on Windows 10:
$ ./build.sh --no-cache
Sending build context to Docker daemon 6.144kB
Step 1/3 : FROM ubuntu:16.04
---> 2a4cca5ac898
Step 2/3 : ARG path1=def_path1
---> Running in a35241ebdef3
Removing intermediate container a35241ebdef3
---> 01475e50af4c
Step 3/3 : RUN mkdir ${path1}
---> Running in 2759e683cbb1
mkdir: cannot create directory 'C:/Program': No such file or directory
mkdir: cannot create directory 'Files/Git/home/dragan': No such file or
directory
The command '/bin/sh -c mkdir ${path1}' returned a non-zero code: 1
Building same Dockerfile in Windows Command Prompt or on Linux or Mac is ok. The problem is only in MINGW bash terminal on Windows because it adds 'C:/Program Files/Git' before the path that is passed as argument.
Is there a way to execute this in MINGW bash so it does not add the 'C:/Program Files/Git' prefix?
Thanks
This is actually a bug/limitation of Git for Windows as described in the Release Notes under Known issues:
If you specify command-line options starting with a slash, POSIX-to-Windows path conversion will kick in converting e.g. "/usr/bin/bash.exe" to "C:\Program Files\Git\usr\bin\bash.exe". When that is not desired -- e.g. "--upload-pack=/opt/git/bin/git-upload-pack" or "-L/regex/" -- you need to set the environment variable MSYS_NO_PATHCONV temporarily, like so:
MSYS_NO_PATHCONV=1 git blame -L/pathconv/ msys2_path_conv.cc
Alternatively, you can double the first slash to avoid POSIX-to-Windows path conversion, e.g. "//usr/bin/bash.exe".
Further to #mat007's answer:
This bash function solved the problem more permanently for docker, without enabling MSYS_NO_PATHCONV globally, which causes another world of pain.
.bashrc
# See https://github.com/docker/toolbox/issues/673#issuecomment-355275054
# Workaround for Docker for Windows in Git Bash.
docker()
{
(export MSYS_NO_PATHCONV=1; "docker.exe" "$#")
}
You may need to do the same for docker-compose

Emulate subwcrev when using git-svn

I use git-svn to interact with an existing SVN repository that contains some C++ projects. subwcrev.exe is used as a pre-build event to update some strings in a C++ header (svnversion.h). These strings get hardcompiled to form some version information for the resulting binary.
Since subwcrev requires .svn metadata to work, the pre-build event is going to fail when used on the git-svn working copy. So I came up with the following bash script which I use as post-commit and post-checkout hooks for my git repository. The script tries to do the same thing as subwcrev based on the output of git svn info (cached in a local file).
#!/bin/sh
if [ ! -f svninfo ] ; then
git svn info > svninfo
fi
revision=`sed -e "/Revision/!d" -e "s/Revision: \(.*\)/\1/" svninfo`
lastchange=`sed -e "/Last Changed Rev/!d" -e "s/Last Changed Rev: \(.*\)/\1/" svninfo`
# Get the last changed date, extract timestamp, replaces dashes with slashes
changedate=`sed -e "/Last Changed Date/!d" -e "s/Last Changed Date: \(.\{19\}\).*/\1/" -e "s!-!\\\\\\/!g" svninfo`
now=`date "+%Y\/%m\/%d %H:%M:%S"`
gitcommit=`git show --abbrev-commit | sed -n -e "s/commit //p"`
for entry in $( find -name svnversion_template.h ); do
newname=`echo $entry|sed -e "s/_template//"`
sed -e "s/\\\$WCRANGE\\\$/${revision}/" \
-e "s/\\\$WCREV\\\$/${lastchange}-${gitcommit}/" \
-e "s/\\\$WCDATE\\\$/${changedate}/" \
-e "s/\\\$WCNOW\\\$/${now}/" \
-e "s/\\\$WCURL\\\$/local git repo/" \
-e "s/\\\$WCMODS.*\\\$/(true)/" \
-e "s/\\\$WCMIXED.*\\\$/(false)/" \
$entry > `echo $entry|sed -e "s/_template//"`
done
What I cannot really emulate so far is the automatic detection of a local uncommitted changes (based on the last checked out SVN revision) that makes subwcrev so useful.
I am replacing $WCREV$ with the revision number of the SVN repository (as subwcrev would do) but this time I add my abbreviated git commit hash to identify the code I compiled. My question now is: Is there a way to distinguish in a shell script whether my current HEAD differs from the last fetched SVN revision so that I could omit adding the -${gitcommit} part and set $WCMODS$ to false?
If there were some thing like a post-"git svn dcommit" hook, my problem would be solved, too, since then that special hook would create the svnversion.h differently. Can such hook be added somehow?
I don't really get your points, but start to improve your script first.
revision=$(grep -Po "(?<=Revision: ).*" svninfo)
lastchange=$(grep -Po "(?<=Last Changed Rev: ).*" svninfo)
# Get the last changed date, extract timestamp, replaces dashes with slashes
changedate=$(grep -Po "(?<=Last Changed Date: ).{19}" svninfo)
changedate=${changedate//-//}
now=$(date "+%Y\/%m\/%d %H:%M:%S")
Then, in for loop, could you please explain detail, what result you need?
Can you show one sample of svnversion_template.h ?
So it looks like you might have to parse the contents of the git svn info query yourself to get what is normally stored in WCREV. The example results look like this for me:
git svn info
Path: .
URL: http://myurl.com/trunk/myrepo
Repository Root: http://myurl.com
Repository UUID: 15fed3e9-81ce-ef4a-a7da-fc36e3df1edc
Revision: 14106
Node Kind: directory
Schedule: normal
Last Changed Author: myusername
Last Changed Rev: 14106
Last Changed Date: 2015-05-29 10:23:10 -0400 (Fri, 29 May 2015)
Now for the second part of your question, whether or not you can tell if your git HEAD matches the latest svn checkout, you'll need to use the command git diff git-svn command. "git-svn" here is the name of the branch that the git-svn program is maintaining, and if everything is up to date, the results will be empty.

Resources