[Newbie] Cloning my repo with stackscript

Hi,

I’ve been working hard on my stackscript this week and I have been making some progress with some help as well. This is the bit I am confused about. I would like to import my code from my private bitbucket git repo to my server automatically in the stackscript.

Firstly, I found this code in other peoples stackscripts. Then in this code, there was further code to grab the source from github:

cd /home/$USERNAME/
sudo -u $USERNAME wget "https://raw.github.com/gist/1210041/6f04294a6b80a6bf8d79a3530823d9449b8dee41/create_project.sh"
sudo -u $USERNAME chmod +x create_project.sh

What is the point of this create_project.sh? I just don’t get why we would want to fetch a script, to fetch the code, rather than just fetching the code to start with.

Secondly, if I want to clone my git repo to my server I was planning to use the following code:

cd /home/$USERNAME/
git branch live
git clone https://James@bitbucket.org/James/mywork.git
git push live

Now, again please appreciate this stuff is way over my head and I’m just trying to stumble through like an idiot to get this working. Could anyone offer me some advice as to the correct way to do this.

Regards,

James

8 Replies

````
GITURL=http://user:pass@bitbucket.org/user/repo.git GITDEST=/home/whatever

if [ ! -d "$GITDEST" ]; then git clone "$GITURL" "$GITDEST" else git --git-dir="$GITDEST/.git" pull origin master
fi
````

Stackscripts run as root on the linode being deployed, so no need to do any sort of sudoing. All that snippet does is check if the destination directory exists - if it does, it does a git pull to update (shouldnt really happen with a stackscript unless you run it more than once after the linode has been booted for the first time), otherwise it performs a clone on the repo.

As for the first point, there's no real reason to pull a script rather than doing it directly in the stackscript - if you need to bundle a load of different pieces of "bootstrap" code together, so it isn't just stuff that can simply fit in a single bash script in the stackscript manager, then obviously it's smarter to add it to a git repo and just clone it in the stackscript, then run it directly that way.

That you for the reply, that was really helpful. I did have another question. There seemed to be a way to do it without putting the password in the URL by adding a trusted SSH key. Does this method offer any advantages?

http://confluence.atlassian.com/display … +bitbucket">http://confluence.atlassian.com/display/BITBUCKET/Using+the+SSH+protocol+with+bitbucket

````

GITURL=http://"BITBUCKETUSERNAME":"BITBUCKETPASSWORD”@bitbucket.org/user/"BITBUCKETREPOSITORY".git
GIT_DEST=/home/whatever

if [ ! -d "$GITDEST" ]; then git clone "$GITURL" "$GITDEST" else git --git-dir="$GITDEST/.git" pull origin master
fi
````

Using the helpful code given to me by Maz, I created the following which allows you to put in your username, password, and repository when you deloy your server. I havent had chance to test this yet, but I want to release my entire stackscript on here so it will hopefully help others.

You've got some wierd utf-8 double quote character in there, bash might throw a fit about it.

You probably also want to UDF the GITDEST, and replace /user/ in the git url with BITBUCKETUSERNAME (as long as your bitbucket username is the same as the repo owner).

Fwiw I'm using something very similar in a stackscript to deploy using puppet - keep your configuration in git / bitbucket, clone it to the server on provision and then run puppet to apply the config - works pretty excellently.

As for ssh key auth, you can use it, but it would require you to download the private SSH key that has access to your repo before doing the git clone, which means you'd need to have it somewhere publically accessible (or passworded) and it basically achieves nothing - you still need some sort of authentication at some point using a username and password to grab it.

As long as the bitbucket username / password are UDF fields rather than hardcoded, security-wise it's about as much as you can do.

````

GITURL=http://"BITBUCKETUSERNAME":"BITBUCKETPASSWORD"@bitbucket.org/"BITBUCKETUSERNAME"/"BITBUCKET_REPOSITORY".git

if [ ! -d "GITDESTINATION" ]; then git clone "$GITURL" "GITDESTINATION" else git --git-dir="$GITDEST/.git" pull origin master
fi
````

Would this work? I removed git_dest equate and put it in as a UDF varaible. I also found and removed the double quote and also changed the user field, thanks for spotting those.

@Spadez:

# <udf name="bitbucket_username" label="BitBucket Username"> 
# <udf name="bitbucket_password" label="BitBucket Password"> 
# <udf name="bitbucket_repository" label="BitBucket Repository"> 
# <udf name="git_destination" label="GIT Deployment Destination" default="/home/whatever">

GIT_URL=http://"BITBUCKET_USERNAME":"BITBUCKET_PASSWORD"@bitbucket.org/"BITBUCKET_USERNAME"/"BITBUCKET_REPOSITORY".git 

if [ ! -d "GIT_DESTINATION" ]; then 
        git clone "$GIT_URL" "GIT_DESTINATION" 
else 
        git --git-dir="$GIT_DEST/.git" pull origin master 
fi</udf></udf></udf></udf>

Would this work? I removed git_dest equate and put it in as a UDF varaible. I also found and removed the double quote and also changed the user field, thanks for spotting those.

Everywhere you use an environment variable it needs a $:

# <udf name="bitbucket_username" label="BitBucket Username"> 
# <udf name="bitbucket_password" label="BitBucket Password"> 
# <udf name="bitbucket_repository" label="BitBucket Repository"> 
# <udf name="git_destination" label="GIT Deployment Destination" default="/home/whatever">

GIT_URL="http://$BITBUCKET_USERNAME:$BITBUCKET_PASSWORD@bitbucket.org/$BITBUCKET_USERNAME/$BITBUCKET_REPOSITORY.git"

if [ ! -d "$GIT_DESTINATION" ]; then 
        git clone "$GIT_URL" "$GIT_DESTINATION" 
else 
        git --git-dir="$GIT_DEST/.git" pull origin master 
fi</udf></udf></udf></udf>

Can you use "https" instead of "http"? for secuirty purposes?

My concern with this method is that I would be sending my username and password over http.

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct