Last active
July 13, 2024 21:51
-
-
Save pjmagee/88472a36adb8a58ccdd45e55f9b54123 to your computer and use it in GitHub Desktop.
Example Dagger CICD
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import ( | |
"context" | |
) | |
type DaggerTerraformAwsLocalstack struct{} | |
func (m *DaggerTerraformAwsLocalstack) localStackService() *Service { | |
// https://www.youtube.com/watch?v=SYCeM-Q6nRs&pp=ygUQZGFnZ2VyIHRlcnJhZm9ybQ%3D%3D | |
// https://docs.dagger.io/manuals/developer/services#container-services | |
// https://github.com/localstack/localstack | |
return dag.Container(). | |
From("localstack/localstack:latest"). | |
WithEnvVariable("SERVICES", "s3"). | |
WithExposedPort(4566). | |
AsService() | |
} | |
func (m *DaggerTerraformAwsLocalstack) Tf(ctx context.Context, dir *Directory) (*Container, error) { | |
// https://docs.dagger.io/manuals/developer/services#bind-services-in-functions | |
cachePath := "/terraform/.terraform/plugin-cache" | |
cacheVolume := dag.CacheVolume("terraform-plugin-cache") | |
return dag.Container(). | |
From("hashicorp/terraform:latest"). | |
WithMountedCache(cachePath, cacheVolume). | |
WithServiceBinding("aws", m.localStackService()). | |
WithEnvVariable("AWS_ACCESS_KEY_ID", "test"). | |
WithEnvVariable("AWS_SECRET_ACCESS_KEY", "test"). | |
WithEnvVariable("AWS_DEFAULT_REGION", "us-east-1"). | |
WithEnvVariable("AWS_S3_ENDPOINT", "http://aws:4566"). | |
WithEnvVariable("AWS_INSECURE", "true"). | |
WithEnvVariable("TF_PLUGIN_CACHE_DIR", cachePath). | |
WithDirectory("/terraform", dir). | |
WithWorkdir("/terraform"). | |
WithoutEntrypoint(). | |
Sync(ctx) | |
} | |
// https://docs.dagger.io/manuals/developer/chaining | |
func (m *DaggerTerraformAwsLocalstack) init(ctx context.Context, tfCtr *Container) (*Container, error) { | |
ctr, err := tfCtr.WithExec([]string{"terraform", "init"}).Sync(ctx) | |
return ctr, err | |
} | |
func (m *DaggerTerraformAwsLocalstack) validate(ctx context.Context, tfCtr *Container) (*Container, error) { | |
ctr, err := tfCtr.WithExec([]string{"terraform", "validate"}).Sync(ctx) | |
return ctr, err | |
} | |
func (m *DaggerTerraformAwsLocalstack) plan(ctx context.Context, tfCtr *Container) (*Container, error) { | |
ctr, err := tfCtr.WithExec([]string{"terraform", "plan", "-out", "./plan"}).Sync(ctx) | |
return ctr, err | |
} | |
func (m *DaggerTerraformAwsLocalstack) apply(ctx context.Context, tfCtr *Container) (*Container, error) { | |
ctr, err := tfCtr.WithExec([]string{"terraform", "apply", "-auto-approve"}).Sync(ctx) | |
return ctr, err | |
} | |
func (m *DaggerTerraformAwsLocalstack) InitPlanApply(ctx context.Context, dir *Directory) (*Container, error) { | |
tfCtr, tfErr := m.Tf(ctx, dir) | |
if tfErr != nil { | |
return nil, tfErr | |
} | |
initCtr, initErr := m.init(ctx, tfCtr) | |
if initErr != nil { | |
return initCtr, initErr | |
} | |
validateCtr, validateErr := m.validate(ctx, initCtr) | |
if validateErr != nil { | |
return validateCtr, validateErr | |
} | |
planCtr, planErr := m.plan(ctx, validateCtr) | |
if planErr != nil { | |
return planCtr, planErr | |
} | |
// If there was no error in the plan, proceed with apply | |
applyCtr, applyErr := m.apply(ctx, planCtr) | |
if applyErr != nil { | |
return applyCtr, applyErr | |
} | |
return applyCtr, nil | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# main.tf | |
provider "aws" { | |
region = "eu-west-1" | |
access_key = "mock_access_key" | |
secret_key = "mock_secret_key" | |
skip_credentials_validation = true | |
skip_metadata_api_check = true | |
skip_requesting_account_id = true | |
skip_region_validation = true | |
s3_use_path_style = true | |
endpoints { | |
s3 = "http://aws:4566" | |
} | |
} | |
resource "aws_s3_bucket" "bucket" { | |
bucket = "my-bucket" | |
tags = { | |
Name = "My bucket" | |
Environment = "Dev" | |
Terraform = "true" | |
Dagger = "true" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment