Joel B
Joel B

Reputation: 882

Scripting "git remote add ..." in a grunt task

I'm in the process of creating a deployment script that deploys a Django app to a remote server. This is being done by pushing a git repository to that remote server. To make it as easy as possible on the other developers, I'm trying to automate as much as possible via a grunt task. The end goal of the grunt task is to set up everything necessary, push to the remote repository, and clean up after itself.

So far, everything seems to be working fairly well, except one small thing. In order to set up the remote repository, I'm calling:

git remote add deploy ssh://<ServerUsername>@<serverIp><RepositoryDirectory>

...where each value is properly substituted in.

When running the grunt task a second time, git complains that the remote repository has already been set up:

fatal: remote deploy already exists.

Because this returns a non-zero value, the rest of the grunt script fails. What I'm wondering is: Are there any sort of command line arguments that I can pass to "git remote ..." that will update or create the reference to the remote repository without complaining? Barring that, is there any way to check via the command line that a particular reference to a remote git repository has already been created?

Upvotes: 0

Views: 94

Answers (1)

gturri
gturri

Reputation: 14629

A simple solution would be to keep using url rather than remote. In bash it would meaning replacing

myRemote=ssh://<ServerUsername>@<serverIp><RepositoryDirectory>
git remote add deploy $myRemote
git push deploy master

by

myRemote=ssh://<ServerUsername>@<serverIp><RepositoryDirectory>
git push $myRemote master

That being said, if you want to add the remote only if it doesn't already exist, you could do, in bash

if ! git remote | grep deploy >/dev/null; then
  git remote add deploy $myRemote
fi

Upvotes: 1

Related Questions