How to execute git-clone command from shell script (linux) - bash

Need to execute git clone command from shell script
I tried to use the git clone command and it is working
I am able to execute the below command from the git-bash console to checkout the developer1_branch from my project
git clone --branch developer1_branch https://project_ip/project.git /tmp/$(date +%Y%m%d)
This command will ask for credentials and it will clone the project to /tmp folder with today's date.
Expected results:
Need to run the same command from the shell script.
To automate the script to read the credentials from a creds.properties file which will have user=userid & password=Password123 as properties?
Please let me know how this can be achieved?

Related

How can I `git push` from a script initiated by a cronjob?

I have the following script:
cd ~/path/to/repo ;
git pull ;
git add . ;
git commit -m "autocommit for repo" ;
git push ;
cd ~ ;
I have it run in a cronjob every minute. It works up until the git push command and then it does nothing. If I run the script manually it works fine. I updated the cronjob to write the output of running the script to a temp text file to see if git push was failing. It is not failing, it just isn't being ran at all it seems. The only output present is related to the git commit command. What might be going wrong?
I'm on a Mac running the latest macOS but I have tried it on my Linux machine as well with the same result.
I updated the cronjob to write the output of running the script to a temp text file to see if git push was failing
Make sure to redirect both stdout and stderr, since most Git command produce outputs to stderr
*/1 * * * * /your/script.sh >> /your/script.log 2>&1
But a post-commit client-side local hook is a good alternative approach.
(As noted here, that means creating a push script named post-commit, without extension, as executable in your .git/hooks folder)

How to echo Jenkins Workspace in Cygwin

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}"

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

Git Clone Failing in Windows Batch Script

I executed this command "C:\Program Files (x86)\Git\bin\git.exe" clone ssh://<user>#<org>:<path to repo> in Git bash terminal and in normal command prompt.
When executed in Git Bash terminal, the clone works fine, but while running it in command prompt, it gives following error.
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
I need to run git clone through windows batch script, that's why
i need to make it run through command line. How to do that work ?
Note :-
1. Add public key to remote(gerrit) that was generated using ssh-keygen.
Your SSH identity is not found while running from command line. Make sure that %HOME%\.ssh directory contains your keys.

Shell script doesn't properly execute from ruby CGI script

I've got a ruby cgi script which calls a shell script.
The shell script does a git pull.
When I run the shell script from the command prompt it works.
But when I run it from the ruby cgi script it executes the script but the git pull doesn't happen.
I'm guessing it's possibly permissions related but I can't quite work out how to fix it.
The ruby script is:
#!/usr/local/rvm/rubies/ruby-1.9.3-p125/bin/ruby
require "cgi"
git_pull = `sh /github/do_git_pull.sh`
move_apanels = `sh /github/move_apanels.sh`
puts "Content-type: text/html\n\n"
puts "<html><body>We've done the following:<ul>"
puts "<li>#{git_pull.to_s}</li>"
puts "<li>#{move_apanels.to_s}</li>"
puts "</ul></body></html>"
And the shell script is:
#!/bin/bash
sudo sh -c cd /github
sudo sh -c git pull origin master
echo "Git Pull Completed"
Both files have chmod 777
Any ideas?
Doing this:
sudo sh -c cd /github
only changes the PWD for the duration of that sh command. It does not affect the current shell. You need to cd and git pull in the same subshell:
sudo sh -c 'cd /github && git pull origin master'
Setting 777 on your scripts won't cut it. Try and find out the user under which your ruby script executes the shell script. Since git uses SSH keys for authentication and normally your SSH keys can only be used by you, then git pull would fail if another user tries to do the git pull.
Check out this question on how to run a shell script as a different user.
Also make sure that the PATH in the target environment is set properly and accessible (if you run the web server chrooted).

Resources