Skip to content

Commit

Permalink
feat(cli): performs dry-runs on deployments
Browse files Browse the repository at this point in the history
  • Loading branch information
jmgilman committed Jan 17, 2025
1 parent 924e6bb commit cd446e4
Show file tree
Hide file tree
Showing 10 changed files with 63 additions and 53 deletions.
24 changes: 12 additions & 12 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Forge
uses: input-output-hk/catalyst-forge/actions/install@master
uses: input-output-hk/catalyst-forge/actions/install@deploy-dry-run
if: ${{ inputs.forge_version != 'local' }}
with:
version: ${{ inputs.forge_version }}
- name: Install Local Forge
uses: input-output-hk/catalyst-forge/actions/install-local@master
uses: input-output-hk/catalyst-forge/actions/install-local@deploy-dry-run
if: ${{ inputs.forge_version == 'local' }}
with:
earthly_token: ${{ secrets.earthly_token }}
Expand All @@ -61,14 +61,14 @@ jobs:
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Setup CI
uses: input-output-hk/catalyst-forge/actions/setup@master
uses: input-output-hk/catalyst-forge/actions/setup@deploy-dry-run
with:
skip_docker: 'true'
skip_github: 'true'
skip_earthly: ${{ steps.local.outputs.skip }}
- name: Discovery
id: discovery
uses: input-output-hk/catalyst-forge/actions/discovery@master
uses: input-output-hk/catalyst-forge/actions/discovery@deploy-dry-run
with:
filters: |
${{ env.FORGE_REGEX_CHECK }}
Expand All @@ -80,7 +80,7 @@ jobs:
${{ env.FORGE_REGEX_PUBLISH }}
check:
uses: input-output-hk/catalyst-forge/.github/workflows/run.yml@master
uses: input-output-hk/catalyst-forge/.github/workflows/run.yml@deploy-dry-run
needs: [discover]
if: (fromJson(needs.discover.outputs.earthfiles)['^check(-.*)?$'] != null) && !failure() && !cancelled()
with:
Expand All @@ -92,7 +92,7 @@ jobs:
earthly_token: ${{ secrets.earthly_token }}

build:
uses: input-output-hk/catalyst-forge/.github/workflows/run.yml@master
uses: input-output-hk/catalyst-forge/.github/workflows/run.yml@deploy-dry-run
needs: [discover, check]
if: (fromJson(needs.discover.outputs.earthfiles)['^build(-.*)?$'] != null) && !failure() && !cancelled()
with:
Expand All @@ -104,7 +104,7 @@ jobs:
earthly_token: ${{ secrets.earthly_token }}

package:
uses: input-output-hk/catalyst-forge/.github/workflows/run.yml@master
uses: input-output-hk/catalyst-forge/.github/workflows/run.yml@deploy-dry-run
needs: [discover, check, build]
if: (fromJson(needs.discover.outputs.earthfiles)['^package(-.*)?$'] != null) && !failure() && !cancelled()
with:
Expand All @@ -116,7 +116,7 @@ jobs:
earthly_token: ${{ secrets.earthly_token }}

test:
uses: input-output-hk/catalyst-forge/.github/workflows/run.yml@master
uses: input-output-hk/catalyst-forge/.github/workflows/run.yml@deploy-dry-run
needs: [discover, check, build, package]
if: (fromJson(needs.discover.outputs.earthfiles)['^test(-.*)?$'] != null) && !failure() && !cancelled()
with:
Expand All @@ -128,7 +128,7 @@ jobs:
earthly_token: ${{ secrets.earthly_token }}

docs:
uses: input-output-hk/catalyst-forge/.github/workflows/docs.yml@master
uses: input-output-hk/catalyst-forge/.github/workflows/docs.yml@deploy-dry-run
needs: [discover, check, build, test]
if: (fromJson(needs.discover.outputs.earthfiles)['^docs(-.*)?$'] != null) && !failure() && !cancelled()
with:
Expand All @@ -138,7 +138,7 @@ jobs:
earthly_token: ${{ secrets.earthly_token }}

release:
uses: input-output-hk/catalyst-forge/.github/workflows/release.yml@master
uses: input-output-hk/catalyst-forge/.github/workflows/release.yml@deploy-dry-run
needs: [discover, check, build, test]
if: (fromJson(needs.discover.outputs.releases)[0] != null) && !failure() && !cancelled()
with:
Expand All @@ -150,7 +150,7 @@ jobs:
earthly_token: ${{ secrets.earthly_token }}

deploy:
uses: input-output-hk/catalyst-forge/.github/workflows/deploy.yml@master
uses: input-output-hk/catalyst-forge/.github/workflows/deploy.yml@deploy-dry-run
needs: [discover, check, build, test, release]
if: (fromJson(needs.discover.outputs.deployments)[0] != null) && !failure() && !cancelled()
with:
Expand All @@ -162,7 +162,7 @@ jobs:
earthly_token: ${{ secrets.earthly_token }}

final:
needs: [check, build, package, test, release]
needs: [check, build, package, test, release, deploy]
if: ${{ always() && (contains(needs.*.result, 'failure') || !failure() && !cancelled()) }}
runs-on: ubuntu-latest
steps:
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,12 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Forge
uses: input-output-hk/catalyst-forge/actions/install@master
uses: input-output-hk/catalyst-forge/actions/install@deploy-dry-run
if: ${{ inputs.forge_version != 'local' }}
with:
version: ${{ inputs.forge_version }}
- name: Install Local Forge
uses: input-output-hk/catalyst-forge/actions/install-local@master
uses: input-output-hk/catalyst-forge/actions/install-local@deploy-dry-run
if: ${{ inputs.forge_version == 'local' }}
with:
earthly_token: ${{ secrets.earthly_token }}
Expand All @@ -61,11 +61,11 @@ jobs:
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Setup CI
uses: input-output-hk/catalyst-forge/actions/setup@master
uses: input-output-hk/catalyst-forge/actions/setup@deploy-dry-run
with:
skip_earthly: ${{ steps.local.outputs.skip }}
- name: Deploy
uses: input-output-hk/catalyst-forge/actions/run@master
uses: input-output-hk/catalyst-forge/actions/run@deploy-dry-run
with:
command: deploy push
args: ${{ matrix.deployment }}
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/docs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Forge
uses: input-output-hk/catalyst-forge/actions/install@master
uses: input-output-hk/catalyst-forge/actions/install@deploy-dry-run
if: ${{ inputs.forge_version != 'local' }}
with:
version: ${{ inputs.forge_version }}
- name: Install Local Forge
uses: input-output-hk/catalyst-forge/actions/install-local@master
uses: input-output-hk/catalyst-forge/actions/install-local@deploy-dry-run
if: ${{ inputs.forge_version == 'local' }}
with:
earthly_token: ${{ secrets.earthly_token }}
Expand All @@ -64,11 +64,11 @@ jobs:
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Setup CI
uses: input-output-hk/catalyst-forge/actions/setup@master
uses: input-output-hk/catalyst-forge/actions/setup@deploy-dry-run
with:
skip_earthly: ${{ steps.local.outputs.skip }}
- name: Run
uses: input-output-hk/catalyst-forge/actions/run@master
uses: input-output-hk/catalyst-forge/actions/run@deploy-dry-run
with:
command: run
args: --artifact ${{ env.OUTPUT }} ${{ matrix.earthfile }}
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,12 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Forge
uses: input-output-hk/catalyst-forge/actions/install@master
uses: input-output-hk/catalyst-forge/actions/install@deploy-dry-run
if: ${{ inputs.forge_version != 'local' }}
with:
version: ${{ inputs.forge_version }}
- name: Install Local Forge
uses: input-output-hk/catalyst-forge/actions/install-local@master
uses: input-output-hk/catalyst-forge/actions/install-local@deploy-dry-run
if: ${{ inputs.forge_version == 'local' }}
with:
earthly_token: ${{ secrets.earthly_token }}
Expand All @@ -65,11 +65,11 @@ jobs:
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Setup CI
uses: input-output-hk/catalyst-forge/actions/setup@master
uses: input-output-hk/catalyst-forge/actions/setup@deploy-dry-run
with:
skip_earthly: ${{ steps.local.outputs.skip }}
- name: Release
uses: input-output-hk/catalyst-forge/actions/run@master
uses: input-output-hk/catalyst-forge/actions/run@deploy-dry-run
with:
command: release
args: ${{ matrix.release.project }} ${{ matrix.release.name }}
Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/run.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,12 @@ jobs:
steps:
- uses: actions/checkout@v4
- name: Install Forge
uses: input-output-hk/catalyst-forge/actions/install@master
uses: input-output-hk/catalyst-forge/actions/install@deploy-dry-run
if: ${{ inputs.forge_version != 'local' }}
with:
version: ${{ inputs.forge_version }}
- name: Install Local Forge
uses: input-output-hk/catalyst-forge/actions/install-local@master
uses: input-output-hk/catalyst-forge/actions/install-local@deploy-dry-run
if: ${{ inputs.forge_version == 'local' }}
with:
earthly_token: ${{ secrets.earthly_token }}
Expand All @@ -62,11 +62,11 @@ jobs:
echo "skip=false" >> $GITHUB_OUTPUT
fi
- name: Setup CI
uses: input-output-hk/catalyst-forge/actions/setup@master
uses: input-output-hk/catalyst-forge/actions/setup@deploy-dry-run
with:
skip_earthly: ${{ steps.local.outputs.skip }}
- name: Run
uses: input-output-hk/catalyst-forge/actions/run@master
uses: input-output-hk/catalyst-forge/actions/run@deploy-dry-run
with:
command: run
args: ${{ matrix.earthfile }}
Expand Down
7 changes: 4 additions & 3 deletions cli/cmd/cmds/deploy/deploy.go → cli/cmd/cmds/deploy/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,14 @@ func (c *PushCmd) Run(ctx run.RunContext) error {
return fmt.Errorf("could not load project: %w", err)
}

var dryrun bool
eh := events.NewDefaultEventHandler(ctx.Logger)
if !eh.Firing(&project, project.GetDeploymentEvents()) && !c.Force {
ctx.Logger.Info("No deployment event is firing, skipping deployment")
return nil
ctx.Logger.Info("No deployment event is firing, performing dry-run")
dryrun = true
}

deployer := deployment.NewGitopsDeployer(&project, &ctx.SecretStore, ctx.Logger)
deployer := deployment.NewGitopsDeployer(&project, &ctx.SecretStore, ctx.Logger, dryrun)
if err := deployer.Load(); err != nil {
return fmt.Errorf("could not load deployer: %w", err)
}
Expand Down
37 changes: 24 additions & 13 deletions cli/pkg/deployment/gitops.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ func (g gitRemote) Push(repo *git.Repository, o *git.PushOptions) error {

// GitopsDeployer is a deployer that deploys projects to a GitOps repository.
type GitopsDeployer struct {
dryrun bool
fs billy.Filesystem
repo *git.Repository
kcl KCLRunner
Expand Down Expand Up @@ -108,21 +109,29 @@ func (g *GitopsDeployer) Deploy() error {
}
}

changes, err := g.hasChanges()
if err != nil {
return fmt.Errorf("could not check if worktree has changes: %w", err)
} else if !changes {
return ErrNoChanges
}
if !g.dryrun {
changes, err := g.hasChanges()
if err != nil {
return fmt.Errorf("could not check if worktree has changes: %w", err)
} else if !changes {
return ErrNoChanges
}

g.logger.Info("Committing changes", "path", bundlePath)
if err := g.commit(); err != nil {
return fmt.Errorf("could not commit changes: %w", err)
}
g.logger.Info("Committing changes", "path", bundlePath)
if err := g.commit(); err != nil {
return fmt.Errorf("could not commit changes: %w", err)
}

g.logger.Info("Pushing changes")
if err := g.push(); err != nil {
return fmt.Errorf("could not push changes: %w", err)
g.logger.Info("Pushing changes")
if err := g.push(); err != nil {
return fmt.Errorf("could not push changes: %w", err)
}
} else {
g.logger.Info("Dry-run: not committing or pushing changes")
g.logger.Info("Dumping manifests")
for _, r := range result {
fmt.Print(r.Manifests)
}
}

return nil
Expand Down Expand Up @@ -245,12 +254,14 @@ func NewGitopsDeployer(
project *project.Project,
store *secrets.SecretStore,
logger *slog.Logger,
dryrun bool,
) GitopsDeployer {
if logger == nil {
logger = slog.New(slog.NewTextHandler(io.Discard, nil))
}

return GitopsDeployer{
dryrun: dryrun,
fs: memfs.New(),
kcl: NewKCLRunner(logger),
logger: logger,
Expand Down
2 changes: 1 addition & 1 deletion cli/pkg/deployment/kcl.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@ func encodeValues(ctx *cue.Context, module schema.Module) ([]byte, error) {

// run runs a KCL module with the given module container and arguments.
func (k *KCLRunner) run(container string, moduleArgs KCLModuleArgs) ([]byte, error) {
args := []string{"run", "-q"}
args := []string{"run", "-q", "--no_style"}
args = append(args, moduleArgs.Serialize()...)
args = append(args, fmt.Sprintf("oci://%s", container))

Expand Down
4 changes: 2 additions & 2 deletions cli/pkg/deployment/kcl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,8 @@ func TestKCLRunnerRunDeployment(t *testing.T) {
assert.Equal(t, "key: value\n", r.result["main"].Values)
assert.Equal(t, "output", r.result["support"].Manifests)
assert.Equal(t, "key1: value1\n", r.result["support"].Values)
assert.Contains(t, r.calls, "run -q -D name= -D namespace=default -D values={\"key\":\"value\"} -D 1.0.0 oci://test.com/module")
assert.Contains(t, r.calls, "run -q -D name= -D namespace=default -D values={\"key1\":\"value1\"} -D 1.0.0 oci://test.com/module1")
assert.Contains(t, r.calls, "run -q --no_style -D name= -D namespace=default -D values={\"key\":\"value\"} -D 1.0.0 oci://test.com/module")
assert.Contains(t, r.calls, "run -q --no_style -D name= -D namespace=default -D values={\"key1\":\"value1\"} -D 1.0.0 oci://test.com/module1")
},
},
{
Expand Down
10 changes: 4 additions & 6 deletions foundry/api/blueprint.cue
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ project: {
}
deployment: {
on: {
//merge: {}
//tag: {}
always: {}
merge: {}
tag: {}
}
environment: "dev"
modules: {
Expand Down Expand Up @@ -47,9 +46,8 @@ project: {
release: {
docker: {
on: {
//merge: {}
//tag: {}
always: {}
merge: {}
tag: {}
}
config: {
tag: _ @forge(name="GIT_HASH_OR_TAG")
Expand Down

0 comments on commit cd446e4

Please sign in to comment.