2021-12-13 03:51:31 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/go-openapi/runtime"
|
|
|
|
"github.com/go-openapi/strfmt"
|
|
|
|
"github.com/gosimple/slug"
|
2021-12-14 04:18:51 +00:00
|
|
|
"github.com/netlify/open-api/v2/go/models"
|
|
|
|
"github.com/netlify/open-api/v2/go/porcelain"
|
|
|
|
ncontext "github.com/netlify/open-api/v2/go/porcelain/context"
|
2021-12-13 03:51:31 +00:00
|
|
|
)
|
|
|
|
|
2021-12-14 04:18:51 +00:00
|
|
|
func netlifyDeploy(netlifyAuthToken, netlifySiteID, buildDir string, prIndex int, prod bool) (*models.Deploy, error) {
|
2021-12-13 03:51:31 +00:00
|
|
|
ctx := ncontext.WithAuthInfo(context.Background(), runtime.ClientAuthInfoWriterFunc(func(req runtime.ClientRequest, reg strfmt.Registry) error {
|
|
|
|
return req.SetHeaderParam("Authorization", fmt.Sprintf("Bearer %s", netlifyAuthToken))
|
|
|
|
}))
|
|
|
|
netlifyClient := porcelain.Default
|
|
|
|
alias := fmt.Sprintf("%d-%s", prIndex, slug.Make(os.Getenv("DRONE_SOURCE_BRANCH")))
|
2021-12-14 04:18:51 +00:00
|
|
|
if prod {
|
|
|
|
alias = ""
|
|
|
|
}
|
2021-12-13 03:51:31 +00:00
|
|
|
return netlifyClient.DeploySite(ctx, porcelain.DeployOptions{
|
|
|
|
SiteID: netlifySiteID,
|
|
|
|
Dir: buildDir,
|
2021-12-14 04:18:51 +00:00
|
|
|
IsDraft: !prod,
|
2021-12-13 03:51:31 +00:00
|
|
|
Branch: alias,
|
|
|
|
})
|
|
|
|
}
|