How to echo Jenkins Workspace in Cygwin - windows

I am trying to build a job from jenkins pipeline, my pipeline is calling one shell script which is on windows server, and this script is in cygwin terminal, question is how to use the Jenkins workspace in my shell script in cygwin, tried below commands are not working, can someone please advise. Thanks.
In jenkins Pipeline:
sh "/home/test.sh $WORKSPACE"
In Cygwin:
#!/cygdrive/d/cygwin64/bin/bash --login
WORKSPACE=$1
echo "$WORKSPACE"
Out put from above command is
D:Jenkinsworkspacey_test123_feature_test
but actual workspace is(multibranch pipeline)
Running on win01 in D:\Jenkins\workspace\y_test123_feature_test
Not sure will cygwin take the different command to retrieve the workspace as you can see above output does not contains slashes in between words.

Workspace can be accessed using the enviornmental variable inside the pipeline.
In jenkins Pipeline:
// Get workspace
// This will give you the workspace of the agent that is available in the current stage
def Workspace=env.WORKSPACE
// Convert path with \\ for cygwin
Workspace.replace("\\", "\\\\");
println(Workspace)
sh "/home/test.sh ${Workspace}"

Related

How to get GitLab Runner to work with Bash on Windows

I am trying to setup CI/CD within our GitLab instance to be run on a Windows 10 machine.
I have installed Git for Windows (which includes Git Bash) on the windows system.
I then register the runner according to the instructions given here.
I have then adjusted the config.toml file to use a bash shell, in this manner :
[[runners]]
name = "DESKTOP-R9R6OV2"
url = "https://git.mycompany.com/"
token = "ozaxN3XoJm2sEPzc3BPx"
executor = "shell"
shell = "bash"
builds_dir="/C/Build/"
As per this article - GitLab CI and Git Bash - I have created a bash.cmd file which contains
#"C:\Program Files\Git\usr\bin\bash.exe" -l
I have also ensured that C:\Program Files\Git and C:\Program Files\Git\usr\bin are in the global path definition as the runner is installed in the system admin account.
The pipeline kicks off, but seems to hang :
Running with gitlab-runner 14.7.0 (98daeee0)
on DESKTOP-R9R6OV2 ozaxN3Xo
Preparing the "shell" executor
Using Shell executor...
Preparing environment
With the process waiting on Preparing environment
I have looked in the Event Viewer logs and there are no messages explaining what is going on.
So my question is, what else do I need to do to get the bash shell to work within GitLab Runner on a Windows 10 system ?
Are there special system permissions which need to be set ?
The working answer to this scenario is to use windows powershell and then launch the git bash shell from there.
So, in your gitlab-ci.yml, add the following powershell commands :
- New-Alias -Name gitBash -Value "$Env:ProgramFiles\Git\bin\bash.exe"
- gitBash -c <name of your bash file>
This will run your bash file within the git bash environment.
This means that your config.toml file must be setup thus :
[[runners]]
...
executor = "shell"
shell = "powershell"

Unable to execute shell scripts on cygwin(mintty terminal) using Jenkins

Unable to execute shell scripts on Cygwin(mintty terminal) using Jenkins pipeline but I can able to execute same scripts directly on Cygwin(mintty terminal), cygwin is installed on jenkins windows agent, anything I am missing on Jenkins windows agent to configure? and also my shell scripts are not executing on cygwin when using the Jenkins, they are executing on windows command prompt, can someone please advise how to make the jenkins to use the cygwin terminalto execute shell scripts instead of command prompt? .Thank you.
Below is the Jenkins pipeline how I am calling test.sh(test.sh is on jenkins windows agent)
stage('Import') {
steps {
script {
sh "/home/kumar/test.sh"
}
}
}
I see below error when execute the build.
/home/kumar/test.sh
FIND: Invalid switch
mv: cannot stat '/home/kumar/TEST_FILES/*': No such file or directory
below find command used in script.
find /cygdrive/d/Jenkins/workspace/sam-org/testing/app_name/src/abc -name "*.dsx" -exec cp "{}" /home/kumar/ITEST_FILES \

Git Portable 64 Bash not working in Windows Server 2016 Task Scheduler

I need help trying to figure out why Git repository commands are not not executing when run in a script in Windows Server 2016 Task scheduler. All works OK when I execute them in a command console.
In the Windows Server 2016 Task Scheduler, my Action "Start a Program" is: C:\Apps\repo.scripts\UpdateMyRepo.bat
The UpdateMyRepo.bat cmd code is:
SET HOME=C:\Users\Repo
REM change to MyRepo git working repository
C:
cd \MyRepo
REM execute my script bash script to update my repository
C:\Apps\Git\bin\bash.exe --login -i -c "/c/Apps/repo.scripts/UpdateMyRepo.sh"
The UpdateMyRepo.sh bash code is
#!/c/Apps/Git/bin/bash.exe -x
export HOME=/c/Users/Repo
cd /c/MyRepo
# write a log entry so we know we are in the repository folder
ls -al > /c/Apps/repo.scripts/myrepofolder.log
# write the git --version to a log file so we know git is working
/c/Apps/Git/bin/git.exe --version > /c/Apps/repo.scripts/version.log
# write the git status to a log file so we know git repository commands work
/c/Apps/Git/bin/git.exe status > /c/Apps/repo.scripts/status.log
# write a done log entry and quit the bash shell
echo done > /c/Apps/repo.scripts/done.log
exit
Everything works in Windows 2016 Task Scheduler except the git status command. The git status writes an empty blank status.log file. Actually, it seems like any other commands, like git add, git commit, git push, etc., that act on the repository yield blank output.
If I execute the command manually when logged in as the Repo user and double clicking on the C:\Apps\repo.scripts\UpdateMyRepo.bat in Windows file explorer or running in a console, all works perfectly and the repository git status is written to the status.log. I get the "null" results when executing the task from the Task Scheduler either manually or on trigger.
Please help me figure out how to run git repository commands in Windows Server 2016 task scheduler. I have already tried too many variations of commands, scripts, and permissions to list each that did not work here.
Platform Details:
Windows Server 2016 Standard, all current updates
Git Portable 64bit, 2.17.1.windows.2
Repository files are stored on in the operating system and task scheduler's local hard drive, not in a network share
update: When I run the Git repository tasks in the WS2016 Task Scheduler and figured out how to log some of the output, I get the following error:
fatal: this operation must be run in a work tree
The repository folder is NOT a "bare" repository. So I am suspecting that the WS2016 Task Scheduler is applying additional permission constraints that do not apply to the user account assigned in the task. I get this same error if I try to run the tasks in an Admin Console instead of a normal user console.
First, most of Git commands writes their output to stderr, not stdout
So you need to redirect stderr to stdout, using for instance &> instead of >.
Second, to be sure all your Git command will be executed in the right working tree/repo, you can add environment variables for the repository location
export GIT_WORK_TREE=/c/path/to/my/repo
export GIT_DIR=/c/path/to/my/repo/.git

run .sh script via Jenkins to execute aws command error

my problem is that i try to execute shell script to copy created files from msbuild to AWS s3 via Jenkins.
Then i add new build step "Execute Shell" and set to execute shell script by command: sh publishS3.sh nothing happens and files doesn't apper in s3 bucket.
my Jenkins use Local Windows Server.
Then i try to execute the shell script by typing sh publishS3.sh in Jenkins local directory all ok , files was copyed secessfully to s3 bucket , but if i try to do it from jenkins nothing was happen. My publishS3.sh script is:
#!/bin/bash
aws s3 cp Com.VistaDraft.Common.dll s3://download.vistadraft.com/MVP
i was tryed to to check witch output i receive after execute by adding at the end command > output.txt but Jenkins generate an empty file. If i try to do the same locally i was receive an message that i secessfully copyed files to s3. i Set the shell script path of jenkins C:\Program Files\Git\git-bash.exe and using git-bash.exe locally too. Maybe whom know where is a problem ? Please suggest.
You could try to add -ex in the first line of the script to allow you to see what it's doing and ease the debugging:
#!/bin/bash -ex
# rest of script
Make sure the aws tool is in the PATH of the environment where Jenkins runs your script. It might help if you specify full path to the command.
You could put which aws in your script to see what's going on.

How can I default to a login shell for Jenkins shell execution

I want to use rvm (or rbenv/chruby for that matter) to select different ruby versions from within my Jenkins jobs.
By default, Jenkins will use /bin/sh, which on Ubuntu, is dash.
For this to change, I can add
#!/bin/bash -l
To the top of every single shell execute function everywhere. Seeing as that's a lot of annoying work, I'd like to be able to set that somewhere central.
Using the "Shell executable" configuration setting, I can get it to run bash, adding parameters like '-l' however will fail with
"/bin/bash -l" -xe /tmp/hudson5660076222778817826.sh FATAL:
command execution failed java.io.IOException: Cannot run program
"/bin/bash -l" (in directory
"/home/jenkins/jobs/workspace/rvm-test"): error=2, No such file or
directory
I tried using the rvm plugin for jenkins, but that doesn't even install on the current release version.
Any ideas? :)
You could work around by creating a wrapper around bash:
#!/bin/sh
# for ex.: /usr/local/bin/login-bash
exec /bin/bash -l "$#"
If you want to use the default ruby just use the rvm-shell, which comes with rvm.
Login as the jenkins user and type:
$ which rvm-shell
/home/jenkins/.rvm/bin/rvm-shell
to get the path of the rvm-shell.
Use this path for the "Shell executable" option.

Resources