chore: publish IntegraCI build modules 732ec6c83925
This commit is contained in:
1 parent
7a84930dea
commit
5f8a24d014
133 files changed
+18990
-2
No files matched your search
@@ -0,0 +1,160 @@
|
||||
// Package main implements the IntegraCI container-paas-deploy Dagger module.
|
||||
//
|
||||
// Targets:
|
||||
// - Google Cloud Run
|
||||
// - AWS ECS Fargate (via aws ecs update-service)
|
||||
// - AWS App Runner
|
||||
//
|
||||
// Use this when a service is already containerised but the deployment target
|
||||
// is *not* Kubernetes. ArgoCD remains the K8s path; this module is the
|
||||
// non-K8s container PaaS path.
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"dagger/container-paas-deploy/internal/dagger"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type ContainerPaasDeploy struct{}
|
||||
|
||||
type PaasResult struct {
|
||||
Provider string `json:"provider"`
|
||||
ServiceName string `json:"service_name"`
|
||||
Region string `json:"region"`
|
||||
Revision string `json:"revision,omitempty"`
|
||||
URL string `json:"url,omitempty"`
|
||||
Output string `json:"output,omitempty"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
// CloudRun deploys a container image to Google Cloud Run.
|
||||
func (m *ContainerPaasDeploy) CloudRun(
|
||||
ctx context.Context,
|
||||
serviceName string,
|
||||
imageURI string,
|
||||
region string,
|
||||
serviceAccount *dagger.Secret,
|
||||
// +optional
|
||||
// +default="1"
|
||||
cpu string,
|
||||
// +optional
|
||||
// +default="512Mi"
|
||||
memory string,
|
||||
// +optional
|
||||
// +default=0
|
||||
minInstances int,
|
||||
// +optional
|
||||
// +default=10
|
||||
maxInstances int,
|
||||
// +optional
|
||||
envCSV string,
|
||||
) (*PaasResult, error) {
|
||||
envFlag := ""
|
||||
if envCSV != "" {
|
||||
envFlag = "--set-env-vars=" + envCSV
|
||||
}
|
||||
c := dag.Container().
|
||||
From("google/cloud-sdk:slim").
|
||||
WithMountedSecret("/work/sa.json", serviceAccount).
|
||||
WithExec([]string{"sh", "-c", "gcloud auth activate-service-account --key-file=/work/sa.json"}).
|
||||
WithExec([]string{"sh", "-c", strings.Join([]string{
|
||||
"gcloud run deploy " + serviceName,
|
||||
"--image=" + imageURI,
|
||||
"--region=" + region,
|
||||
"--platform=managed",
|
||||
"--allow-unauthenticated",
|
||||
"--cpu=" + cpu,
|
||||
"--memory=" + memory,
|
||||
fmt.Sprintf("--min-instances=%d --max-instances=%d", minInstances, maxInstances),
|
||||
envFlag,
|
||||
"|| true",
|
||||
}, " ")})
|
||||
out, _ := c.Stdout(ctx)
|
||||
url := ""
|
||||
if i := strings.Index(out, "Service URL: "); i >= 0 {
|
||||
rest := out[i+len("Service URL: "):]
|
||||
end := strings.IndexAny(rest, "\n ")
|
||||
if end > 0 {
|
||||
url = rest[:end]
|
||||
} else {
|
||||
url = rest
|
||||
}
|
||||
}
|
||||
return &PaasResult{
|
||||
Provider: "cloud_run",
|
||||
ServiceName: serviceName,
|
||||
Region: region,
|
||||
URL: strings.TrimSpace(url),
|
||||
Output: out,
|
||||
Success: url != "",
|
||||
}, nil
|
||||
}
|
||||
|
||||
// ECSFargate triggers a rolling update on an existing ECS service.
|
||||
//
|
||||
// Assumes the task definition is registered separately (best practice is to
|
||||
// register it via Crossplane / Terraform). This module only swings the
|
||||
// service to the new image revision.
|
||||
func (m *ContainerPaasDeploy) ECSFargate(
|
||||
ctx context.Context,
|
||||
cluster string,
|
||||
serviceName string,
|
||||
taskDefinition string,
|
||||
region string,
|
||||
awsAccessKey *dagger.Secret,
|
||||
awsSecretKey *dagger.Secret,
|
||||
) (*PaasResult, error) {
|
||||
c := dag.Container().
|
||||
From("amazon/aws-cli:2.17.0").
|
||||
WithSecretVariable("AWS_ACCESS_KEY_ID", awsAccessKey).
|
||||
WithSecretVariable("AWS_SECRET_ACCESS_KEY", awsSecretKey).
|
||||
WithEnvVariable("AWS_REGION", region).
|
||||
WithExec([]string{"sh", "-c", strings.Join([]string{
|
||||
"aws ecs update-service",
|
||||
"--cluster " + cluster,
|
||||
"--service " + serviceName,
|
||||
"--task-definition " + taskDefinition,
|
||||
"--force-new-deployment",
|
||||
"|| true",
|
||||
}, " ")})
|
||||
out, _ := c.Stdout(ctx)
|
||||
return &PaasResult{
|
||||
Provider: "ecs_fargate",
|
||||
ServiceName: serviceName,
|
||||
Region: region,
|
||||
Output: out,
|
||||
Success: strings.Contains(out, `"serviceArn"`),
|
||||
}, nil
|
||||
}
|
||||
|
||||
// AppRunner creates / updates an App Runner service from an image.
|
||||
func (m *ContainerPaasDeploy) AppRunner(
|
||||
ctx context.Context,
|
||||
serviceName string,
|
||||
imageURI string,
|
||||
region string,
|
||||
awsAccessKey *dagger.Secret,
|
||||
awsSecretKey *dagger.Secret,
|
||||
) (*PaasResult, error) {
|
||||
c := dag.Container().
|
||||
From("amazon/aws-cli:2.17.0").
|
||||
WithSecretVariable("AWS_ACCESS_KEY_ID", awsAccessKey).
|
||||
WithSecretVariable("AWS_SECRET_ACCESS_KEY", awsSecretKey).
|
||||
WithEnvVariable("AWS_REGION", region).
|
||||
WithExec([]string{"sh", "-c", strings.Join([]string{
|
||||
"aws apprunner create-service --service-name " + serviceName,
|
||||
"--source-configuration ImageRepository={ImageIdentifier=" + imageURI + ",ImageRepositoryType=ECR}",
|
||||
"|| aws apprunner start-deployment --service-arn $(aws apprunner list-services --query \"ServiceSummaryList[?ServiceName=='" + serviceName + "'].ServiceArn | [0]\" --output text)",
|
||||
"|| true",
|
||||
}, " ")})
|
||||
out, _ := c.Stdout(ctx)
|
||||
return &PaasResult{
|
||||
Provider: "app_runner",
|
||||
ServiceName: serviceName,
|
||||
Region: region,
|
||||
Output: out,
|
||||
Success: strings.Contains(out, `"ServiceArn"`),
|
||||
}, nil
|
||||
}
|
||||
Reference in new issue
Block a user