Skip to content

Commit e9340c1

Browse files
authored
Add support for prebuilt deployments (#67)
* Add a data source for prebuilt projects * Flesh out tests and docs for Prebuilt deployments * Some stylistic improvements
1 parent 7d5b6e9 commit e9340c1

26 files changed

+585
-24
lines changed

Diff for: client/dns_record_create.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ type CreateDNSRecordRequest struct {
2626
Value string `json:"value,omitempty"`
2727
}
2828

29-
// CreateProjectDomain creates a DNS record for a specified domain name within Vercel.
29+
// CreateDNSRecord creates a DNS record for a specified domain name within Vercel.
3030
func (c *Client) CreateDNSRecord(ctx context.Context, teamID string, request CreateDNSRecordRequest) (r DNSRecord, err error) {
3131
url := fmt.Sprintf("%s/v4/domains/%s/records", c.baseURL, request.Domain)
3232
if teamID != "" {

Diff for: client/dns_record_update.go

+3
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,15 @@ import (
99
"github.com/hashicorp/terraform-plugin-log/tflog"
1010
)
1111

12+
// SRVUpdate defines the updatable fields within an SRV block of a DNS record.
1213
type SRVUpdate struct {
1314
Port *int64 `json:"port"`
1415
Priority *int64 `json:"priority"`
1516
Target *string `json:"target"`
1617
Weight *int64 `json:"weight"`
1718
}
1819

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

29+
// UpdateDNSRecord updates a DNS record for a specified domain name within Vercel.
2730
func (c *Client) UpdateDNSRecord(ctx context.Context, teamID, recordID string, request UpdateDNSRecordRequest) (r DNSRecord, err error) {
2831
url := fmt.Sprintf("%s/v4/domains/records/%s", c.baseURL, recordID)
2932
if teamID != "" {

Diff for: client/environment_variables_get.go

+1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ func (c *Client) getEnvironmentVariables(ctx context.Context, projectID, teamID
3333
return envResponse.Env, err
3434
}
3535

36+
// GetEnvironmentVariable gets a singluar environment variable from Vercel based on its ID.
3637
func (c *Client) GetEnvironmentVariable(ctx context.Context, projectID, teamID, envID string) (e EnvironmentVariable, err error) {
3738
url := fmt.Sprintf("%s/v1/projects/%s/env/%s", c.baseURL, projectID, envID)
3839
if teamID != "" {

Diff for: client/file_create.go

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"github.com/hashicorp/terraform-plugin-log/tflog"
1010
)
1111

12+
// CreateFileRequest defines the information needed to upload a file to Vercel.
1213
type CreateFileRequest struct {
1314
Filename string
1415
SHA string

Diff for: docs/data-sources/prebuilt_project.md

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
---
2+
# generated by https://github.com/hashicorp/terraform-plugin-docs
3+
page_title: "vercel_prebuilt_project Data Source - terraform-provider-vercel"
4+
subcategory: ""
5+
description: |-
6+
Provides the output of a project built via vercel build and provides metadata for use with a vercel_deployment
7+
The build command https://vercel.com/docs/cli#commands/build can be used to build a project locally or in your own CI environment.
8+
Build artifacts are placed into the .vercel/output directory according to the Build Output API https://vercel.com/docs/build-output-api/v3.
9+
This allows a Vercel Deployment to be created without sharing the Project's source code with Vercel.
10+
---
11+
12+
# vercel_prebuilt_project (Data Source)
13+
14+
Provides the output of a project built via `vercel build` and provides metadata for use with a `vercel_deployment`
15+
16+
The [build command](https://vercel.com/docs/cli#commands/build) can be used to build a project locally or in your own CI environment.
17+
Build artifacts are placed into the `.vercel/output` directory according to the [Build Output API](https://vercel.com/docs/build-output-api/v3).
18+
19+
This allows a Vercel Deployment to be created without sharing the Project's source code with Vercel.
20+
21+
## Example Usage
22+
23+
```terraform
24+
# In this example, we are assuming that a nextjs UI exists in a `ui` directory
25+
# and has been prebuilt via `vercel build`.
26+
# We assume any terraform code exists in a separate `terraform` directory.
27+
# E.g.
28+
# ```
29+
# ui/
30+
# .vercel/
31+
# output/
32+
# ...
33+
# src/
34+
# index.js
35+
# package.json
36+
# ...
37+
# terraform/
38+
# main.tf
39+
# ...
40+
# ```
41+
42+
data "vercel_project" "example" {
43+
name = "my-awesome-project"
44+
}
45+
46+
data "vercel_prebuilt_project" "example" {
47+
path = "../ui"
48+
}
49+
50+
resource "vercel_deployment" "example" {
51+
project_id = data.vercel_project.example.id
52+
files = data.vercel_prebuilt_project.example.output
53+
path_prefix = data.vercel_prebuilt_project.example.path
54+
}
55+
```
56+
57+
<!-- schema generated by tfplugindocs -->
58+
## Schema
59+
60+
### Required
61+
62+
- `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.
63+
64+
### Read-Only
65+
66+
- `id` (String) The ID of this resource.
67+
- `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.
68+
69+

Diff for: docs/resources/deployment.md

+15
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,21 @@ resource "vercel_deployment" "git_example" {
7878
project_id = vercel_project.git_example.id
7979
ref = "d92f10e" # or a git branch
8080
}
81+
82+
## Or deploying a prebuilt project
83+
data "vercel_project" "prebuilt_example" {
84+
name = "my-prebuilt-project"
85+
}
86+
87+
data "vercel_prebuilt_project" "prebuilt_example" {
88+
path = "../ui"
89+
}
90+
91+
resource "vercel_deployment" "prebuilt_example" {
92+
project_id = data.vercel_project.prebuilt_example.id
93+
files = data.vercel_prebuilt_project.prebuilt_example.output
94+
path_prefix = data.vercel_prebuilt_project.prebuilt_example.path
95+
}
8196
```
8297

8398
<!-- schema generated by tfplugindocs -->
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
# In this example, we are assuming that a nextjs UI exists in a `ui` directory
2+
# and has been prebuilt via `vercel build`.
3+
# We assume any terraform code exists in a separate `terraform` directory.
4+
# E.g.
5+
# ```
6+
# ui/
7+
# .vercel/
8+
# output/
9+
# ...
10+
# src/
11+
# index.js
12+
# package.json
13+
# ...
14+
# terraform/
15+
# main.tf
16+
# ...
17+
# ```
18+
19+
data "vercel_project" "example" {
20+
name = "my-awesome-project"
21+
}
22+
23+
data "vercel_prebuilt_project" "example" {
24+
path = "../ui"
25+
}
26+
27+
resource "vercel_deployment" "example" {
28+
project_id = data.vercel_project.example.id
29+
files = data.vercel_prebuilt_project.example.output
30+
path_prefix = data.vercel_prebuilt_project.example.path
31+
}

Diff for: examples/resources/vercel_deployment/resource.tf

+15
Original file line numberDiff line numberDiff line change
@@ -44,3 +44,18 @@ resource "vercel_deployment" "git_example" {
4444
project_id = vercel_project.git_example.id
4545
ref = "d92f10e" # or a git branch
4646
}
47+
48+
## Or deploying a prebuilt project
49+
data "vercel_project" "prebuilt_example" {
50+
name = "my-prebuilt-project"
51+
}
52+
53+
data "vercel_prebuilt_project" "prebuilt_example" {
54+
path = "../ui"
55+
}
56+
57+
resource "vercel_deployment" "prebuilt_example" {
58+
project_id = data.vercel_project.prebuilt_example.id
59+
files = data.vercel_prebuilt_project.prebuilt_example.output
60+
path_prefix = data.vercel_prebuilt_project.prebuilt_example.path
61+
}

Diff for: glob/glob.go renamed to file/glob.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package glob
1+
package file
22

33
import (
44
"fmt"

Diff for: glob/ignores.go renamed to file/ignores.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package glob
1+
package file
22

33
import (
44
"bufio"

Diff for: file/prebuilt.go

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package file
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"os"
7+
)
8+
9+
// Builds defines some of the information that can be contained within a builds.json file
10+
// as part of the Build API output.
11+
type Builds struct {
12+
Target string `json:"target"`
13+
Error *struct{} `json:"error"`
14+
Builds []struct {
15+
Error *struct{} `json:"error"`
16+
} `json:"builds"`
17+
}
18+
19+
// ReadBuildsJSON will read a builds.json file and return the parsed content as a Builds struct.
20+
func ReadBuildsJSON(path string) (builds Builds, err error) {
21+
content, err := os.ReadFile(path)
22+
if err != nil {
23+
return builds, err
24+
}
25+
26+
err = json.Unmarshal(content, &builds)
27+
if err != nil {
28+
return builds, fmt.Errorf("could not parse file %s: %w", path, err)
29+
}
30+
31+
return builds, err
32+
}

Diff for: vercel/data_source_file_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -47,9 +47,9 @@ func TestAcc_DataSourceFile(t *testing.T) {
4747
{
4848
Config: testAccFileConfig(),
4949
Check: resource.ComposeAggregateTestCheckFunc(
50-
resource.TestCheckResourceAttr("data.vercel_file.test", "path", "example/index.html"),
51-
resource.TestCheckResourceAttr("data.vercel_file.test", "id", "example/index.html"),
52-
testChecksum("data.vercel_file.test", "file.example/index.html", Checksums{
50+
resource.TestCheckResourceAttr("data.vercel_file.test", "path", "examples/one/index.html"),
51+
resource.TestCheckResourceAttr("data.vercel_file.test", "id", "examples/one/index.html"),
52+
testChecksum("data.vercel_file.test", "file.examples/one/index.html", Checksums{
5353
unix: "60~9d3fedcc87ac72f54e75d4be7e06d0a6f8497e68",
5454
windows: "65~c0b8b91602dc7a394354cd9a21460ce2070b9a13",
5555
}),
@@ -62,7 +62,7 @@ func TestAcc_DataSourceFile(t *testing.T) {
6262
func testAccFileConfig() string {
6363
return `
6464
data "vercel_file" "test" {
65-
path = "example/index.html"
65+
path = "examples/one/index.html"
6666
}
6767
`
6868
}

0 commit comments

Comments
 (0)