vault/tools/pipeline/internal/cmd/github_find_workflow_artifact.go
Vault Automation 3a108ea88e
Backport [VAULT-41857] pipeline(find-artifact): add support for finding artifacts from branches into ce/main (#11971)
* [VAULT-41857] pipeline(find-artifact): add support for finding artifacts from branches (#11799)

Add support for finding matching workflow artifacts from branches rather than PRs. This allows us to trigger custom HCP image builds from a branch rather than an PR. It also enables us to build and test the HCP image on a scheduled nightly cadence, which we've also enabled.

As part of these changes I also added support for specifying which environment you want to test and threaded it through the cloud scenario now that there are multiple variants. We also make the testing workflow workflow_dispatch-able so that we can trigger HVD testing for any custom image in any environment without building a new image.

Signed-off-by: Ryan Cragun <me@ryan.ec>
Co-authored-by: Ryan Cragun <me@ryan.ec>
2026-01-26 22:27:10 +00:00

65 lines
2.7 KiB
Go

// Copyright IBM Corp. 2016, 2025
// SPDX-License-Identifier: BUSL-1.1
package cmd
import (
"context"
"fmt"
"github.com/hashicorp/vault/tools/pipeline/internal/pkg/github"
"github.com/spf13/cobra"
)
var findWorkflowArtifact = &github.FindWorkflowArtifactReq{}
func newGithubFindWorkflowArtifactCmd() *cobra.Command {
findWorkflowArtifactCmd := &cobra.Command{
Use: "workflow-artifact [--pr 1234 | --branch main] [--workflow build --pattern 'vault_[0-9]' ]",
Short: "Find an artifact associated with a pull requests workflow run",
Long: "Find an artifact associated with a pull requests workflow run",
RunE: runFindGithubWorkflowArtifactCmd,
}
findWorkflowArtifactCmd.PersistentFlags().StringVarP(&findWorkflowArtifact.ArtifactName, "name", "n", "", "The exact artifact name to match")
findWorkflowArtifactCmd.PersistentFlags().StringVarP(&findWorkflowArtifact.ArtifactPattern, "pattern", "m", "", "A pattern to match an artifact. Only the first match will be returned")
findWorkflowArtifactCmd.PersistentFlags().StringVarP(&findWorkflowArtifact.Owner, "owner", "o", "hashicorp", "The Github organization")
findWorkflowArtifactCmd.PersistentFlags().StringVarP(&findWorkflowArtifact.Repo, "repo", "r", "vault", "The Github repository. Private repositories require auth via a GITHUB_TOKEN env var")
findWorkflowArtifactCmd.PersistentFlags().IntVarP(&findWorkflowArtifact.PullNumber, "pr", "p", 0, "The pull request to use as the trigger of the workflow")
findWorkflowArtifactCmd.PersistentFlags().StringVarP(&findWorkflowArtifact.Branch, "branch", "b", "", "The branch to use as the trigger of the workflow")
findWorkflowArtifactCmd.PersistentFlags().StringVarP(&findWorkflowArtifact.WorkflowName, "workflow", "w", "", "The name of the workflow the artifact will be associated with")
findWorkflowArtifactCmd.PersistentFlags().BoolVar(&findWorkflowArtifact.WriteToGithubOutput, "github-output", false, "Whether or not to write 'workflow-artifact' to $GITHUB_OUTPUT")
return findWorkflowArtifactCmd
}
func runFindGithubWorkflowArtifactCmd(cmd *cobra.Command, args []string) error {
cmd.SilenceUsage = true // Don't spam the usage on failure
res, err := findWorkflowArtifact.Run(context.TODO(), githubCmdState.GithubV3)
if err != nil {
return fmt.Errorf("finding workflow artifact: %w", err)
}
switch rootCfg.format {
case "json":
jsonBytes, err := res.ToJSON()
if err != nil {
return err
}
fmt.Println(string(jsonBytes))
default:
fmt.Println(res.ToTable())
}
if findWorkflowArtifact.WriteToGithubOutput {
jsonBytes, err := res.ToGithubOutput()
if err != nil {
return err
}
return writeToGithubOutput("workflow-artifact", jsonBytes)
}
return nil
}