I have a bash script (script1.sh) where I perform a git clone.
Then, from that repository, I run a another script (script2.sh) which runs fine.
I run script2.sh just fine, but the git repo is non existant. Any folder just isn't there. If I run the git clone on the command line, it clones it just fine.
Why is my script no git cloning my repo correctly?
How I run the first script.
sudo bash script1.sh
script1.sh
cd /home/ubuntu
git clone http://mygit-thing.com/myrepository.git localfolder
#run script from the repo
sudo bash localfolder/script2.sh
script2.sh
~ Some unrelated unix commands
Notes: I tried looking at the /home/ubuntu folder and could not find it. It's not a "hidden" file as well.
This could solve your problem replace your script1.sh:
home=/home/ubuntu
folder=$home/localfolder
git clone http://mygit-thing.com/myrepository.git $folder
#run script from the repo
bash $folder/script2.sh
if it does not work, it is maybe possible that you are not allowed to write on $home because of your current user permissions or because your fs is read-only You can check that by executing mount without option, it will list all mounted fs.
Another point, sudoing from inside a script is not recommended. Currently you are basically sudoing on a sudo. If you want to be sure the right user is executing your script, you better check the current id than doing nested sudo.
As #jibe suggested, you're calling bash from sudo, which will call bash from different location. Provide full path to the local git repository.
I have same issue during to write blue-green deployment script , I resolved calling with bash.
#!/bin/bash
bash /home/ubuntu/deployment-guru/green-deployment.sh
green-deployment.sh
#!/bin/sh
cd /home/ubuntu/my_api && git pull origin blue-green-jenkins-integration
before calling without bash i was facing following issue
Permission denied (publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights
and the repository exists.
Related
I am managing a website using git. One of the requirements for the git repository is that bare = true. It uses a post-receive hook to manage pushes from my local computer. The problem is that sometimes I would like to make changes to a WordPress directory on my website using the wp-admin view online. So then I would just ssh into the directory and run git --work-tree="BLAH" add . and git --work-tree="BLAH" commit -m "BLAH". Is there a way to set up an alias, like alias git="git --work-tree=\"BLAH\"" and have that work for all git commands?
There are times when alias are a great tool. Then there are times when things start getting too complicated where a shell script is better.
To create a single command that executes other commands just create a file (maybe call it git-add-all) then type the following:
#! /bin/bash
git --work-tree="BLAH" add .
git --work-tree="BLAH" commit -m "BLAH"
Then you can run the script by simply doing:
bash git-add-all
Even better, make the script executable:
chmod +x git-add-all
Then you can use it like any command:
./git-add-all
Advanced tips:
To be able to run the script from any git directory you can copy/move the file to one of the directories in your $PATH. For example /usr/loca/bin. Then you can simply run git-add-all instead of ./git-add-all.
Even better is to create your own personal scripts directory and include it in $PATH. I personally use ~/bin. To add the directory to $PATH you just need to add the following to .bashrc or .profile:
export PATH=/home/username/bin:$PATH
or if you're doing this for the root user:
export PATH=/root/bin:$PATH
In case anyone is curious how I solved it (thanks to shellter's comment), I wrote a bash script then prompted the user for input like so:
#!/bin/bash
function fix {
git --work-tree="PATH_TO_WORKING_TREE" $1
}
echo -n "git "
read -e INPUT
until [ "$INPUT" = "quit" ]; do
fix $INPUT
echo -n "git "
read -e INPUT
done
Running it:
user#server [repo.git] $ git-fix
git status
# On branch master
nothing to commit (working directory clean)
git quit
There is a .bashrc file in Linux. You can edit it for creating alias for your favorite and frequently used commands.
To create an alias permanently add the alias to your .bashrc file
gedit ~/.bashrc
The alias should look like:
alias al='cmd'
You can read more about it over here.
I have a project in which there is a .sh file and it has to be in the executable mode once pulled by the others. Now, I already changed its permissions on my local machine. However I want it to be pushed/pulled as executable as well, so that the other users do not have to run chmod command all the time.
I have been informed about two possibile solutions: .gitattributes and git update-index --chmod=+x script.sh, however I am not sure what exactly should I follow, given my condition.
I've seen this post here and this answer there, and I am thinking which one would suit my case more. I want this process to be done automatically, not by the user everytime, added.
Any thoughts?
Since you've tagged this question with linux, you can just check the file in. Now that you've changed it on your computer:
% chmod +x script.sh
Git will notice that the file has changed:
% git status
On branch old2
Changes not staged for commit:
(use "git add <file>..." to update what will be committed)
(use "git checkout -- <file>..." to discard changes in working directory)
modified: script.sh
And you can diff to see that the file is, in fact, now executable:
% git diff foo.sh
diff --git a/foo.sh b/foo.sh
old mode 100644
new mode 100755
Mode 100644 reflects a non-executable file, and mode 100755 reflects an executable file, similar to the Unix file permission bits. (Had you changed the file in addition to changing the permissions, you would also see the changes.)
When you check this in and your collaborators pull your changes, the file will be made executable for them, too.
Note that this does not work automatically on systems that do not have the notion of an execute bit (ie, Windows), and in that case, you will need to do something with update-index.
Note also that this relies on your system to have configured core.filemode being set correctly. This value should be set to true on all non-Windows systems.
% git config core.filemode
true
If, for some reason, that command returns false and you are on a non-Windows computer, run:
% git config core.filemode true
to re-enable it.
You have to decide which is fit for you. Two methods those you given above they can acceptable and they almost the same.
You should know how to give working permissions to executable files. In Linux, true way to do it is "chmod". Also you can sue git hooks as well. For doing in normal method I preferred this:
git add file.sh #this could be py file or something
git update-index --chmod=+x file.sh #make it executable
git commit -m "here the commit" #commit it
git push #push it
So if you want to do it another way you should try this:
#!/usr/bin/bash
chmod +x file.sh
And you can run it. But before the run it, you should give working permissions to your script that you made for giving working permissions to other scripts :)
Or you can give the permissions dynamically:
su -c 'chmod +x file.sh'
In this way, you should give the working permission for one time and it runs.
Did you consider Git hooks?
Maybe this could be solution for you
.git/hooks/post-checkout:
#!/bin/sh
chmod +x script.sh
Here you can find more about Git hooks.
I've looked this up and found lots of answers but I am a unix/linux dummy. Not able to follow peoples instructions. Not sure which files to edit etc..
need simple step by step instructions here
I have a shell file set up like so:
#!/bin/bash
exec &>> /var/www/nginx/yokohama/laravel/storage/cron.log
today='date +%Y-%m-%d.%H:%M:%S';
cp /var/www/nginx/yok/yok_data.xml /var/www/nginx/yok/yok_XML_Files/backup-$(date +"%Y_%m_%d").xml
git add /var/www/nginx/yok/yok_XML_Files/backup-$(date +"%Y_%m_%d").xml
git commit -m "cool test"
git push origin staging
if I run it manually as root it works and we're all good. but doesnt work through my cron..
here is my crontab
* * * * * /usr/bin/sh /var/www/nginx/yok/laravel/commit_data.sh
In my cron.log I'm getting
Permission denied (publickey).^M
How can I fix this? Simple instructions.
Permission denied (publickey).^M
The ^M at the end that your script might have \r\n eol (end of lines) characters instead of \n: try dos2unix.
Also make sure your script is executable:
chmod 755 /var/www/nginx/yok/laravel/commit_data.sh
The OP hamobi mentions another reason in the comments:
problem even more basic. had to move users rsa key into bitbucket.
I was reading an article that told me to add a file and place it in my path. Not knowing what the author meant by path, i simply put it in my root directory.
Trying to run 'git diffall', git says diffall is not a command, any ideas? Thanks in advance.
The article snippet for more information:
Write the following code to a file called git-diffall and place in your path (I put it in >…/my-git-install-dir/cmd/ )
#!/bin/sh
git diff --name-only "$#" | while read filename; do
git difftool "$#" --no-prompt "$filename" &
done
And run it in git (with usual diff input parameters), for example:
git diffall
git diffall HEAD
your 'path' is the collection of directories where the system looks for executables. To see it, simply execute echo $PATH at the commandline. Then put your script in one of those directories.
I've just thrown together the following shell script:
cd /home/firefli/webprojects/project1
svn checkout file:///home/firefli/svn/project1/trunk .
rm -rf /home/firefli/public_html/project1
svn export . /home/firefli/public_html/project1
It does work when I do a commit and then run the script manually but I still have a couple of questions.
Can I run a bash script, or does it have to be C? (I've seen lots of C examples)
Where do I put it to make it execute post-commit?
There is a hooks directory inside your Subversion repository. It should contain a number of templates that you can modify and use.
Your script can happily be a bash script. The provided templates use /bin/sh
Just remove the .tmpl extension and you're good to go.
The Subversion docs provide more info here