Skip to content

Add --template flag to sc cicd generate for selective workflow generation #64

@alex-y-su

Description

@alex-y-su

Summary

Extend sc cicd generate with a --template flag to allow generating specific workflow types (e.g., only pr-preview), rather than generating all configured templates at once.

Background

Simple Container is a cloud-agnostic deployment tool that simplifies microservice deployment across AWS, GCP, and Kubernetes. It provides:

  • Infrastructure-as-Code using lightweight YAML configurations
  • Automated deployments via sc deploy command
  • GitHub Actions integration via sc cicd generate
  • Secrets management with sc secrets commands

Currently, sc cicd generate generates all workflow types configured in server.yaml under workflow-generation.templates. This feature request proposes adding a --template flag to selectively generate specific workflows.

Motivation

PR preview environments are critical for modern development workflows:

  1. Code Review Quality - Reviewers can test changes in a live environment before approving
  2. Faster Feedback Loops - Developers see their changes deployed without waiting for merge
  3. Reduced Production Risks - Issues are caught earlier in isolated environments
  4. Stakeholder Demos - Non-technical stakeholders can preview features via unique URLs

Being able to generate just the pr-preview workflow independently would be useful for:

  • Teams adding PR previews to an existing CI/CD setup
  • Iterating on preview workflow configuration without regenerating all workflows
  • Documentation and onboarding examples

Proposed Solution

Extended Command: sc cicd generate --template

# Generate only PR preview workflow for a stack
sc cicd generate --stack myorg/myapp --template pr-preview

# Generate multiple specific templates
sc cicd generate --stack myorg/myapp --template deploy --template pr-preview

# Dry run to preview what would be generated
sc cicd generate --stack myorg/myapp --template pr-preview --dry-run

# Generate all templates (current behavior, unchanged)
sc cicd generate --stack myorg/myapp

Available Templates

The --template flag would accept any of the existing workflow templates:

Template Description
deploy Deploy stack on push to main/default branch
destroy Destroy stack (with confirmation)
destroy-parent Destroy parent infrastructure stack
provision Provision infrastructure resources
pr-preview Deploy ephemeral preview environments for PRs

PR Preview Workflow Features

The generated pr-preview workflow includes:

Feature Description
Label-triggered deploys Deploy only when a specific label (e.g., deploy-preview) is added to PR
Automatic cleanup Destroy preview environment when PR is closed/merged
Dynamic URLs Generate unique URLs like pr-123.preview.myapp.com
PR comments Automatically comment with preview URL and cleanup status

Generated PR Preview Workflow Behavior

PR Opened/Updated + "deploy-preview" label
    │
    ▼
┌─────────────────────┐
│ Deploy Preview Env  │
│ - Build container   │
│ - Deploy to cloud   │
│ - Create DNS record │
└─────────────────────┘
    │
    ▼
┌─────────────────────┐
│ Comment on PR       │
│ "Preview: https://  │
│  pr-123.app.com"    │
└─────────────────────┘

PR Closed/Merged
    │
    ▼
┌─────────────────────┐
│ Destroy Preview Env │
│ - Remove resources  │
│ - Delete DNS record │
│ - Comment cleanup   │
└─────────────────────┘

Configuration in server.yaml

To enable PR preview workflow generation, configure a preview environment and include pr-preview in templates:

cicd:
  type: github-actions
  config:
    organization: "my-company"

    environments:
      staging:
        type: staging
        auto-deploy: true

      production:
        type: production
        protection: true

      # Preview environment for PR deployments
      preview:
        type: preview
        protection: false
        auto-deploy: true
        runner: "ubuntu-latest"
        pr-preview:
          label-trigger: "deploy-preview"
          domain-base: "preview.myapp.com"

    workflow-generation:
      enabled: true
      templates: ["deploy", "destroy", "pr-preview"]  # Include pr-preview

Benefits

For Developers

  • Generate only the workflow needed: sc cicd generate --template pr-preview
  • Quick iteration on preview configuration
  • No need to regenerate all workflows when updating one

For DevOps

  • Selective workflow updates without touching production workflows
  • Easier migration path for adding PR previews to existing setups
  • Fine-grained control over generated files

For Organizations

  • Improved code review process with live previews
  • Faster release cycles
  • Reduced production incidents

Alternatives Considered

  1. Generate all templates always - Current behavior; works but forces regeneration of all workflows
  2. Separate sc cicd preview command - Adds complexity; extending existing command is simpler
  3. Manual workflow creation - Error-prone and requires GitHub Actions expertise

Additional Context

Related Documentation

Similar Features in Other Tools

  • Vercel Preview Deployments
  • Netlify Deploy Previews
  • Railway PR Environments
  • Render Pull Request Previews

Implementation Notes

The existing codebase already has:

  • PR preview template in pkg/clouds/github/templates.go:346 (prPreviewTemplate)
  • Template loading in pkg/clouds/github/workflow_generator.go:52-58 (LoadTemplates())
  • Available templates via GetWorkflowTemplateNames(): ["deploy", "destroy", "destroy-parent", "provision", "pr-preview"]
  • Generate command in pkg/cmd/cmd_cicd/cmd_generate.go

Suggested Implementation

  1. Add --template flag to cmd_generate.go:
cmd.Flags().StringSliceVar(&params.Templates, "template", []string{},
    "Generate specific templates only (e.g., --template pr-preview). Can be specified multiple times.")
  1. Extend GenerateParams in pkg/assistant/cicd/service.go:
type GenerateParams struct {
    // ... existing fields ...
    Templates []string  // Filter to generate only these templates
}
  1. Filter templates in WorkflowGenerator.GenerateWorkflows():
templatesToGenerate := wg.config.WorkflowGeneration.Templates
if len(filterTemplates) > 0 {
    // Validate and use only specified templates
    templatesToGenerate = filterTemplates
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions