Unable to deploy a private repo using the API

PLEASE help us help you by writing a good post!

  • we need to know your netlify site name. Example: gifted-antelope-58b104.netlify.app
  • DNS issues? Tell us the custom domain, tell us the error message! We can’t help if we don’t know your domain.
  • Build problems? Link or paste the FULL build log & build settings screenshot

The better the post - the faster the answer.

I am creating a website using the API and linking a Github repo at the same time. Here’s the payload I am sending

data = { 
    "name": name,
    "repo": {
        "branch": "master",
        "private": True,
        "provider": "github",
        "repo": f"my_github_account/my_repo_name",
    }   
}  

headers = {"Authorization": "Bearer the_actual_netlify_token"}
response = requests.post("https://api.netlify.com/api/v1/sites", headers=headers, json=data)

print(response.status_code)
# HTTP 201
print(response.json())

Here’s the response I get

{
	"id": "d1c545fe-b3c4-4230-8f2c-bcf0626174d5",
	"site_id": "d1c545fe-b3c4-4230-8f2c-bcf0626174d5",
	"plan": "nf_team_dev",
	"ssl_plan": null,
	"premium": false,
	"claimed": true,
	"name": "askdjfiwa20",
	"custom_domain": null,
	"domain_aliases": [],
	"password": null,
	"sso_login": false,
	"sso_login_context": "all",
	"notification_email": null,
	"url": "http://askdjfiwa20.netlify.app",
	"admin_url": "https://app.netlify.com/sites/askdjfiwa20",
	"deploy_id": "",
	"build_id": "63a7efcbf5a3690f6663d8fe",
	"deploy_url": "http://63a7efccf5a3690f6663d903--askdjfiwa20.netlify.app",
	"state": "current",
	"screenshot_url": null,
	"created_at": "2022-12-25T06:38:03.464Z",
	"updated_at": "2022-12-25T06:38:03.656Z",
	"user_id": "63939517a9cfed008e5ed4cd",
	"error_message": null,
	"ssl": false,
	"ssl_url": "https://askdjfiwa20.netlify.app",
	"force_ssl": null,
	"ssl_status": null,
	"max_domain_aliases": 100,
	"build_settings": {
		"cmd": null,
		"dir": null,
		"env": {},
		"created_at": "2022-12-25T06:38:03.650Z",
		"updated_at": "2022-12-25T06:38:03.650Z",
		"private_logs": null,
		"allowed_branches": ["master"],
		"functions_dir": null,
		"installation_id": null,
		"skip_prs": null,
		"untrusted_flow": "review",
		"base_rel_dir": true,
		"stop_builds": false,
		"public_repo": false,
		"skip_automatic_builds": null,
		"provider": "github",
		"repo_type": "git",
		"repo_url": "https://github.com/sahilklanto/askdjfiwa20",
		"repo_branch": "master",
		"repo_path": "sahilklanto/askdjfiwa20",
		"repo_owner_type": "User",
		"base": null,
		"deploy_key_id": null
	},
	"processing_settings": {
		"css": {
			"bundle": true,
			"minify": true
		},
		"js": {
			"bundle": true,
			"minify": true
		},
		"images": {
			"optimize": true
		},
		"html": {
			"pretty_urls": true
		},
		"skip": true,
		"ignore_html_forms": false
	},
	"prerender": null,
	"prerender_headers": null,
	"deploy_hook": "https://api.netlify.com/hooks/github",
	"published_deploy": null,
	"managed_dns": true,
	"jwt_secret": null,
	"jwt_roles_path": "app_metadata.authorization.roles",
	"account_slug": "sahil-yz6lmhg",
	"account_name": "sahil-yz6lmhg's team",
	"account_type": "Starter",
	"capabilities": {
		"title": "Netlify Team Free",
		"asset_acceleration": true,
		"form_processing": true,
		"cdn_propagation": "partial",
		"domain_aliases": true,
		"secure_site": false,
		"sso_secure_site": false,
		"secure_site_context": false,
		"prerendering": true,
		"proxying": true,
		"ssl": "custom",
		"rate_cents": 0,
		"yearly_rate_cents": 0,
		"ipv6_domain": "cdn.makerloop.com",
		"branch_deploy": true,
		"managed_dns": true,
		"geo_ip": true,
		"split_testing": true,
		"id": "nf_team_dev",
		"cdn_tier": "reg"
	},
	"dns_zone_id": null,
	"identity_instance_id": null,
	"use_functions": null,
	"use_edge_handlers": null,
	"parent_user_id": null,
	"automatic_tls_provisioning": null,
	"disabled": null,
	"lifecycle_state": "active",
	"id_domain": "d1c545fe-b3c4-4230-8f2c-bcf0626174d5.netlify.app",
	"use_lm": null,
	"build_image": "focal",
	"automatic_tls_provisioning_expired": false,
	"analytics_instance_id": null,
	"functions_region": null,
	"functions_config": {
		"site_created_at": "2022-12-25T06:38:03.464Z"
	},
	"plugins": [],
	"account_subdomain": null,
	"functions_env": {},
	"cdp_enabled": true,
	"cdp_enabled_contexts": ["deploy-preview", "branch-deploy"],
	"authlify_token_id": null,
	"build_timelimit": null,
	"use_custom_domain_in_all_contexts": false,
	"uses_new_env_var": true,
	"deploy_retention_in_days": 90,
	"password_context": "all",
	"use_envelope": true,
	"default_domain": "askdjfiwa20.netlify.app"
}

As you can see the site is created successfully. However when I look at deploys, it gives out this error

Failed during stage 'preparing repo': error checking for ref: : fatal: could not read Username for '[https://github.com](https://github.com/)': No such device or address

Here’s the full log

12:41:59 PM: build-image version: d55944c047ef0aee2ac01971edde72cb2a50acfe (focal)
12:41:59 PM: build-image tag: v4.16.1
12:41:59 PM: buildbot version: 33f88475bcf3426608b49b7681d295235efb41f1
12:41:59 PM: Fetching cached dependencies
12:41:59 PM: Failed to fetch cache, continuing with build
12:41:59 PM: Starting to prepare the repo for build
12:41:59 PM: User git error while checking for ref refs/heads/master
12:41:59 PM: Failing build: Failed to prepare repo
12:41:59 PM: Failed during stage 'preparing repo': error checking for ref: : fatal: could not read Username for 'https://github.com': No such device or address
: exit status 128
12:41:59 PM: Finished processing build request in 531.873764ms

Have you configured the Netlify GitHub App to be able to access your repo?

Hey man, I actually started passing deploy_key in my payload and the deploys started working on my website. However, apparently the deploy_key is now expired and I need to generate a new one? Can you point me to the docs about deploy keys? Thanks!

Hi :wave:t6: , to generate a new key please do the following:

To generate a new key pair for a Netlify site, go to Site settings > Build & deploy > Continuous deployment > Deploy key, and select Generate public key . Netlify will store the private key, and provide the public key for you to add to the repository settings for your submodule.

You can read more about this in our docs article here.

Hey, I actualy did follow that step, I had created a deploy key, started passing it to the createSite endpoint and it worked. I added the public key to my github account as well.

My question is that the deploy keys weren’t there when I queried the listDeployKeys endpoint. I just want to know if there’s an expiration period on the deploy keys we create?

Thanks

Edit - I really love the product BTW and the response time on forums is really good too. Kudos to you folks!

Heya @sahil1 ,

We’re pretty sure that that endpoint will only return deploy keys that you’ve personally created on Netlify, rather than ones that your teammates have created.
Finally, this also wouldn’t show deploy keys created with API tokens, which were created by your teammates, rather than you (unless you also run the listDeployKeys call with their token!) .

I’m also not clear if it returns across account boundaries or “just from your default account”, but since deploy keys stored in our system aren’t scoped by account but instead by user, I think that should be “all of your keys”.

As Hrishikesh mentioned, the preferred method for connecting GitHub and Netlify these days is using the GitHub App we wrote - this means you don’t need deploy keys at all. Are you willing/able to use it instead? Then you just pass the (same) installation_id with every site creation rather than needing a deploy key for every site - which is how our system intends you to work if you do use deploy keys (rather than using the same key with multiple sites which seems like it would be handy, but which GitHub doesn’t allow.)

Hey, I am creating a deploy key via the API using my own token. I had created a deploy key yesterday and now when I queried the listDeploykeys endpoint there’s nothing there. Here’s the deploykey I had created if that helps

63ab37f788ccc4462e133765

Here’s the one that was created two days ago that also just dissapeared

63a813f30a3d6b1f55c26f16

I am on a free plan, is that why it’s happening?

Also, WRT the Github app, I had installed and it and the site that has a private repo linked to it, still fails to deploy. So I am not sure what am I doing is wrong?

Just an FYI, I am creating a site using the API itself, here’s the payload that I pass

data = { 
    "name": "name_of_site",
    "repo": {
        "repo": "githubusername/github_repo_name",
        "repo_id": "id of the repository",
        "private": True,
        "deploy_key_id": "my deployment key",
        "branch": "master",
        "provider": "github"
    }   
}   

EDIT - I just created ANOTHER deploy key (which I am sure will disappear for some reason the next day) and the deploys are working now. Just tell me why the deploy keys just disappear suddenly.

Thanks

Hey, It happened again. My deploy key got deleted AGAIN. Can you please tell me if the deploy key is attached to any site that already exists? Cause I have been creating and deleting websites a bunch of times after creating the deploy key.

Hey @fool , guess what? It happened AGAIN. The deploy key I created by making a call like this

curl -X POST -H “Authorization: Bearer MyAuthToken” https://api.netlify.com/api/v1/deploy_keys

that returned a deploy key with the public key, just disappeared.

Here’s the deploy key I got 63ac0020feea0b4d11a197e8 before it went away.

Could you please let me know why this might be happening?

Again, I am creating and deleting sites because I am testing my code for my project. Could that be the reason why it might be happening? or is it because I am on a free plan? Whatever it is, I’d like a concrete answer before I upgrade to a paid plan.

Thanks

Hi, @sahil1. The deploy key is specific to the site. If you delete the site, you delete the deploy key.

About the initial repo clone not working, is because the repo object requires the GitHub app installation id in the installation_id attribute on the repo object.

The id itself comes the GitHub API. There are two ways to find the correct value.

Either:

  • link a repo in the web UI and examine the installation_id in the network tab of devtools

or:

For example, if the new URL in the address bar was:

https://github.com/settings/installations/123456

The the installation_id would be 123456. Please note that value is an integer and not a string so it should not be quoted when sending it in JSON to the Netlify API.

If you use the correct installation_id there should not be any reason to use deploy keys. (The Netlify GitHub app removes the requirement for the deploy key as now the GitHub app controls the access.)

​Please let us know if that does not work or if there are any other questions.

1 Like

Hey @luke , thanks for the reply. I’ll try to use installation_id and see if it works.

Just some clarifications on the deploy_key front. Say I create a deploy_key with one website up and running on my account, let’s call it Website1. Will the deploy key belong to Website1 and will get deleted if Website1 gets deleted?

OR

Say I create the deploy_key and use it to create a website, Website2, will the deploy_key belong to Website2 now and be deleted if Website2 is deleted?

Essentially, what I am asking is, when I create the deploy_key, to which site does it belong to? The one that was already there when it was created. Or the one that was created with the deploy key.

Thanks

We recommend using 1 deploy key per site. That is how we do it, and how we intend you to do it. I don’t know the answer to your question, since the answer to the situation is “it doesn’t matter if you use the keys as we intend” :slight_smile:

There are no cases where we’d create a deploy key for you automatically these days - we don’t use them ourselves anymore, preferring the github app and its installation_id specifically is the same for every site so it can be more easily programmed to :wink:

We’ve heard of customers using one key for multiple sites, and seen them have trouble doing so, so we strongly discourage any workflow like that, since it is not intended nor supported.