这是indexloc提供的服务,不要输入任何密码
Skip to content

Add support for prebuilt deployments #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Sep 5, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion client/dns_record_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type CreateDNSRecordRequest struct {
Value string `json:"value,omitempty"`
}

// CreateProjectDomain creates a DNS record for a specified domain name within Vercel.
// CreateDNSRecord creates a DNS record for a specified domain name within Vercel.
func (c *Client) CreateDNSRecord(ctx context.Context, teamID string, request CreateDNSRecordRequest) (r DNSRecord, err error) {
url := fmt.Sprintf("%s/v4/domains/%s/records", c.baseURL, request.Domain)
if teamID != "" {
Expand Down
3 changes: 3 additions & 0 deletions client/dns_record_update.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,15 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// SRVUpdate defines the updatable fields within an SRV block of a DNS record.
type SRVUpdate struct {
Port *int64 `json:"port"`
Priority *int64 `json:"priority"`
Target *string `json:"target"`
Weight *int64 `json:"weight"`
}

// UpdateDNSRecordRequest defines the structure of the request body for updating a DNS record.
type UpdateDNSRecordRequest struct {
MXPriority *int64 `json:"mxPriority,omitempty"`
Name *string `json:"name,omitempty"`
Expand All @@ -24,6 +26,7 @@ type UpdateDNSRecordRequest struct {
Value *string `json:"value,omitempty"`
}

// UpdateDNSRecord updates a DNS record for a specified domain name within Vercel.
func (c *Client) UpdateDNSRecord(ctx context.Context, teamID, recordID string, request UpdateDNSRecordRequest) (r DNSRecord, err error) {
url := fmt.Sprintf("%s/v4/domains/records/%s", c.baseURL, recordID)
if teamID != "" {
Expand Down
1 change: 1 addition & 0 deletions client/environment_variables_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ func (c *Client) getEnvironmentVariables(ctx context.Context, projectID, teamID
return envResponse.Env, err
}

// GetEnvironmentVariable gets a singluar environment variable from Vercel based on its ID.
func (c *Client) GetEnvironmentVariable(ctx context.Context, projectID, teamID, envID string) (e EnvironmentVariable, err error) {
url := fmt.Sprintf("%s/v1/projects/%s/env/%s", c.baseURL, projectID, envID)
if teamID != "" {
Expand Down
1 change: 1 addition & 0 deletions client/file_create.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (
"github.com/hashicorp/terraform-plugin-log/tflog"
)

// CreateFileRequest defines the information needed to upload a file to Vercel.
type CreateFileRequest struct {
Filename string
SHA string
Expand Down
69 changes: 69 additions & 0 deletions docs/data-sources/prebuilt_project.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
---
# generated by https://github.com/hashicorp/terraform-plugin-docs
page_title: "vercel_prebuilt_project Data Source - terraform-provider-vercel"
subcategory: ""
description: |-
Provides the output of a project built via vercel build and provides metadata for use with a vercel_deployment
The build command https://vercel.com/docs/cli#commands/build can be used to build a project locally or in your own CI environment.
Build artifacts are placed into the .vercel/output directory according to the Build Output API https://vercel.com/docs/build-output-api/v3.
This allows a Vercel Deployment to be created without sharing the Project's source code with Vercel.
---

# vercel_prebuilt_project (Data Source)

Provides the output of a project built via `vercel build` and provides metadata for use with a `vercel_deployment`

The [build command](https://vercel.com/docs/cli#commands/build) can be used to build a project locally or in your own CI environment.
Build artifacts are placed into the `.vercel/output` directory according to the [Build Output API](https://vercel.com/docs/build-output-api/v3).

This allows a Vercel Deployment to be created without sharing the Project's source code with Vercel.

## Example Usage

```terraform
# In this example, we are assuming that a nextjs UI exists in a `ui` directory
# and has been prebuilt via `vercel build`.
# We assume any terraform code exists in a separate `terraform` directory.
# E.g.
# ```
# ui/
# .vercel/
# output/
# ...
# src/
# index.js
# package.json
# ...
# terraform/
# main.tf
# ...
# ```

data "vercel_project" "example" {
name = "my-awesome-project"
}

data "vercel_prebuilt_project" "example" {
path = "../ui"
}

resource "vercel_deployment" "example" {
project_id = data.vercel_project.example.id
files = data.vercel_prebuilt_project.example.output
path_prefix = data.vercel_prebuilt_project.example.path
}
```

<!-- schema generated by tfplugindocs -->
## Schema

### Required

- `path` (String) The path to the project. Note that this path is relative to the root of your terraform files. This should be the directory that contains the `.vercel/output` directory.

### Read-Only

- `id` (String) The ID of this resource.
- `output` (Map of String) A map of output file to metadata about the file. The metadata contains the file size and hash, and allows a deployment to be created if the file changes.


15 changes: 15 additions & 0 deletions docs/resources/deployment.md
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,21 @@ resource "vercel_deployment" "git_example" {
project_id = vercel_project.git_example.id
ref = "d92f10e" # or a git branch
}

## Or deploying a prebuilt project
data "vercel_project" "prebuilt_example" {
name = "my-prebuilt-project"
}

data "vercel_prebuilt_project" "prebuilt_example" {
path = "../ui"
}

resource "vercel_deployment" "prebuilt_example" {
project_id = data.vercel_project.prebuilt_example.id
files = data.vercel_prebuilt_project.prebuilt_example.output
path_prefix = data.vercel_prebuilt_project.prebuilt_example.path
}
```

<!-- schema generated by tfplugindocs -->
Expand Down
31 changes: 31 additions & 0 deletions examples/data-sources/vercel_prebuilt_project/data-source.tf
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# In this example, we are assuming that a nextjs UI exists in a `ui` directory
# and has been prebuilt via `vercel build`.
# We assume any terraform code exists in a separate `terraform` directory.
# E.g.
# ```
# ui/
# .vercel/
# output/
# ...
# src/
# index.js
# package.json
# ...
# terraform/
# main.tf
# ...
# ```

data "vercel_project" "example" {
name = "my-awesome-project"
}

data "vercel_prebuilt_project" "example" {
path = "../ui"
}

resource "vercel_deployment" "example" {
project_id = data.vercel_project.example.id
files = data.vercel_prebuilt_project.example.output
path_prefix = data.vercel_prebuilt_project.example.path
}
15 changes: 15 additions & 0 deletions examples/resources/vercel_deployment/resource.tf
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,18 @@ resource "vercel_deployment" "git_example" {
project_id = vercel_project.git_example.id
ref = "d92f10e" # or a git branch
}

## Or deploying a prebuilt project
data "vercel_project" "prebuilt_example" {
name = "my-prebuilt-project"
}

data "vercel_prebuilt_project" "prebuilt_example" {
path = "../ui"
}

resource "vercel_deployment" "prebuilt_example" {
project_id = data.vercel_project.prebuilt_example.id
files = data.vercel_prebuilt_project.prebuilt_example.output
path_prefix = data.vercel_prebuilt_project.prebuilt_example.path
}
2 changes: 1 addition & 1 deletion glob/glob.go → file/glob.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package glob
package file

import (
"fmt"
Expand Down
2 changes: 1 addition & 1 deletion glob/ignores.go → file/ignores.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package glob
package file

import (
"bufio"
Expand Down
32 changes: 32 additions & 0 deletions file/prebuilt.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package file

import (
"encoding/json"
"fmt"
"os"
)

// Builds defines some of the information that can be contained within a builds.json file
// as part of the Build API output.
type Builds struct {
Target string `json:"target"`
Error *struct{} `json:"error"`
Builds []struct {
Error *struct{} `json:"error"`
} `json:"builds"`
}

// ReadBuildsJSON will read a builds.json file and return the parsed content as a Builds struct.
func ReadBuildsJSON(path string) (builds Builds, err error) {
content, err := os.ReadFile(path)
if err != nil {
return builds, err
}

err = json.Unmarshal(content, &builds)
if err != nil {
return builds, fmt.Errorf("could not parse file %s: %w", path, err)
}

return builds, err
}
8 changes: 4 additions & 4 deletions vercel/data_source_file_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,9 @@ func TestAcc_DataSourceFile(t *testing.T) {
{
Config: testAccFileConfig(),
Check: resource.ComposeAggregateTestCheckFunc(
resource.TestCheckResourceAttr("data.vercel_file.test", "path", "example/index.html"),
resource.TestCheckResourceAttr("data.vercel_file.test", "id", "example/index.html"),
testChecksum("data.vercel_file.test", "file.example/index.html", Checksums{
resource.TestCheckResourceAttr("data.vercel_file.test", "path", "examples/one/index.html"),
resource.TestCheckResourceAttr("data.vercel_file.test", "id", "examples/one/index.html"),
testChecksum("data.vercel_file.test", "file.examples/one/index.html", Checksums{
unix: "60~9d3fedcc87ac72f54e75d4be7e06d0a6f8497e68",
windows: "65~c0b8b91602dc7a394354cd9a21460ce2070b9a13",
}),
Expand All @@ -62,7 +62,7 @@ func TestAcc_DataSourceFile(t *testing.T) {
func testAccFileConfig() string {
return `
data "vercel_file" "test" {
path = "example/index.html"
path = "examples/one/index.html"
}
`
}
Loading