194 lines
5.0 KiB
Go
194 lines
5.0 KiB
Go
// Package main implements the IntegraCI static-deploy Dagger module.
|
|
//
|
|
// One-stop deployment for static / SPA / Jamstack sites:
|
|
// - Vercel (vercel deploy)
|
|
// - Netlify (netlify-cli deploy)
|
|
// - AWS S3 + CloudFront invalidation
|
|
// - Cloudflare Pages (wrangler)
|
|
//
|
|
// Each provider returns a normalised StaticResult so the IntegraCI
|
|
// StaticDeployWorkflow + Backstage UI render uniform output.
|
|
package main
|
|
|
|
import (
|
|
"context"
|
|
"dagger/static-deploy/internal/dagger"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
type StaticDeploy struct{}
|
|
|
|
type StaticResult struct {
|
|
Provider string `json:"provider"`
|
|
Site string `json:"site"`
|
|
URL string `json:"url,omitempty"`
|
|
Output string `json:"output,omitempty"`
|
|
Success bool `json:"success"`
|
|
}
|
|
|
|
// Vercel deploys the built `dist` (or whatever output dir) to Vercel.
|
|
func (m *StaticDeploy) Vercel(
|
|
ctx context.Context,
|
|
source *dagger.Directory,
|
|
siteName string,
|
|
vercelToken *dagger.Secret,
|
|
// +optional
|
|
// +default="."
|
|
dir string,
|
|
// +optional
|
|
// +default=true
|
|
prod bool,
|
|
) (*StaticResult, error) {
|
|
prodFlag := ""
|
|
if prod {
|
|
prodFlag = "--prod"
|
|
}
|
|
c := dag.Container().
|
|
From("node:20-slim").
|
|
WithMountedDirectory("/src", source).
|
|
WithSecretVariable("VERCEL_TOKEN", vercelToken).
|
|
WithExec([]string{"sh", "-c", "npm install -g vercel@latest >/dev/null 2>&1"}).
|
|
WithExec([]string{"sh", "-c", fmt.Sprintf(
|
|
"cd /src/%s && vercel deploy %s --token=$VERCEL_TOKEN --yes --name %s || true",
|
|
dir, prodFlag, siteName,
|
|
)})
|
|
out, _ := c.Stdout(ctx)
|
|
url := ""
|
|
for _, line := range strings.Split(out, "\n") {
|
|
if strings.HasPrefix(strings.TrimSpace(line), "https://") {
|
|
url = strings.TrimSpace(line)
|
|
break
|
|
}
|
|
}
|
|
return &StaticResult{
|
|
Provider: "vercel",
|
|
Site: siteName,
|
|
URL: url,
|
|
Output: out,
|
|
Success: url != "",
|
|
}, nil
|
|
}
|
|
|
|
// Netlify deploys via netlify-cli.
|
|
func (m *StaticDeploy) Netlify(
|
|
ctx context.Context,
|
|
source *dagger.Directory,
|
|
siteID string,
|
|
netlifyToken *dagger.Secret,
|
|
// +optional
|
|
// +default="dist"
|
|
dir string,
|
|
// +optional
|
|
// +default=true
|
|
prod bool,
|
|
) (*StaticResult, error) {
|
|
prodFlag := ""
|
|
if prod {
|
|
prodFlag = "--prod"
|
|
}
|
|
c := dag.Container().
|
|
From("node:20-slim").
|
|
WithMountedDirectory("/src", source).
|
|
WithSecretVariable("NETLIFY_AUTH_TOKEN", netlifyToken).
|
|
WithExec([]string{"sh", "-c", "npm install -g netlify-cli@latest >/dev/null 2>&1"}).
|
|
WithExec([]string{"sh", "-c", fmt.Sprintf(
|
|
"cd /src && netlify deploy %s --dir=%s --site=%s || true",
|
|
prodFlag, dir, siteID,
|
|
)})
|
|
out, _ := c.Stdout(ctx)
|
|
url := ""
|
|
if i := strings.Index(out, "Website URL:"); i >= 0 {
|
|
rest := out[i+len("Website URL:"):]
|
|
end := strings.IndexAny(rest, "\n ")
|
|
if end > 0 {
|
|
url = strings.TrimSpace(rest[:end])
|
|
}
|
|
}
|
|
return &StaticResult{
|
|
Provider: "netlify",
|
|
Site: siteID,
|
|
URL: url,
|
|
Output: out,
|
|
Success: url != "" || strings.Contains(out, "Deploy is live!"),
|
|
}, nil
|
|
}
|
|
|
|
// S3 syncs a directory to an S3 bucket and invalidates a CloudFront dist.
|
|
func (m *StaticDeploy) S3(
|
|
ctx context.Context,
|
|
source *dagger.Directory,
|
|
bucket string,
|
|
region string,
|
|
awsAccessKey *dagger.Secret,
|
|
awsSecretKey *dagger.Secret,
|
|
// +optional
|
|
cloudfrontDistID string,
|
|
// +optional
|
|
// +default="dist"
|
|
dir string,
|
|
) (*StaticResult, error) {
|
|
c := dag.Container().
|
|
From("amazon/aws-cli:2.17.0").
|
|
WithMountedDirectory("/src", source).
|
|
WithSecretVariable("AWS_ACCESS_KEY_ID", awsAccessKey).
|
|
WithSecretVariable("AWS_SECRET_ACCESS_KEY", awsSecretKey).
|
|
WithEnvVariable("AWS_REGION", region).
|
|
WithExec([]string{"sh", "-c", fmt.Sprintf(
|
|
"aws s3 sync /src/%s s3://%s --delete || true", dir, bucket,
|
|
)})
|
|
if cloudfrontDistID != "" {
|
|
c = c.WithExec([]string{"sh", "-c", fmt.Sprintf(
|
|
"aws cloudfront create-invalidation --distribution-id %s --paths '/*' || true",
|
|
cloudfrontDistID,
|
|
)})
|
|
}
|
|
out, _ := c.Stdout(ctx)
|
|
url := fmt.Sprintf("https://%s.s3-website-%s.amazonaws.com", bucket, region)
|
|
return &StaticResult{
|
|
Provider: "s3",
|
|
Site: bucket,
|
|
URL: url,
|
|
Output: out,
|
|
Success: !strings.Contains(strings.ToLower(out), "fatal"),
|
|
}, nil
|
|
}
|
|
|
|
// CloudflarePages deploys via wrangler.
|
|
func (m *StaticDeploy) CloudflarePages(
|
|
ctx context.Context,
|
|
source *dagger.Directory,
|
|
projectName string,
|
|
apiToken *dagger.Secret,
|
|
accountID *dagger.Secret,
|
|
// +optional
|
|
// +default="dist"
|
|
dir string,
|
|
) (*StaticResult, error) {
|
|
c := dag.Container().
|
|
From("node:20-slim").
|
|
WithMountedDirectory("/src", source).
|
|
WithSecretVariable("CLOUDFLARE_API_TOKEN", apiToken).
|
|
WithSecretVariable("CLOUDFLARE_ACCOUNT_ID", accountID).
|
|
WithExec([]string{"sh", "-c", "npm install -g wrangler@latest >/dev/null 2>&1"}).
|
|
WithExec([]string{"sh", "-c", fmt.Sprintf(
|
|
"cd /src && wrangler pages deploy %s --project-name=%s || true",
|
|
dir, projectName,
|
|
)})
|
|
out, _ := c.Stdout(ctx)
|
|
url := ""
|
|
for _, l := range strings.Split(out, "\n") {
|
|
if i := strings.Index(l, "https://"); i >= 0 {
|
|
url = strings.TrimSpace(l[i:])
|
|
break
|
|
}
|
|
}
|
|
return &StaticResult{
|
|
Provider: "cloudflare_pages",
|
|
Site: projectName,
|
|
URL: url,
|
|
Output: out,
|
|
Success: url != "",
|
|
}, nil
|
|
}
|