Vault Automation 51f56b8536
[VAULT-39158, VAULT-39159]pipeline: add support for building HVD images (#9012) (#9130)
* [VAULT-39159]: pipeline: add support for querying HCP image service

In order to facilitate testing Vault Enterprise directly in HCP we need
tools to both request an image be built from a candidate build and to
also wait for the image to be available in order to execute test
scenarios with it. This PR adds a few new `pipeline` sub-commands that
can will be used for this purpose.

`pipeline github find workflow-artifact` can be used to find the path of
an artifact that matches the given filter criteria. You'll need to
provide a pull request number, workflow name, and either an exact
artifact name or a pattern. When providing a pattern only the first
match will be returned so make sure your regular expression is robust.

`pipeline hcp get image` will return the image information for an HCP
image. You will need to supply auth via the `HCP_USERNAME` and
`HCP_PASSWORD` environment variables in order to query the image
service. It also takes an enviroment flag so you can query the image
service in different environments.

`pipeline hcp wait image` is like `pipeline hcp get image` except that
it will continue to retry for a given timeout and with a given delay
between requests. In this way it can be used to wait for an image to be
available.

As part of this we also update our Go modules to the latest versions
that are compatible.



* [VAULT-39158]: actions(build-hcp-image): add workflow for building HCP images



* copywrite: add missing headers



* remove unused output



* address feedback



* allow prerelease artifacts



---------

Signed-off-by: Ryan Cragun <me@ryan.ec>
Co-authored-by: Ryan Cragun <me@ryan.ec>
2025-09-04 23:20:25 +00:00

73 lines
1.9 KiB
Go

// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: BUSL-1.1
package cmd
import (
"fmt"
"os"
"path/filepath"
"github.com/google/go-github/v74/github"
"github.com/hashicorp/vault/tools/pipeline/internal/pkg/git"
"github.com/spf13/cobra"
)
type githubCommandState struct {
Github *github.Client
Git *git.Client
}
var githubCmdState = &githubCommandState{
Github: github.NewClient(nil),
Git: git.NewClient(git.WithLoadTokenFromEnv()),
}
func newGithubCmd() *cobra.Command {
githubCmd := &cobra.Command{
Use: "github",
Short: "Github commands",
Long: "Github commands",
}
githubCmd.PersistentPreRunE = func(cmd *cobra.Command, args []string) error {
if token, set := os.LookupEnv("GITHUB_TOKEN"); set {
githubCmdState.Github = githubCmdState.Github.WithAuthToken(token)
} else {
fmt.Println("\x1b[1;33;49mWARNING\x1b[0m: GITHUB_TOKEN has not been set. While not always required for read actions on public repositories you're likely to get throttled without it")
}
return nil
}
githubCmd.AddCommand(newGithubCopyCmd())
githubCmd.AddCommand(newGithubCreateCmd())
githubCmd.AddCommand(newGithubFindCmd())
githubCmd.AddCommand(newGithubListCmd())
githubCmd.AddCommand(newGithubSyncCmd())
return githubCmd
}
func writeToGithubOutput(key string, bytes []byte) error {
devPath, ok := os.LookupEnv("GITHUB_OUTPUT")
if !ok {
return fmt.Errorf("$GITHUB_OUTPUT has not been set. Cannot write %s to it", key)
}
expanded, err := filepath.Abs(devPath)
if err != nil {
return fmt.Errorf("failed to expand $GITHUB_OUTPUT path: %w", err)
}
dev, err := os.OpenFile(expanded, os.O_APPEND|os.O_WRONLY, 0o644)
if err != nil {
return fmt.Errorf("failed to open $GITHUB_OUTPUT for writing: %w", err)
}
defer func() { _ = dev.Close() }()
_, err = dev.Write(append(append([]byte(key+"="), bytes...), []byte("\n")...))
if err != nil {
return fmt.Errorf("failed to write key %s to $GITHUB_OUTPUT: %w", key, err)
}
return nil
}