|
| 1 | +// This Source Code Form is subject to the terms of the Mozilla Public |
| 2 | +// License, v. 2.0. If a copy of the MPL was not distributed with this |
| 3 | +// file, You can obtain one at http://mozilla.org/MPL/2.0/. |
| 4 | + |
| 5 | +package js |
| 6 | + |
| 7 | +import ( |
| 8 | + "fmt" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "github.com/siderolabs/kres/internal/config" |
| 12 | + "github.com/siderolabs/kres/internal/dag" |
| 13 | + "github.com/siderolabs/kres/internal/output/ghworkflow" |
| 14 | + "github.com/siderolabs/kres/internal/project/meta" |
| 15 | +) |
| 16 | + |
| 17 | +// Chromatic generates a standalone GitHub Actions workflow that publishes |
| 18 | +// Storybook snapshots to Chromatic on every push and pull request. |
| 19 | +type Chromatic struct { |
| 20 | + dag.BaseNode |
| 21 | + |
| 22 | + meta *meta.Options |
| 23 | + |
| 24 | + SOPSExtractKey string `yaml:"sopsExtractKey,omitempty"` |
| 25 | + Enabled bool `yaml:"enabled"` |
| 26 | +} |
| 27 | + |
| 28 | +// NewChromatic creates a new Chromatic node. |
| 29 | +func NewChromatic(meta *meta.Options) *Chromatic { |
| 30 | + return &Chromatic{ |
| 31 | + BaseNode: dag.NewBaseNode("chromatic"), |
| 32 | + meta: meta, |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +// CompileGitHubWorkflow implements ghworkflow.Compiler. |
| 37 | +func (c *Chromatic) CompileGitHubWorkflow(o *ghworkflow.Output) error { |
| 38 | + if !c.Enabled { |
| 39 | + return nil |
| 40 | + } |
| 41 | + |
| 42 | + nodeVersion := strings.TrimSuffix(config.NodeContainerImageVersion, "-alpine") |
| 43 | + |
| 44 | + checkoutStep := ghworkflow.Step("Checkout code"). |
| 45 | + SetUsesWithComment( |
| 46 | + "actions/checkout@"+config.CheckOutActionRef, |
| 47 | + "version: "+config.CheckOutActionVersion, |
| 48 | + ). |
| 49 | + SetWith("fetch-depth", "0") |
| 50 | + |
| 51 | + setupNodeStep := ghworkflow.Step("Setup Node.js"). |
| 52 | + SetUsesWithComment( |
| 53 | + "actions/setup-node@"+config.SetupNodeActionRef, |
| 54 | + "version: "+config.SetupNodeActionVersion, |
| 55 | + ). |
| 56 | + SetWith("node-version", nodeVersion) |
| 57 | + |
| 58 | + installStep := &ghworkflow.JobStep{ |
| 59 | + Name: "Install dependencies", |
| 60 | + Run: "npm ci\n", |
| 61 | + WorkingDirectory: "frontend/", |
| 62 | + } |
| 63 | + |
| 64 | + var getTokenStep *ghworkflow.JobStep |
| 65 | + |
| 66 | + if c.SOPSExtractKey != "" { |
| 67 | + getTokenStep = &ghworkflow.JobStep{ |
| 68 | + Name: "Get token", |
| 69 | + Run: fmt.Sprintf( |
| 70 | + "chromaticProjectToken=$(sops decrypt --extract='%s' .secrets.yaml)\n"+ |
| 71 | + "echo \"::add-mask::${chromaticProjectToken}\"\n"+ |
| 72 | + "echo \"CHROMATIC_PROJECT_TOKEN=${chromaticProjectToken}\" >> $GITHUB_ENV\n", |
| 73 | + c.SOPSExtractKey, |
| 74 | + ), |
| 75 | + } |
| 76 | + } |
| 77 | + |
| 78 | + chromaticStep := ghworkflow.Step("Run Chromatic"). |
| 79 | + SetID("chromatic"). |
| 80 | + SetUsesWithComment( |
| 81 | + "chromaui/action@"+config.ChromaticActionRef, |
| 82 | + "version: "+config.ChromaticActionVersion, |
| 83 | + ). |
| 84 | + SetWith("projectToken", "${{ env.CHROMATIC_PROJECT_TOKEN }}"). |
| 85 | + SetWith("workingDir", "frontend/") |
| 86 | + |
| 87 | + steps := []*ghworkflow.JobStep{checkoutStep, setupNodeStep, installStep} |
| 88 | + if getTokenStep != nil { |
| 89 | + steps = append(steps, getTokenStep) |
| 90 | + } |
| 91 | + |
| 92 | + steps = append(steps, chromaticStep) |
| 93 | + |
| 94 | + o.AddWorkflow( |
| 95 | + "chromatic", |
| 96 | + &ghworkflow.Workflow{ |
| 97 | + Name: "chromatic", |
| 98 | + Concurrency: ghworkflow.Concurrency{ |
| 99 | + Group: "${{ github.workflow }}-${{ github.head_ref || github.run_id }}", |
| 100 | + CancelInProgress: true, |
| 101 | + }, |
| 102 | + On: ghworkflow.On{ |
| 103 | + Push: ghworkflow.Push{ |
| 104 | + Branches: ghworkflow.Branches{ |
| 105 | + c.meta.MainBranch, |
| 106 | + "release-*", |
| 107 | + }, |
| 108 | + Tags: []string{"v*"}, |
| 109 | + }, |
| 110 | + PullRequest: ghworkflow.PullRequest{ |
| 111 | + Branches: ghworkflow.Branches{ |
| 112 | + c.meta.MainBranch, |
| 113 | + "release-*", |
| 114 | + }, |
| 115 | + }, |
| 116 | + }, |
| 117 | + // https://www.chromatic.com/docs/github-actions/ |
| 118 | + Jobs: map[string]*ghworkflow.Job{ |
| 119 | + "chromatic": { |
| 120 | + Name: "Run chromatic", |
| 121 | + RunsOn: ghworkflow.NewRunsOnGroupLabel(ghworkflow.GenericRunner, ""), |
| 122 | + Environment: &ghworkflow.JobEnvironment{ |
| 123 | + Name: "Storybook preview", |
| 124 | + URL: "${{ steps.chromatic.outputs.storybookUrl }}", |
| 125 | + }, |
| 126 | + Steps: steps, |
| 127 | + }, |
| 128 | + }, |
| 129 | + }, |
| 130 | + ) |
| 131 | + |
| 132 | + return nil |
| 133 | +} |
0 commit comments