6

I am using ec2 instance @ ubuntu . I am trying to automatically do "git pull" after i launched a new instance from my AMI. The repo dir is already in my AMI, all i need is update the repo.

what i am doing now is I put "git pull origin master" in rc.local.... but it doesn't work....

Wen
  • 401
  • 5
  • 13

5 Answers5

4

I got it to work..

sudo -u ubuntu -i git --git-dir=/home/ubuntu/blastoff/.git --work-tree=/home/ubuntu/blastoff/ fetch origin sudo -u ubuntu -i git --git-dir=/home/ubuntu/blastoff/.git --work-tree=/home/ubuntu/blastoff/ merge origin/master

Wen
  • 401
  • 5
  • 13
3

git --git-dir=some/dir/.git pull origin master should work

AJ.
  • 1,248
  • 10
  • 27
0

The right place to put the code is not /etc/rc.local/ but ~/.profile. You can then run commands as the logged in user without the need for sudo or su to change the user running the commands.

Zelphir Kaltstahl
  • 5,722
  • 10
  • 57
  • 86
0

Taking notes from https://stackoverflow.com/a/8880633/659188 and your working answer above, you could apply this to potentially multiple folders by doing something like this in your rc.local file (also only pulls current branch instead of always being master):

#!/bin/bash -e
# /etc/rc.local

# Ensure folders in array have a trailing slash!

declare -a folders=("/var/www/html/project1/" "/var/www/html/project2/" "/some/other/location/")

# Update to latest in all above folders

for i in "${folders[@]}"
do
        sudo -u ubuntu -i git --git-dir=$i/.git --work-tree=$i fetch origin
        sudo -u ubuntu -i git --git-dir=$i/.git --work-tree=$i pull
done

exit 0
Sators
  • 2,746
  • 1
  • 20
  • 26
0

If you want to do the git pull when the instance has been created (first boot), you could use cloud-init.

Check the AWS docs To pass a shell script to an instance with user data

This could be automated by using ansible/saltstack etc but for testing, you could manually upload your script. In step 3 "Configure instance" in the Advance Details, select option As file and put the script below.

enter image description here

You could upload there your custom script:

#!/bin/sh

echo "git pull or any other custom commands here"
nbari
  • 25,603
  • 10
  • 76
  • 131