Terraform provider for Netlify
Branch: master
Clone or download
mitchellh Merge pull request #11 from alvin-huang/provider-fix
correct description for provider auth
Latest commit 0f8578d Feb 3, 2019
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
netlify
scripts add changelog script Sep 13, 2018
vendor
website
.gitignore Initial commit, supports deploy keys Dec 7, 2017
.travis.yml provider: Require Go 1.11 in TravisCI and README Jan 8, 2019
CHANGELOG.md
LICENSE
Makefile
README.md basic usage examples Sep 7, 2018
main.go

README.md

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 git@github.com: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