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

GitSource: added git source deployment #21

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

Closed
wants to merge 1 commit into from

Conversation

WCMinor
Copy link
Contributor

@WCMinor WCMinor commented Apr 22, 2022

Followed the API documentation https://vercel.com/docs/rest-api#endpoints/deployments/create-a-new-deployment

This PR makes able the deployment from Git Source avoiding the need of upload the files to Vercel.

It works passing a new parameter called git_source, it is a map and it looks like this:

resource "vercel_deployment" "deployment" {
  team_id = var.vercel_team_id
  project_id = var.vercel_project_id
  git_source = {
    ref = var.git_branch
    repo_id = var.fe_github_repo_id
    type = "github"
  }
}

Here is an example of output, it just works:

vercel_deployment.deployment: Creating...
vercel_deployment.deployment: Still creating... [10s elapsed]
vercel_deployment.deployment: Still creating... [20s elapsed]
vercel_deployment.deployment: Still creating... [30s elapsed]
vercel_deployment.deployment: Still creating... [40s elapsed]
vercel_deployment.deployment: Still creating... [50s elapsed]
vercel_deployment.deployment: Still creating... [1m0s elapsed]
vercel_deployment.deployment: Creation complete after 1m6s [id=dpl_EQBQgeWeo5PXesr1MLSjrQC9MRrZ]

Apply complete! Resources: 1 added, 0 changed, 0 destroyed.

@WCMinor WCMinor requested a review from dglsparsons as a code owner April 22, 2022 16:04
@WCMinor WCMinor marked this pull request as draft April 25, 2022 12:55
@WCMinor WCMinor changed the title WIP: GitSource: added git source deployment GitSource: added git source deployment Apr 25, 2022
@WCMinor WCMinor marked this pull request as ready for review April 25, 2022 14:38
@WCMinor
Copy link
Contributor Author

WCMinor commented Apr 25, 2022

After few tests this just worked well all the times, we're using our fork in prod, so please feel free to merge if you want/like or to propose changes. Thanks for your job guys!

@WCMinor WCMinor force-pushed the git_source_deployment branch 2 times, most recently from 99d24b0 to 167d4c4 Compare April 28, 2022 15:01
@WCMinor
Copy link
Contributor Author

WCMinor commented Apr 28, 2022

Could you please review and merge if you think it is ok?

@WCMinor WCMinor force-pushed the git_source_deployment branch from 167d4c4 to 19cce9f Compare April 28, 2022 15:22
Comment on lines 71 to 72
- **files** (Map of String) A map of files to be uploaded for the deployment. This should be provided by a `vercel_project_directory` or `vercel_file` data source.
- **git_source** (Attributes) A map with the Git repo information (see [below for nested schema](#nestedatt--git_source))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should probably validate that one of files or git_source is present.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

on it

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment on lines 89 to 91
- **ref** (String) Branch or commit hash
- **repo_id** (String) Frontend git repo ID
- **type** (String) Type of git repo, supported values are: github
Copy link
Collaborator

@dglsparsons dglsparsons May 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How are these all optional? Aren't they required if specifying a git_source?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I set them as required, let me check why they appear as optional in the docs

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was investigating a bit and could not find why this happens. tfplugindocs just renders them as optional even when they are set as required. if happens the same here

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ahh interesting. I guess it's because the parent property is optional then.

Comment on lines 59 to 65
var gs client.GitSource
gs.Ref = g.Ref.Value
gs.RepoId = g.RepoId.Value
gs.Type = g.Type.Value
return gs
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not:

return client.GitSource{
   Ref: g.Ref.Value,
   RepoId: g.RepoId.Value, 
   Type: g.Type.Value,
}

Comment on lines +23 to +29
// GitSource represents the Git integration source for a deployment.
type GitSource struct {
RepoId types.String `tfsdk:"repo_id"`
Ref types.String `tfsdk:"ref"`
Type types.String `tfsdk:"type"`
}

Copy link
Collaborator

@dglsparsons dglsparsons May 3, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'd also want to add acceptance tests specifically around creating a resource_deployment with git_source set.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

working on these

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

@WCMinor WCMinor force-pushed the git_source_deployment branch 5 times, most recently from 0510334 to 1315b35 Compare May 4, 2022 14:36
@WCMinor WCMinor requested a review from dglsparsons May 4, 2022 14:36
@WCMinor
Copy link
Contributor Author

WCMinor commented May 4, 2022

I think the tests are not passing because the PR is coming from an external repo and that mess the GH secrets maybe? the tests work locally on my end.

VERCEL_API_TOKEN must be set for acceptance tests

@WCMinor WCMinor force-pushed the git_source_deployment branch 2 times, most recently from 934df13 to d984051 Compare May 4, 2022 14:39
Comment on lines +212 to +213
// checkMutualyExclusiveAttributes checks whether git_source and files are not set at the same time
func (d *Deployment) checkMutualyExclusiveAttributes() error {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can do this as a validation instead, but I'll merge the changes and fix this up retrospectively.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that makes sense

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

just to clarify, are you merging this or you require more changes?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, i wasn't very clear. I meant i'd take a look at fixing that (either post merge, or by checking out your branch). I've only had a surface level skim so far :)

@@ -20,6 +20,11 @@ type DeploymentFile struct {
Sha string `json:"sha"`
Size int `json:"size"`
}
type GitSource struct {
RepoId string `json:"repoId"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Digging into this, where does the repoId come from?
I can see in the example that you have it as a number. 🤔. I wonder if we need some data source to expose this so it's more usable?

Copy link
Collaborator

@dglsparsons dglsparsons May 5, 2022

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, so repoId looks like it is exposed through a project. I think it could be exposed from both the vercel_project resource and the vercel_project data source.
This would mean you can use it like:

data "vercel_project" "my_project" {
   name = "my-project"
}

resource "vercel_deployment" "example" {
  team_id    = var.vercel_team_id
  project_id = data.vercel_project.my_project.id
  git_source = {
    ref = "main" // or any other branch you fancy. 
    repo_id = data.vercel_project.my_project.git_repository.repo_id
    type = data.vercel_project.my_project.git_repository.type
  }
}

I think this would be more preferable than having to dig out a repoId. What do you think @WCMinor?

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another issue I can see here is that Terraform would not know when the vercel_deployment resource needs re-creating. Short of having something like the ref set to a specific commit ID. So this would only act as a one-off deployment.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's true that you can find it out that way, but I think it's somewhat surprising to see (and results in a magic number). I think if we had the data source, or exposed it via a project it would be more intuitive on how to use the two together to trigger a deployment.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Out of curiosity have you considered putting them in a single repository, behind something like https://turborepo.org/ ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we're actually moving on from a monorepo to a distributed system to allow each team to manage their own code as the teams get bigger

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense! As an update on this issue - I've been having a play around, and I'm coming to the belief that the only thing needed is a ref field on the vercel_deployment resource.

For example

resource "vercel_project" "example" {
   name = "my-test-project" 
   git_repository = {
       type = "github" 
       repo = "acme/example"
   }
}

# Deploy the main branch of acme/example. 
resource "vercel_deployment" "example" {
    project_id = vercel_project.example.id
    ref        = "main" # or a commit ID. 
}

The deployment resource can, itself, look up the relevant repoId / equivalent bitbucket/gitlab parameters, and pass these onto the deployment creation.

You could even use it with a data-source to pull a project by a name:

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

resource "vercel_deployment" "example" {
    project_id = data.vercel_project.example.id
    ref        = "main" 
}

We already have a hard requirement that for a deployment to be created, a project must already exist. I think using the git-connection this way would work nicely, and also prevents any confusing fields being presented back out of the provider (like repoId or projectUuid or workspaceUuid). What do you think @WCMinor? Would this work for your use-case?

With my playing around I think i'm fairly close to a working version, so if this works for you, I should be able to have a PR up soonish.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks great yeah, much easier to understand and to configure.

Funny enough, if you pass a public github repo (not configured in your project) to the deploy API , it works, it deploys a project based on such public repo. That is the only case in which it would make sense to use a standalone git_source parameter. Honestly, I think that functionality is kinda out of the scope of this provider as it is not really documented by Vercel.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. It's probably more a result of accidental API design than anything else 😓

ReadyState string `json:"readyState"`
Target *string `json:"target"`
URL string `json:"url"`
GitSource GitSource `json:"git_source"`
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I suspect the snake case here isn't correct.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mmm, as per their recommendations it looks fine:

Attribute names within Terraform configuration blocks are conventionally named as all-lowercase with underscores separating words, as shown above.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

feel free to change it if you think it is more consistent

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is the json response from the Vercel API, so not to do with the terraform configuration :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right, I just tested it

    },
    "gitSource": {
        "repoId": 449314886,
        "ref": "dario-terraform-deployment-test",
        "type": "github",
        "sha": "f4f09e995618dea3cd77e39a2cb710628ae03228"

changed

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hooray. We should probably set this back onto the terraform state, rather than using the value from plan in that case (so it's readable). However... I think we probably need to fix the repoId issue with different git providers first.

@WCMinor WCMinor force-pushed the git_source_deployment branch from d984051 to 5559450 Compare May 5, 2022 16:22
@dglsparsons
Copy link
Collaborator

Closing this in favour of #29

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants