Add some unit test and dockerfile

This commit is contained in:
Daisuke Nakahara 2025-05-04 21:02:44 +09:00
parent 1bee169123
commit 0819ae1a71
7 changed files with 474 additions and 3 deletions

170
app/main.go Normal file
View file

@ -0,0 +1,170 @@
package main
import (
"context"
"encoding/json"
"fmt"
"log"
"os"
"os/exec"
"path/filepath"
"github.com/aws/aws-lambda-go/lambda"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/feature/s3/manager"
"github.com/aws/aws-sdk-go-v2/service/s3"
)
type Config struct {
RepoURL string
RepoBranch string
S3Bucket string
S3Key string
AWSRegion string
}
var commandRunner = exec.Command
func handleRequest(ctx context.Context, event json.RawMessage) error {
// Call your existing process (for example, runDeploymentProcess)
if err := runDeploymentProcess(ctx); err != nil {
// Log the error; you may also report it via CloudWatch alarms
log.Printf("Deployment process failed: %v", err)
return err
}
return nil
}
func main() {
lambda.Start(handleRequest)
}
func runDeploymentProcess(ctx context.Context) error {
cfg, err := loadConfig()
if err != nil {
log.Fatalf("Configuration error: %v", err)
return err
}
// Create a unique temp directory for this run
repoDir, err := os.MkdirTemp("", "repo-*")
if err != nil {
log.Fatalf("Error creating temporary directory: %v", err)
return err
}
defer os.RemoveAll(repoDir)
zipFilePath := filepath.Join(repoDir, "source.zip")
// 1. Clone the repository
if err := cloneRepository(ctx, cfg.RepoURL, cfg.RepoBranch, repoDir); err != nil {
log.Fatalf("Failure in cloning: %v", err)
return err
}
// 2. Create a ZIP archive of the repository
if err := createZipArchive(ctx, repoDir, zipFilePath); err != nil {
log.Fatalf("Failure in creating ZIP archive: %v", err)
return err
}
// 3. Upload the ZIP file to S3
cfg_s3, err := config.LoadDefaultConfig(ctx, config.WithRegion(cfg.AWSRegion))
if err != nil {
log.Fatalf("Error loading configuration: %v", err)
return err
}
s3Client := s3.NewFromConfig(cfg_s3)
uploader := manager.NewUploader(s3Client)
if err := uploadToS3WithUploader(ctx, zipFilePath, cfg.S3Bucket, cfg.S3Key, uploader); err != nil {
log.Fatalf("Failure in uploading to S3: %v", err)
return err
}
return nil
}
func loadConfig() (*Config, error) {
repoURL := os.Getenv("REPO_URL")
if repoURL == "" {
return nil, fmt.Errorf("REPO_URL environment variable not set")
}
repoBranch := os.Getenv("REPO_BRANCH")
if repoBranch == "" {
repoBranch = "main"
}
s3Bucket := os.Getenv("S3_BUCKET")
if s3Bucket == "" {
return nil, fmt.Errorf("S3_BUCKET environment variable not set")
}
s3Key := os.Getenv("S3_KEY")
if s3Key == "" {
s3Key = "source.zip"
}
awsRegion := os.Getenv("AWS_REGION")
if awsRegion == "" {
awsRegion = "ap-northeast-1"
}
return &Config{
RepoURL: repoURL,
RepoBranch: repoBranch,
S3Bucket: s3Bucket,
S3Key: s3Key,
AWSRegion: awsRegion,
}, nil
}
func cloneRepository(_ context.Context, repoURL, repoBranch, repoDir string) error {
cloneCmd := commandRunner("git", "clone", "--branch", repoBranch, repoURL, repoDir)
cloneCmd.Stdout = os.Stdout
cloneCmd.Stderr = os.Stderr
fmt.Printf("Cloning repository %s (branch %s)...\n", repoURL, repoBranch)
if err := cloneCmd.Run(); err != nil {
return fmt.Errorf("error cloning repository: %v", err)
}
fmt.Println("Repository cloned successfully.")
return nil
}
func createZipArchive(_ context.Context, repoDir, zipFilePath string) error {
zipCmd := commandRunner("zip", "-r", zipFilePath, ".")
zipCmd.Dir = repoDir // Change to the cloned repo directory
zipCmd.Stdout = os.Stdout
zipCmd.Stderr = os.Stderr
fmt.Println("Creating ZIP archive of the repository...")
if err := zipCmd.Run(); err != nil {
return fmt.Errorf("error creating ZIP archive: %v", err)
}
fmt.Printf("ZIP archive created at %s.\n", zipFilePath)
return nil
}
type Uploader interface {
Upload(ctx context.Context, input *s3.PutObjectInput, opts ...func(*manager.Uploader)) (*manager.UploadOutput, error)
}
func uploadToS3WithUploader(ctx context.Context, zipPath, bucket, key string, uploader Uploader) error {
// Open the ZIP file
f, err := os.Open(zipPath)
if err != nil {
log.Fatalf("Error opening ZIP file: %v", err)
}
defer f.Close()
// Upload the file to S3.
fmt.Printf("Uploading %s to s3://%s/%s...\n", zipPath, bucket, key)
result, err := uploader.Upload(ctx, &s3.PutObjectInput{
Bucket: aws.String(bucket),
Key: aws.String(key),
Body: f,
})
if err != nil {
return fmt.Errorf("failed to upload file: %v", err)
}
fmt.Printf("Successfully uploaded to %s\n", result.Location)
return nil
}