hashicorp / terraform-provider-netlify

Terraform Netlify provider. Please note: This Terraform provider is archived per our provider archiving process: https://terraform.io/docs/internals/archiving.html
https://registry.terraform.io
Mozilla Public License 2.0
56 stars 35 forks source link

Provider fails to create a proper connection to a GitLab repository #14

Open mojavelinux opened 5 years ago

mojavelinux commented 5 years ago

When I try to use the netlify provider to establish a link between Netlify and a GitLab repository, it doesn't seem to work. The site gets set up and the repository is populated with both the web hook and the deploy key, but the connection seems broken.

Here are the two problems I observe:

  1. Netlify won't acknowledge an event from the web hook (presumably because it doesn't think there's an association with that repository)
  2. Netlify cannot clone a private repository (the build only works if the repository is public)

I'm confident there's a problem with the linkage between Netlify and GitLab because I see a lock icon next to the repository on the Build Settings page. If I edit the Build Settings and reestablish the link to the repository, the lock goes away and everything starts working. (But then, I didn't use Terraform to set it up, so it defeats the whole point of using Terraform).

Here's the configuration I'm using:

provider "gitlab" {
  token = "${var.gitlab_token}"
}

provider "netlify" {
  token = "${var.netlify_token}"
}

resource "gitlab_project" "docs_ui" {
  name = "Docs UI"
  path = "docs-ui"
  namespace_id = 0123456
  visibility_level = "private"
  description = "A project that generates the UI for the documentation."
  issues_enabled = false
  wiki_enabled = false
  snippets_enabled = false
}

resource "netlify_deploy_key" "docs_ui" {}

resource "netlify_site" "docs_ui" {
  name = "${gitlab_project.docs_ui.path}-random-string-here"
  repo {
    repo_branch = "master"
    deploy_key_id = "${netlify_deploy_key.docs_ui.id}"
    dir = "public"
    provider = "gitlab"
    repo_path = "${substr(gitlab_project.docs_ui.web_url, length("https://gitlab.com/"), -1)}"
  }
}

resource "gitlab_deploy_key" "docs_ui" {
  project = "${gitlab_project.docs_ui.id}"
  title = "Netlify Deploy Key"
  key = "${netlify_deploy_key.docs_ui.public_key}"
}

resource "gitlab_project_hook" "docs_ui" {
  project = "${gitlab_project.docs_ui.id}"
  url = "https://api.netlify.com/hooks/gitlab"
  enable_ssl_verification = true
  push_events = true
  merge_requests_events = true
}

If I can guess, the problem seems to be that Netlify is never given any auth information for GitLab. If that's possible, I don't understand where that is supposed to be set.

I'd be happy to contribute an GitLab example for the README if I can get it working.

mojavelinux commented 5 years ago

I did a lot more research on this and I think I've figured out what's going on. I'm happy to report that Terraform is not to blame.

The first problem has to do with the webhook. It appears that when a Netlify site is created using the open-api, Netlify refuses to recognize events from the webhook (at least, not the one for GitLab). (This is where that lock icon comes in).

I verified this theory by using the Netlify API directly to create the site. Assume in this case the site is public, so no deploy key is required.

const Netlify = require('netlify')
const client = new Netlify(process.env.NETLIFY_TOKEN)

;(async () => {
  await client.createSite({
    body: {
      name: 'name-of-site-1234567890',
      repo: {
        provider: 'gitlab',
        repo_path: 'organization/name-of-site',
        repo_branch: 'master',
      } 
    } 
  })
})()

The site gets set up and builds correctly. However, changes to the repository reported by the webhook (assuming one is already in place) are not detected.

This appears to be a bug in Netlify and I'll report it there.

The second issue was a user error regarding the deploy key. I had reported that Terraform does not setting up the site correctly when connecting to a private repository. This statement is incorrect. Instead, it turned out to be an ordering problem.

Netlify determines it is working with a private repository if:

a) A deploy_key_id is specified b) The deploy_key works at the time the site is created

Therefore, the Terraform resources must be executed in this order:

In my set up, the netlify_site resource was running before the gitlab_deploy_key, so Netlify was assuming the repository is public. I fixed this by using a depends_on clause:

resource "netlify_site" "docs_ui" {
  name = "docs_ui-1234567890"
  repo {
    provider = "gitlab"
    deploy_key_id = "${netlify_deploy_key.docs_ui.id}"
    repo_path = "${substr(gitlab_project.docs_ui.web_url, length("https://gitlab.com/"), -1)}"
    repo_branch = "master"
  }
  depends_on = ["gitlab_deploy_key.docs_ui"]
}

Once I made that change, the Netlify site was created correctly.

I'd still be interested in seeing a GitLab example make its way to the README. Let me know if you'd like me to send a PR.

mojavelinux commented 5 years ago

Here's the upstream issue in Netlify: https://github.com/netlify/open-api/issues/143