Skip to content

Commit

Permalink
fix(runtime): deploy right git ref
Browse files Browse the repository at this point in the history
Fix #96
  • Loading branch information
miton18 committed Dec 27, 2024
1 parent 5b0bee0 commit 15dc684
Show file tree
Hide file tree
Showing 10 changed files with 54 additions and 20 deletions.
2 changes: 1 addition & 1 deletion docs/resources/docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ Can be either app_xxx or postgres_yyy ID format

Optional:

- `commit` (String) Deploy application on the given commit/tag
- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master`
- `repository` (String)


Expand Down
2 changes: 1 addition & 1 deletion docs/resources/java_war.md
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Can be either app_xxx or postgres_yyy ID format

Optional:

- `commit` (String) Deploy application on the given commit/tag
- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master`
- `repository` (String)


Expand Down
2 changes: 1 addition & 1 deletion docs/resources/nodejs.md
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ Can be either app_xxx or postgres_yyy ID format

Optional:

- `commit` (String) Deploy application on the given commit/tag
- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master`
- `repository` (String)


Expand Down
2 changes: 1 addition & 1 deletion docs/resources/php.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ Can be either app_xxx or postgres_yyy ID format

Optional:

- `commit` (String) Deploy application on the given commit/tag
- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master`
- `repository` (String)


Expand Down
2 changes: 1 addition & 1 deletion docs/resources/python.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,7 @@ Can be either app_xxx or postgres_yyy ID format

Optional:

- `commit` (String) Deploy application on the given commit/tag
- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master`
- `repository` (String)


Expand Down
2 changes: 1 addition & 1 deletion docs/resources/scala.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Can be either app_xxx or postgres_yyy ID format

Optional:

- `commit` (String) Deploy application on the given commit/tag
- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master`
- `repository` (String)


Expand Down
2 changes: 1 addition & 1 deletion docs/resources/static.md
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ Can be either app_xxx or postgres_yyy ID format

Optional:

- `commit` (String) Deploy application on the given commit/tag
- `commit` (String) Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master`
- `repository` (String)


Expand Down
32 changes: 22 additions & 10 deletions pkg/application/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package application

import (
"context"
"fmt"
"os"
"strings"

Expand All @@ -10,6 +11,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/transport/http"
"github.com/go-git/go-git/v5/storage/memory"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-log/tflog"
"go.clever-cloud.dev/client"
)

Expand All @@ -21,8 +23,7 @@ func gitDeploy(ctx context.Context, d Deployment, cc *client.Client, cleverRemot
cloneOpts := &git.CloneOptions{
URL: d.Repository,
RemoteName: "origin",
//Depth: 1,
Progress: os.Stdout,
Progress: os.Stdout,
}

r, err := git.CloneContext(ctx, memory.NewStorage(), nil, cloneOpts)
Expand Down Expand Up @@ -50,15 +51,26 @@ func gitDeploy(ctx context.Context, d Deployment, cc *client.Client, cleverRemot
RemoteURL: cleverRemote,
Force: true,
Progress: os.Stdout,
// TODO: deploy right branch/tag/commit
/*RefSpecs: []config.RefSpec{
"master:master",
},*/
Auth: auth,
Atomic: true,
Auth: auth,
}
err = remote.Push(pushOptions)
if err != nil {
if d.Commit != nil {
// refs/heads/[BRANCH]
// [COMMIT]
ref := config.RefSpec(fmt.Sprintf("%s:refs/heads/master", *d.Commit))
if err := ref.Validate(); err != nil {
diags.AddError("failed to build ref spec to push", err.Error())
return diags
}

pushOptions.RefSpecs = []config.RefSpec{ref}
}

tflog.Debug(ctx, "pushing...", map[string]interface{}{
"options": fmt.Sprintf("%+v", pushOptions),
})

err = remote.PushContext(ctx, pushOptions)
if err != nil && err != git.NoErrAlreadyUpToDate {
diags.AddError("failed to push to clever remote", err.Error())
return diags
}
Expand Down
27 changes: 25 additions & 2 deletions pkg/attributes/blocks.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,12 @@
package attributes

import (
"context"
"strings"

"github.com/go-git/go-git/v5/plumbing"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/schema/validator"
"github.com/hashicorp/terraform-plugin-framework/types"
"go.clever-cloud.com/terraform-provider/pkg"
)
Expand Down Expand Up @@ -31,8 +36,26 @@ var blocks = map[string]schema.Block{
},
"commit": schema.StringAttribute{
Optional: true,
Description: "Support either '<branch>:<SHA>' or '<tag>'",
MarkdownDescription: "Deploy application on the given commit/tag",
Description: "The git reference you want to deploy",
MarkdownDescription: "Support multiple syntax like `refs/heads/[BRANCH]` or `[COMMIT]`, in most of the case, you can use `refs/heads/master`",
Validators: []validator.String{
pkg.NewValidator(
"if reference (not commit hash) is provided test it's syntax",
func(ctx context.Context, req validator.StringRequest, res *validator.StringResponse) {
if req.ConfigValue.IsNull() || !strings.Contains(req.ConfigValue.ValueString(), "/") {
return
}

ref := plumbing.ReferenceName(req.ConfigValue.ValueString())
if err := ref.Validate(); err != nil {
res.Diagnostics.AddAttributeError(
req.Path,
"invalid Git reference",
err.Error(),
)
}
}),
},
},
},
},
Expand Down
1 change: 0 additions & 1 deletion pkg/resources/nodejs/crud.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,6 @@ func (r *ResourceNodeJS) Create(ctx context.Context, req resource.CreateRequest,
createRes, diags := application.CreateApp(ctx, createReq)
resp.Diagnostics.Append(diags...)
if resp.Diagnostics.HasError() {
tflog.Error(ctx, "ERROR IS WELL HERE", map[string]interface{}{})
return
}

Expand Down

0 comments on commit 15dc684

Please sign in to comment.