Deploy the github repo using api

Hi, all.
Hope you are going well.

I am new to netlify api. I have referred to netlify api docs. I want to deploy my own github repo using api. There are no info about it.

Can anyone guide me to do that.

Hoping with a positive response.

Regards, shiva.

Hey @shiva,
Are you interested in deploying a site to Netlify via the API or specifically integrating with Github via the API?

Here’s a blog post about the first:

And here’s a support guide about the second:

Let us know if that helps or if you have other questions!

Hey, @jen. Thankyou.

It worked fine.

The support guide is worked for me. I have deployed my site which is in GitHub using API.

But I have to do same thing with private GitHub repo. Is there any way to do that.

Hoping with a positive response.

Regards, shiva.

@jen,

Can you please say this

Hey @shiva,
Sorry for the delay here! We host many sites that are deployed from private repositories, so linking one should absolutely be possible. Are you running into any errors? Any details about what you’ve tried so far would be super helpful.

@jen,
Thanks for the reply.

I used the following code to deploy my site

    var settings = {
        "url": "https://api.netlify.com/api/v1/sites",
        "method": "POST",
        "timeout": 0,
        "headers": {
          "Authorization": "Bearer "+token,
          "Content-Type": "application/json"
        },
        "data": JSON.stringify({"name":sitename,"repo":{"provider":"github","id":305328387,"repo":"Shiva-sandupatla/testrepo","private":false,"branch":"main","deploy_key_id":public_key}}),
      };
      console.log({"name":sitename,"repo":{"provider":"github","id":305328387,"repo":"Shiva-sandupatla/testrepo","private":true,"branch":"main","deploy_key_id":public_key}});
      
      $.ajax(settings).done(function (response) {

        console.log(response);

      }).fail(function(e){

          console.log(e);

      });

In the above code sitename , token and public_key are the variables

When I request, I got success response as below

{"id":"788c169b-799c-4f60-83db-0675bf9d0a7b","site_id":"788c169b-799c-4f60-83db-0675bf9d0a7b","plan":"nf_team_dev","ssl_plan":null,"premium":false,"claimed":true,"name":"dddddddd","custom_domain":null,"domain_aliases":[],"password":null,"notification_email":null,"url":"http://dddddddd.netlify.app","admin_url":"https://app.netlify.com/sites/dddddddd","deploy_id":"","build_id":"5f95155f9026889226bdd1c2","deploy_url":"http://5f9515609026889226bdd1c8--dddddddd.netlify.app","state":"current","screenshot_url":null,"created_at":"2020-10-25T06:04:15.654Z","updated_at":"2020-10-25T06:04:15.713Z","user_id":"5f088353e996d5023cffc741","error_message":null,"ssl":false,"ssl_url":"https://dddddddd.netlify.app","force_ssl":null,"ssl_status":null,"max_domain_aliases":100,"build_settings":{"cmd":null,"dir":null,"env":{},"created_at":"2020-10-25T06:04:15.688Z","updated_at":"2020-10-25T06:04:15.688Z","private_logs":null,"allowed_branches":["main"],"functions_dir":null,"installation_id":null,"skip_prs":null,"untrusted_flow":"review","base_rel_dir":true,"stop_builds":false,"public_repo":true,"provider":"github","repo_type":"git","repo_url":"https://github.com/Shiva-sandupatla/testrepo","repo_branch":"main","repo_path":"Shiva-sandupatla/testrepo","base":null,"deploy_key_id":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDPWw2IqbvzVH2c+J5pY5xIaVZz2SrCtIbfwRSfNu87YIivGtsabN4SRjkyDC4mias8Uc7IIj0c7jDiXX0cAzrILbQQNPPoQeaSb+gOZkYkeeM7dD4UlAPQepT6LBMWZSbQS3+3v+TcyWu501rh4Gn/xoyE5QVE8tQRtHWitfdOw5woMuPO49ZIBlIsociSXvEoGn25W53Jc47nG+TcNoDYbnrsOFDNbJWAoixhd3YeS1N3jiZxU1z9imqf/JZ9RuzWNy91KcT2s6Eg4BjD4umzK1L3liISXdHXUKBzWkurL9+CQTH6lI46F29wbolsInPBL4HeMF8bDxOFh6rjo7KlAYERI0JMkISUm9XByn0t3QGy32BsM6erW8gYYQPlsJnZCjFSz8N5UFJpWXshEErcLKwkC/DC5hEGir2mROXHn8lhowpiOSpYeZtKx4LRdTbp5CUZgD+hIWxnyXyviQKalpXS8z3x2P1wsw9sj/0HTTgMM1scvcZd/nl4HdvYm6FT5ct1pi9vqht1HGaTu3s4QXm+DEyLKg/oIpO6JLbQ5L3mM8q2GD9xyHGdseGcu54/ddtl/O+Hh1Rmz4Uu+ZHRM9K+O1wTuNKXYDyMiI2yLUv9RAV0ZUvhYWKO1j8IYePlyHJPDniZe6gUXfisO2xV+oVPA1yuzEy4K6rvo0Q+fQ=="},"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":"shiva-team","account_name":"Shiva","account_type":"Starter","capabilities":{"title":"Netlify Team Free","asset_acceleration":true,"form_processing":true,"cdn_propagation":"partial","build_node_pool":"buildbot-external-ssd","domain_aliases":true,"secure_site":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":"788c169b-799c-4f60-83db-0675bf9d0a7b.netlify.app","use_lm":null,"build_image":"xenial","automatic_tls_provisioning_expired":false,"analytics_instance_id":null,"functions_region":null,"functions_config":{"site_created_at":"2020-10-25T06:04:15.654Z"},"plugins":[],"default_domain":"dddddddd.netlify.app"}

But when i open my dashboard in the website it says deployment failed.

Here is the deploy log:

11:45:07 AM: Build ready to start
11:45:08 AM: build-image version: b0258b965567defc4a2d7e2f2dec2e00c8f73ad6
11:45:08 AM: build-image tag: v3.4.1
11:45:08 AM: buildbot version: 0a44c930054be748eff0c581d346282fece89cc0
11:45:08 AM: Fetching cached dependencies
11:45:08 AM: Failed to fetch cache, continuing with build
11:45:08 AM: Starting to prepare the repo for build
11:45:09 AM: User git error while checking for ref refs/heads/main
11:45:09 AM: Failing build: Failed to prepare repo
11:45:09 AM: 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
11:45:09 AM: Finished processing build request in 677.152865ms

Hoping with a positive response.

Regards, shiva.

Hey @shiva,
It looks like you deleted this site since writing in? Please let us know if you have a different site you’d like us to take a look at!

Hey @jen,

Sorry for that.

Here is the code and response of the website have a look at it.

code:

    var settings = {
        "url": "https://api.netlify.com/api/v1/sites",
        "method": "POST",
        "timeout": 0,
        "headers": {
          "Authorization": "Bearer "+token,
          "Content-Type": "application/json"
        },
        "data": JSON.stringify({"name":sitename,"repo":{"provider":"github","id":305328387,"repo":"Shiva-sandupatla/testrepo","private":false,"branch":"main","deploy_key_id":public_key}}),
      };
      console.log({"name":sitename,"repo":{"provider":"github","id":305328387,"repo":"Shiva-sandupatla/testrepo","private":true,"branch":"main","deploy_key_id":public_key}});
      
      $.ajax(settings).done(function (response) {

        console.log(response);

      }).fail(function(e){

          console.log(e);

      });

Response:

{"id":"2497a7c8-7b21-4c28-b876-a90008c3c172","site_id":"2497a7c8-7b21-4c28-b876-a90008c3c172","plan":"nf_team_dev","ssl_plan":null,"premium":false,"claimed":true,"name":"test-deploy-by-shiva","custom_domain":null,"domain_aliases":[],"password":null,"notification_email":null,"url":"http://test-deploy-by-shiva.netlify.app","admin_url":"https://app.netlify.com/sites/test-deploy-by-shiva","deploy_id":"","build_id":"5f9bbaa8a32410b4bdb40851","deploy_url":"http://5f9bbaa8a32410b4bdb40857--test-deploy-by-shiva.netlify.app","state":"current","screenshot_url":null,"created_at":"2020-10-30T07:03:04.041Z","updated_at":"2020-10-30T07:03:04.085Z","user_id":"5f088353e996d5023cffc741","error_message":null,"ssl":false,"ssl_url":"https://test-deploy-by-shiva.netlify.app","force_ssl":null,"ssl_status":null,"max_domain_aliases":100,"build_settings":{"cmd":null,"dir":null,"env":{},"created_at":"2020-10-30T07:03:04.067Z","updated_at":"2020-10-30T07:03:04.067Z","private_logs":null,"allowed_branches":["main"],"functions_dir":null,"installation_id":null,"skip_prs":null,"untrusted_flow":"review","base_rel_dir":true,"stop_builds":false,"public_repo":true,"provider":"github","repo_type":"git","repo_url":"https://github.com/Shiva-sandupatla/testrepo","repo_branch":"main","repo_path":"Shiva-sandupatla/testrepo","base":null,"deploy_key_id":"ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAACAQDGPqkoSwl3Ao+0PQSy5mToe5ZB97EAOnI9PgEo0P+kTNQvpsFQVzQMtsAJkHQqFR+4QR4Pxyw0MnEiKAiHzlnMpmMyRV9q8A/7HTlBj5SjZZRu/wPmyc0uXSlDbUOYvqXvqztckTZJV9sFjXsUIOFiiovEYRUB5fbeZZjHxeGREGqPjjXYSUmCzMBuBbb7PtZsMrbM1yiMy8Vm8DwHQmfOlETnFXIR8dhUPaSrucIsU+xXN7dW6MpQr+4DSAvTE5xSazWNlLIDUDrXvMJjRtPDW7eOBJ08lG9qKyKmIXeAlTWdUKTKn7K3Hi3ywHHi6b8ZF7dTOnE5gxrCpn9sWhleu5QJif0dypKHyCilak9njIjGiq/X6l0ZelnPI591xfPsTxdn0ytky9Ig676SANZvUayfpL7vlGv04VhgBcpo3zWTCBJ1ide4xCetzjD2pRjugtfxoNnh5g5BsauPeimx3Kkfh9WFsvQBbMcBLf7RYoIwaQRMxegDAPj+KFdnMUMflhAgOvrHe32A6x05dyhxHJ9gW5qJzI/1e5hXbT0l2A96JN6k7OtUVp/ei4UyskJRwgT10gKR+WfgEW2Vt2fGh0s4rgCnxDrYx3X8CFqCR5qj/wMMFKps7KSN2WcZrUJ2zGDTMRfWv8j+IDWtXshR2qvre4WYeCWMUM+EWNp9kw=="},"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":"shiva-team","account_name":"Shiva","account_type":"Starter","capabilities":{"title":"Netlify Team Free","asset_acceleration":true,"form_processing":true,"cdn_propagation":"partial","build_node_pool":"buildbot-external-ssd","domain_aliases":true,"secure_site":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":"2497a7c8-7b21-4c28-b876-a90008c3c172.netlify.app","use_lm":null,"build_image":"xenial","automatic_tls_provisioning_expired":false,"analytics_instance_id":null,"functions_region":null,"functions_config":{"site_created_at":"2020-10-30T07:03:04.041Z"},"plugins":[],"default_domain":"test-deploy-by-shiva.netlify.app"}

Please have a look at it.

Regards, shiva.

@jen, please have a look at it.

Hi @shiva. I may be wrong but I think you’re supposed to use the deploy key id for the deploy_key_id argument not the public key which starts with ssh-rsa. If you look at the support thread the id they use there is much closer to the deploy_key_id.

Having said that, I found this thread because I have a similar issue using the deploy key id:

4:39:29 PM: Failed during stage 'preparing repo': error checking for ref: Warning: Permanently added 'github.com' (RSA) to the list of known hosts.
1 Like

For anyone with the same issue I had, the GitHub account I was linked to had a restriction on the repositories that the Netlify GitHub app could access. I changed this to all repositories and it worked:

EDIT: I thought this had fixed it as it worked after I changed it but it seems it’s not working for me again and is coming up with the above error again.

EDIT2: Turns out you have to create a deploy key on GitHub using the public_key that gets returned with the deploy_key_id. You can see how to do this in the GitHub API docs here.

1 Like

Hey @shiva,
@jamesalester is absolutely correct, that you will want to use the deploy key id in your call to Netlify instead of the public key. There are instructions for getting both values in the support guide I linked above. Please let us know if that doesn’t work for you.

Hey @jen, @jamesalester

I have tried it but I got the logs as below:

7:40:55 PM: Build ready to start
7:40:57 PM: build-image version: 253f72909528f25a82c4640ca58f2e974d52abd9
7:40:57 PM: build-image tag: v3.4.4
7:40:57 PM: buildbot version: d7fbddb77714d524c4b4de0312067b122124afee
7:40:57 PM: Fetching cached dependencies
7:40:57 PM: Failed to fetch cache, continuing with build
7:40:57 PM: Starting to prepare the repo for build
7:40:57 PM: User git error while checking for ref refs/heads/main
7:40:57 PM: Failing build: Failed to prepare repo
7:40:57 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
7:40:57 PM: Finished processing build request in 723.608083ms

If you don’t mind can you please provide me a detail procedure on deploying my own private repo.

Hoping with a positive response.

Regards, shiva

Thanks alot @jen & @jamesalester,

I have been trying deploy_key_id by without putting it in the github deploy keys using api.

In the documentation the procedure is not clear.

In future if anyone want the fully detailed procedure of deploying private repo using netlify api head to this

once again i thank @jen & @jamesalester

1 Like