Git Product home page Git Product logo

hashicorp / terraform-provider-netlify Goto Github PK

View Code? Open in Web Editor NEW
56.0 15.0 36.0 8 MB

Terraform Netlify provider. Please note: This Terraform provider is archived per our provider archiving process: https://terraform.io/docs/internals/archiving.html

Home Page: https://registry.terraform.io

License: Mozilla Public License 2.0

Makefile 5.08% Go 84.98% Shell 6.15% HTML 3.79%
terraform netlify terraform-provider

terraform-provider-netlify's Introduction

Please note: This Terraform provider is archived, per our provider archiving process. What does this mean?

  1. The code repository and all commit history will still be available.
  2. Existing released binaries will remain available on the releases site.
  3. Issues and pull requests are not being monitored.
  4. New releases will not be published.

If anyone from the community or an interested third party is willing to maintain it, they can fork the repository and publish it to the Terraform Registry. If you are interested in maintaining this provider, please reach out to the Terraform Provider Development Program at [email protected].


Terraform Provider

Requirements

  • Terraform 0.10.x
  • Go 1.8 (to build the provider plugin)

Building The Provider

Clone repository to: $GOPATH/src/github.com/terraform-providers/terraform-provider-netlify

$ mkdir -p $GOPATH/src/github.com/terraform-providers; cd $GOPATH/src/github.com/terraform-providers
$ git clone [email protected]:terraform-providers/terraform-provider-netlify.git

Enter the provider directory and build the provider

$ cd $GOPATH/src/github.com/terraform-providers/terraform-provider-netlify
$ make build
# or if you're on a mac:
$ gnumake build

Using the provider

NOTE: This provider is best used when paired with a VCS system provider such as the Github provider, which will be used for reference in these examples, since Netlify integrates directly with your VCS system in order to continuously deploy your website.

Using this provider requires an auth token from Netlify. You can generate a token here: https://app.netlify.com/account/applications. You will also likely need an auth token for your VCS system. In this example, we'll use Github, so you'll want to get a Github token as well. We'll start by configuring Github. In this example, we'll assume that we're using a repo at github.com/username/repo.

// configure the github provider
provider "github" {
  organization = "<username>"
}

// Configure the repository with the dynamically created Netlify key.
resource "github_repository_deploy_key" "key" {
  title      = "Netlify"
  repository = "<repo>"
  key        = "${netlify_deploy_key.key.public_key}"
  read_only  = false
}

// Create a webhook that triggers Netlify builds on push.
resource "github_repository_webhook" "main" {
  repository = "<repo>"
  name       = "web"
  events     = ["delete", "push", "pull_request"]

  configuration {
    content_type = "json"
    url          = "https://api.netlify.com/hooks/github"
  }

  depends_on = ["netlify_site.main"]
}

This pairs closely with the Netlify provider instructions as you can see, example shown below:

// A new, unique deploy key for this specific website
resource "netlify_deploy_key" "key" {}

resource "netlify_site" "main" {
  name = "<name of netlify site>"

  repo {
    repo_branch = "<github branch to deploy>"
    command = "<command used to build your website>"
    deploy_key_id = "${netlify_deploy_key.key.id}"
    dir = "<directory your website is built into, relative to root>"
    provider = "github"
    repo_path = "<username/repo>"
  }
}

With all the details filled in here, you should be able to run the script and have your site deploy. Of course, it's likely that you will want to configure some of these values as variables, and you can use GITHUB_TOKEN and NETLIFY_TOKEN environment variables as well to represent these API keys.

Developing the Provider

If you wish to work on the provider, you'll first need Go installed on your machine (version 1.8+ is required). You'll also need to correctly setup a GOPATH, as well as adding $GOPATH/bin to your $PATH.

To compile the provider, run make build. This will build the provider and put the provider binary in the $GOPATH/bin directory.

$ make build
...
$ $GOPATH/bin/terraform-provider-netlify
...

In order to test the provider, you can simply run make test.

$ make test

In order to run the full suite of Acceptance tests, run make testacc.

Note: Acceptance tests create real resources, and often cost money to run.

$ make testacc

terraform-provider-netlify's People

Contributors

aicarmic avatar alvin-huang avatar appilon avatar bflad avatar calavera avatar cgriggs01 avatar dvrkps avatar h13ronim avatar kmoe avatar mitchellh avatar paultyng avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

terraform-provider-netlify's Issues

Manual deploys

From what I can see, the docs don't make it clear how to set up a site for manual deploys (ie. no git repo). The repo block seems to be required.

If this is possible, can a note be added to the documentation on how to do it?

Provider fails to create a proper connection to a GitLab repository

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.

Support importing of sites

Importing of sites

I'm currently in the process of consolidating all of my online infrastructure into Terraform
and would thus need to import my existing site into state.

Would this require special knowledge or is it I can contribute with some basic Go abilities?

(*models.Error) is not supported by the TextConsumer, data field should be optional in webhooks

# Configure the Netlify Provider
provider "netlify" {
  token = "NETLIFY_TOKEN"
}

terraform {
  required_version = ">= 0.12.20"
}


// How do I know about events and Event Types?
// curl -sSfL -H "Authorization: Bearer ${NETLIFY_TOKEN}" https://api.netlify.com/api/v1/hooks/types | jq

resource "netlify_hook" "commit_status" {

  site_id = "SITE_ID"
  type    = "github_app_commit_status"
  event   = "github_app_checks"

  data = { }
}

The above config will fail with error

2020-02-23T15:19:28.196+0100 [DEBUG] plugin: using plugin: version=5
2020/02/23 15:19:28 [WARN] Provider "registry.terraform.io/-/netlify" produced an invalid plan for netlify_hook.commit_status, but we are tolerating it because it is using the legacy plugin SDK.
    The following problems may be the cause of any confusing errors from downstream operations:
      - .data: planned value cty.NullVal(cty.Map(cty.String)) does not match config value cty.MapVal(map[string]cty.Value{"message":cty.NullVal(cty.String)})
netlify_hook.commit_status: Creating...
2020/02/23 15:19:28 [DEBUG] netlify_hook.commit_status: applying the planned Create change
2020/02/23 15:19:28 [DEBUG] netlify_hook.commit_status: apply errored, but we're indicating that via the Error pointer rather than returning it: &{0 } (*models.Error) is not supported by the TextConsumer, can be resolved by supporting TextUnmarshaler interface
2020/02/23 15:19:28 [ERROR] <root>: eval: *terraform.EvalApplyPost, err: &{0 } (*models.Error) is not supported by the TextConsumer, can be resolved by supporting TextUnmarshaler interface
2020/02/23 15:19:28 [ERROR] <root>: eval: *terraform.EvalSequence, err: &{0 } (*models.Error) is not supported by the TextConsumer, can be resolved by supporting TextUnmarshaler interface

Error: &{0 } (*models.Error) is not supported by the TextConsumer, can be resolved by supporting TextUnmarshaler interface

  on main.tf line 5, in resource "netlify_hook" "commit_status":
   5: resource "netlify_hook" "commit_status" {


2020-02-23T15:19:28.530+0100 [DEBUG] plugin: plugin process exited: path=/home/prasad/Git/infra/netlify/test/.terraform/plugins/linux_amd64/terraform-provider-netlify_v0.4.0_x4 pid=8908
2020-02-23T15:19:28.530+0100 [DEBUG] plugin: plugin exited


Also data fields are optional in some cases, eg. for hook_type
github_app_checks there are no fields

  {
    "name": "github_app_checks",
    "fields": [],
    "events": [
      "deploy_building",
      "deploy_created",
      "deploy_failed"
    ]
  },

netlify_deploy_key.key: &{0 } (*models.Error) is not supported by the TextConsumer

Hi! I'm getting the following error when trying to apply the terraform configuration.
Here is the error stack:

Error: Error applying plan:

1 error(s) occurred:

* netlify_deploy_key.key: 1 error(s) occurred:

* netlify_deploy_key.key: &{0 } (*models.Error) is not supported by the TextConsumer, can be resolved by supporting TextUnmarshaler interface

Terraform does not automatically rollback in the face of errors.
Instead, your Terraform state file has been partially updated with
any resources that successfully completed. Please address the error
above and apply again to incrementally change your infrastructure.

config

variable token {}
variable base_url {}

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

provider "github" {
  organization = "user"
}

resource "netlify_deploy_key" "key" {}

resource "github_repository_deploy_key" "key" {
  title      = "Netlify"
  repository = "repo"

  key       = "${netlify_deploy_key.key.public_key}"
  read_only = false
}

resource "github_repository_webhook" "main" {
  repository = "gatsby-starter-default"
  name       = "web"
  events     = ["delete", "push", "pull_request"]

  configuration {
    content_type = "json"
    url          = "https://api.netlify.com/hooks/github"
  }

  depends_on = ["netlify_site.main"]
}

resource "netlify_site" "main" {
  name = "testing-terraform"

  repo {
    provider    = "github"
    repo_path   = "user/repo"
    repo_branch = "master"
    command     = "yarn build"
    dir         = "public"

    deploy_key_id = "${netlify_deploy_key.key.id}"
  }
}

`terraform apply` appears to trigger a deploy

This is listed as a failed deploy as I have not enabled any webhooks and I'm only using the cli tool to deploy. I think applying the config should not create a new deployment. Is this a limitation of the Netlify API that it does this?

10:54:08 PM: Build ready to start
10:54:14 PM: build-image version: 9e0f207a27642d0115b1ca97cd5e8cebbe492f63
10:54:14 PM: build-image tag: v3.3.2
10:54:14 PM: buildbot version: 75cd99f62ada9e21edea53208e8baf0eab85a045
10:54:14 PM: Fetching cached dependencies
10:54:14 PM: Failed to fetch cache, continuing with build
10:54:14 PM: Starting to prepare the repo for build
10:54:15 PM: git ref refs/heads/master does not exist or you do not have permission
10:54:15 PM: Failing build: Failed to prepare repo
10:54:15 PM: failed during stage 'preparing repo': git ref refs/heads/master does not exist

This is the log int he Netlify UI for the deploy

API Error After Enabling GitHub App

Hey,

WIll flesh this out when I get a bit more time but wanted to call out that there appears to be a change required in the provider as a result of a breaking change in the Netlify API.

I get this error in terraform plan & terraform apply after installing the Netlify GitHub App for a site made by terraform:

Error: json: cannot unmarshal number into Go struct field RepoInfo.installation_id of type string

Found these posts which provide goodcontext:

  1. netlify/open-api#162
  2. https://community.netlify.com/t/502-cannot-unmarshal-number-into-go-struct-field-apigatewayproxyresponse-multivalueheaders/16350

[PROPOSAL] Switch to Go Modules

As part of the preparation for Terraform v0.12, we would like to migrate all providers to use Go Modules. We plan to continue checking dependencies into vendor/ to remain compatible with existing tooling/CI for a period of time, however go modules will be used for management. Go Modules is the official solution for the go programming language, we understand some providers might not want this change yet, however we encourage providers to begin looking towards the switch as this is how we will be managing all Go projects in the future. Would maintainers please react with ๐Ÿ‘ for support, or ๐Ÿ‘Ž if you wish to have this provider omitted from the first wave of pull requests. If your provider is in support, we would ask that you avoid merging any pull requests that mutate the dependencies while the Go Modules PR is open (in fact a total codefreeze would be even more helpful), otherwise we will need to close that PR and re-run go mod init. Once merged, dependencies can be added or updated as follows:

$ GO111MODULE=on go get github.com/some/module@master
$ GO111MODULE=on go mod tidy
$ GO111MODULE=on go mod vendor

GO111MODULE=on might be unnecessary depending on your environment, this example will fetch a module @ master and record it in your project's go.mod and go.sum files. It's a good idea to tidy up afterward and then copy the dependencies into vendor/. To remove dependencies from your project, simply remove all usage from your codebase and run:

$ GO111MODULE=on go mody tidy
$ GO111MODULE=on go mod vendor

Thank you sincerely for all your time, contributions, and cooperation!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.