[Support Guide] How to connect Azure DevOps repositories to Netlify

Hello friends!

We’ve released this functionality in-app as of 16 May 2022:

and

Advice below is the prior best practice to connect manually, which should no longer be needed in most cases.

Below is a video demo & docs on how to manually connect Azure DevOps repositories to Netlify.

Step 1. Initialize the Netlify site with the Netlify CLI

After installing the CLI, run the following command

netlify init --manual

This command will create a new site in Netlify and give you the ssh key you will need to plugin to your Azure DevOps account. That URL will look something like https://dev.azure.com/{your-user-name}/_usersSettings/keys. You can always grab this key again from https://app.netlify.com/sites/{your-site-slug}/settings/deploys#deploy-key

This netlify init --manual command will also give you back a deploy hook that looks like: https://api.netlify.com/hooks/{hookId}, this hook required a specific payload & is troublesome to work with in Azure DevOps, we can ignore this.

2. Create a new Netlify build hook.

Head into your newly minted Netlify site admin and create a new build hook.
https://app.netlify.com/sites/{your-site-slug}/settings/deploys#build-hooks
Take this new build hook URL and plug it into your Azure DevOps repo under settings & service hooks (See docs on creating service hooks. The service hooks admin URL will look something like https://dev.azure.com/{user-name}/{repo-name}/_settings/serviceHooks

Change code & verify builds in Netlify

Great job so far. Now let’s change our code in Azure DevOps and verify that out webhook is triggering a build inside of Netlify!
Visit https://app.netlify.com/sites/{your-site-slug}/deploys to verify the code change has triggered a Netlify build & deploy.

2 Likes

Hi David,
Thanks for the useful content. I followed you suggested step to run “netlify init --manual” command and create a Netlify site, however, no SSH public key is presented after the site creation. Could you tell me if I miss anything?


Choose a unique site name (e.g. null-makes-great-sites.netlify.app) or leave it blank for a random name. You can update the site name later.
? Site name (optional): {mysite}
Site Created
Admin URL: Netlify App
URL: https://{mysite}.netlify.app
Site ID: 401b9036-a0c9-4cfa-9b93-950ab0897aca
“{mysite}” site was created

To deploy to this site. Run your site build and then netlify deploy

Thanks in advance

Hi, @steve_wong. You can generate the SSH key here:

https://app.netlify.com/sites/{mysite}/settings/deploys#deploy-key

I also don’t know if the site is correctly linked to the repo. If the repo isn’t linked correctly, the key would be useless because we will never connect to the upstream repo.

I believe you can relink the repo using netlify link:

$ netlify link help
Link a local repo or project folder to an existing site on Netlify

USAGE
  $ netlify link

OPTIONS
  --auth=auth                    Netlify auth token
  --gitRemoteName=gitRemoteName  Name of Git remote to use. e.g. "origin"
  --id=id                        ID of site to link to
  --json                         Output return values as JSON
  --name=name                    Name of site to link to
  --silent                       Silence CLI output

If there are still issues after generating the key and linking the repo, please let us know.

This doesn’t work on netlify 2.55. Following the steps listed appears to link the Azure Repo to the Netlify site, but the tool doesn’t parse the repo url properly and the resulting repo URL is only the path portion of the actual URL.

For instance, if the git remote origin URI to your Azure Repo is Azure DevOps Services | Sign In, the tool stores the URI as myaccount/Project/_git/MyRepo, resulting in the following error:

Failed during stage 'preparing repo': error checking for ref: fatal: 'myaccount/Project/_git/MyRepo' does not appear to be a git repository
fatal: Could not read from remote repository.

This probably happens because your repo is specified in that https format. It needs to be specified in the ssh format instead. Not sure what this looks like for MS, but for github it’s:

git@github.com:login/project.git

Is there something similar for the MS situation? (You don’t have to use it otherwise, but it is the only thing we can use, so you need to have the repo specified like that in your .git/config when you link with us!)

https is the default protocol for Azure Repos, and I have GitHub repos linked to my Netlify sites using an https URI. If Azure Repos git remotes must be in ssh format, this document should clearly specify that at the beginning. Since creating ssh keys is not a standard Windows/Visual Studio operation, a link to a document on how to do that for Windows users wouldn’t go amiss, either.

You do not in fact have your GitHub repos linked to your netlify sites using an https URI.

We always fetch with deploy keys using SSH.

Our UI links to your commit via HTTPS, and specifies the repo in that format in our UI, but we require it to be in ssh format to use.

Good suggestion on the docs! Mind writing that up for us? I don’t use Azure Devops or windows myself, and neither do any of my team so it is unlikely we’ll create more docs for this unofficial support ourselves :slight_smile: . I will edit the article to mention the requirement for ssh, once I confirm it is true with the fellow who wrote this post.

Hey @DavidWells that’s works for me, thank you :slight_smile:

1 Like

HI there @DavidWells , thank you for this guide, it has been very helpful. I have a question around connecting multiple sites to a single repo. I have a single repo in Azure Devops which is currently linked to a Netlify site. I would like to link the repo to another different Netlify site in the same account. When I run:
“netlify init --manual” the cli responds: “This site has been initialized”.

Is there a way I can init a new site in Netlify and link it to the same repo?

Thank you.

The site link is stored in a text file with the site API id stored there. The path to this file is .netlify/state.json relative to the base directory of the repo itself.

For example:

$ cat .netlify/state.json
{
	"siteId": "5092fde1-c045-4222-86f3-bef6d75b1575"
}

Because of this, a single local repo directory can only be linked to a single Netlify site.

Now, you can link a single repo to multiple Netlify sites. The limitation is that using netlify link in the local repo directory links that specific directory to that specific site (via the .netlify/state.json file).

So, one workaround is to make multiple clones of the same repo (each to a different directory) and then link the repo to different sites that way. Each clone of the repo would have a different .netlify/state.json. (Note, directory .netlify should be in .gitignore. We do not recommend tracking those files in Git.)

However, there is probably a better way of doing this. For example, are you making manual deploys to different sites from a single local repo?

If the answer is “yes”, I would recommend not linking at all. Instead, just pass the site id to the netlify CLI command directly in the command line itself, like so:

netlify deploy --prod -d publish --site="API-ID-GOES-HERE"

Replacing “API-ID-GOES-HERE” with the actual API ID for the site.

​Please let us know if there are questions about either solution or if neither will meet your requirements.

Thank you for the response. In answer to your question, I currently have a repo that has an env variable called country which has two possible values. Changing this will produce a different site in terms of content. This is the only thing that would change between the two sites.

Currently the site that is already linked is set to deploy from manual (triggers a build and deploy when a merge to master is complete in Azure Devops). The build uses the settings in the netlify.toml file and the env value of country=AUS.

We would like to replicate this same process but for the env value of country=NZ.

So what you’re saying is there’s a couple of options:

  1. Clone the repo and link to a new site, this seems straight forward but would just mean writing a process to keep the repos in sync I think.

  2. Deploy via the CLI. If we went with the second option, would the site files have to be built locally via cli command before deploying? (FYI, it’s a Gatsby site)

Thank you for your help

***Edit, I ended up following your advice and cloned the repo into a different directory and then followed the steps from the video again. All working as intended now.

there would only be one repo - you’d link it to two sites. Then in each site’s build settings, you’d set a variable specific to each country. You can specify the rest of the per-site settings in netlify.toml in the repo, if you don’t want to worry about keeping them in sync :slight_smile:

(cf File-based configuration | Netlify Docs for the format of netlify.toml)

Azure repos doesn’t support git over ssh with LFS. Is there any way to link the repo with https?

Oh, wow, sorry to hear that. That would mean there is no way to use LFS there, then, I suppose.

Our Large Media service uses our own LFS service rather than your git provider’s, but it does have a fair number of caveats including not being able to link the same repo on two netlify sites and more: Large Media requirements and limitations | Netlify Docs

If none of those seem problematic to you, you could try using it since that should work around the missing feature at Azure Devops.

Hi I am coming from GitHub to Devops and was hoping there were some guidelines on how to setup hooks to build deploy previews and branch’s?

hi _Duncan, unfortunately, we don’t have any content for you on this topic at the moment! I’m sorry, I wish i had better news.

Hi everyone, I’m stuck following these instructions as every time I get to the last step configuring the Git SSH URL I get the following error:

The SSH URL of the remote git repo: git@ssh.dev.azure.com:v3/ignite-systems/Wintrix/ignite-portal
› Error: Failed updating site with repo information with error: Not Found

Anybody experience something similar? That URL is definitely valid and reachable as I git a git clone URL and it all works great.

Hey @GerardWilkinson,

Sorry to keep you waiting. Unfortunately, our first-hand support for GitHub, Gitlab and Bitbucket hasn’t quite extended to Azure! However, there are some handy guides out there with a couple of gotchas. Hopefully this helps and I look forward to hearing back from you!

1 Like

Hi David,

I have a question, I am able to do all the things that you instructed, using azure DevOps, however, I have a request could you also show us how to configure the deploy preview? basically, I want to deploy the pull request version as a preview

Thank you

hey richard, unfortunately, david left netlify some time ago! i will still leave your question open, but i am not sure who else might be able to assist, as azure questions are getting increasingly uncommon around these parts. if you make any progress, please do share it though!