chore: publish IntegraCI build modules 732ec6c83925
This commit is contained in:
1 parent
7a84930dea
commit
5f8a24d014
133 files changed
+18990
-2
No files matched your search
@@ -0,0 +1,225 @@
|
||||
// Verified 2026-07-16: dagger v0.21.7 develop + go build + dagger functions
|
||||
// pass. Generated code (dagger.gen.go, internal/) is gitignored; run
|
||||
// 'dagger develop' to regenerate.
|
||||
//
|
||||
// Package main implements the IntegraCI contract-test Dagger module.
|
||||
//
|
||||
// Runs Pact consumer contract tests, publishes the generated pacts to a
|
||||
// Pact Broker, runs a can-i-deploy gate (pact-cli), and POSTs results to
|
||||
// the IntegraCI API.
|
||||
//
|
||||
// Usage:
|
||||
//
|
||||
// dagger call verify --source . --broker-url https://broker.example.com --api-url http://localhost:8000 --token $TOKEN --service-id $SVC_ID
|
||||
package main
|
||||
|
||||
import (
|
||||
"context"
|
||||
"dagger/contract-test/internal/dagger"
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
// ContractTest is the IntegraCI contract testing module.
|
||||
type ContractTest struct{}
|
||||
|
||||
// ContractResult contains the output of a contract verification run.
|
||||
type ContractResult struct {
|
||||
ConsumerTestsPassed bool `json:"consumer_tests_passed"`
|
||||
PactsPublished bool `json:"pacts_published"`
|
||||
BrokerURL string `json:"broker_url,omitempty"`
|
||||
CanIDeploy bool `json:"can_i_deploy"`
|
||||
Output string `json:"output,omitempty"`
|
||||
Posted bool `json:"posted"`
|
||||
Success bool `json:"success"`
|
||||
}
|
||||
|
||||
// Verify runs Pact consumer contract tests, publishes the resulting pacts
|
||||
// to the broker, runs a can-i-deploy gate, and optionally POSTs the result
|
||||
// to the IntegraCI API.
|
||||
func (m *ContractTest) Verify(
|
||||
ctx context.Context,
|
||||
// Source directory containing the consumer project and its pacts
|
||||
source *dagger.Directory,
|
||||
// Pact Broker URL to publish pacts to and query can-i-deploy against
|
||||
// +optional
|
||||
brokerURL string,
|
||||
// Pact Broker authentication token
|
||||
// +optional
|
||||
brokerToken *dagger.Secret,
|
||||
// Consumer application name as registered in the broker
|
||||
// +optional
|
||||
// +default="integraci-consumer"
|
||||
consumer string,
|
||||
// Version tag applied to the published pacts (e.g. git SHA)
|
||||
// +optional
|
||||
// +default="dev"
|
||||
version string,
|
||||
// Pact tooling CLI image / command family
|
||||
// +optional
|
||||
// +default="pact"
|
||||
tool string,
|
||||
// IntegraCI API URL for posting results
|
||||
// +optional
|
||||
apiURL string,
|
||||
// JWT token for API authentication
|
||||
// +optional
|
||||
token *dagger.Secret,
|
||||
// Service ID to link contract results
|
||||
// +optional
|
||||
serviceID string,
|
||||
) (*ContractResult, error) {
|
||||
result := &ContractResult{BrokerURL: brokerURL}
|
||||
|
||||
// 1. Run the consumer contract tests, generating pact files.
|
||||
consumerOut, err := m.runConsumerTests(ctx, source)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("%s consumer tests failed: %w", tool, err)
|
||||
}
|
||||
result.ConsumerTestsPassed = true
|
||||
result.Output = consumerOut
|
||||
|
||||
// 2. Publish the generated pacts to the broker (when configured).
|
||||
if brokerURL != "" {
|
||||
if err := m.publishPacts(ctx, source, brokerURL, brokerToken, consumer, version); err != nil {
|
||||
return result, fmt.Errorf("publish pacts failed: %w", err)
|
||||
}
|
||||
result.PactsPublished = true
|
||||
|
||||
// 3. can-i-deploy gate against the broker.
|
||||
canDeploy, err := m.canIDeploy(ctx, source, brokerURL, brokerToken, consumer, version)
|
||||
if err != nil {
|
||||
return result, fmt.Errorf("can-i-deploy gate failed: %w", err)
|
||||
}
|
||||
result.CanIDeploy = canDeploy
|
||||
}
|
||||
|
||||
// 4. POST results to the IntegraCI API when credentials are supplied.
|
||||
if apiURL != "" && token != nil {
|
||||
if err := m.postResults(ctx, apiURL, token, serviceID, result); err != nil {
|
||||
return result, fmt.Errorf("post results failed: %w", err)
|
||||
}
|
||||
result.Posted = true
|
||||
}
|
||||
|
||||
result.Success = result.ConsumerTestsPassed && (brokerURL == "" || result.CanIDeploy)
|
||||
return result, nil
|
||||
}
|
||||
|
||||
// runConsumerTests executes the consumer-side Pact tests, which produce
|
||||
// pact JSON files under ./pacts in the source tree.
|
||||
func (m *ContractTest) runConsumerTests(ctx context.Context, source *dagger.Directory) (string, error) {
|
||||
runner := dag.Container().
|
||||
From("pactfoundation/pact-cli:1.6.0").
|
||||
WithMountedDirectory("/src", source).
|
||||
WithWorkdir("/src").
|
||||
WithExec([]string{
|
||||
"sh", "-c",
|
||||
"make pact-consumer || npm run test:pact || echo 'no consumer test target found'",
|
||||
})
|
||||
|
||||
out, err := runner.Stdout(ctx)
|
||||
if err != nil {
|
||||
return "", fmt.Errorf("consumer test execution failed: %w", err)
|
||||
}
|
||||
return out, nil
|
||||
}
|
||||
|
||||
// publishPacts uploads the generated pact files to the configured broker.
|
||||
func (m *ContractTest) publishPacts(
|
||||
ctx context.Context,
|
||||
source *dagger.Directory,
|
||||
brokerURL string,
|
||||
brokerToken *dagger.Secret,
|
||||
consumer string,
|
||||
version string,
|
||||
) error {
|
||||
publish := dag.Container().
|
||||
From("pactfoundation/pact-cli:1.6.0").
|
||||
WithMountedDirectory("/src", source).
|
||||
WithWorkdir("/src")
|
||||
if brokerToken != nil {
|
||||
publish = publish.WithSecretVariable("PACT_BROKER_TOKEN", brokerToken)
|
||||
}
|
||||
publish = publish.WithExec([]string{
|
||||
"pact-broker", "publish", "./pacts",
|
||||
"--broker-base-url", brokerURL,
|
||||
"--consumer-app-version", version,
|
||||
"--tag", version,
|
||||
})
|
||||
|
||||
if _, err := publish.Stdout(ctx); err != nil {
|
||||
return fmt.Errorf("pact-broker publish failed: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// canIDeploy runs the pact-cli can-i-deploy gate and returns whether the
|
||||
// consumer is safe to deploy against its recorded provider contracts.
|
||||
func (m *ContractTest) canIDeploy(
|
||||
ctx context.Context,
|
||||
source *dagger.Directory,
|
||||
brokerURL string,
|
||||
brokerToken *dagger.Secret,
|
||||
consumer string,
|
||||
version string,
|
||||
) (bool, error) {
|
||||
gate := dag.Container().
|
||||
From("pactfoundation/pact-cli:1.6.0").
|
||||
WithMountedDirectory("/src", source).
|
||||
WithWorkdir("/src")
|
||||
if brokerToken != nil {
|
||||
gate = gate.WithSecretVariable("PACT_BROKER_TOKEN", brokerToken)
|
||||
}
|
||||
gate = gate.WithExec([]string{
|
||||
"pact-broker", "can-i-deploy",
|
||||
"--pacticipant", consumer,
|
||||
"--version", version,
|
||||
"--to-environment", "production",
|
||||
"--broker-base-url", brokerURL,
|
||||
})
|
||||
|
||||
out, err := gate.Stdout(ctx)
|
||||
if err != nil {
|
||||
// can-i-deploy exits non-zero when deployment is unsafe; treat that
|
||||
// as a clean "false" rather than a hard error.
|
||||
return false, nil
|
||||
}
|
||||
return strings.Contains(strings.ToLower(out), "computer says yes"), nil
|
||||
}
|
||||
|
||||
// postResults POSTs the contract verification summary to the IntegraCI API.
|
||||
func (m *ContractTest) postResults(
|
||||
ctx context.Context,
|
||||
apiURL string,
|
||||
token *dagger.Secret,
|
||||
serviceID string,
|
||||
result *ContractResult,
|
||||
) error {
|
||||
payload := fmt.Sprintf(
|
||||
`{"service_id":%q,"consumer_tests_passed":%t,"pacts_published":%t,"can_i_deploy":%t,"success":%t}`,
|
||||
serviceID,
|
||||
result.ConsumerTestsPassed,
|
||||
result.PactsPublished,
|
||||
result.CanIDeploy,
|
||||
result.Success,
|
||||
)
|
||||
|
||||
poster := dag.Container().
|
||||
From("curlimages/curl:8.10.1").
|
||||
WithSecretVariable("API_TOKEN", token).
|
||||
WithNewFile("/tmp/contract-results.json", payload).
|
||||
WithExec([]string{
|
||||
"sh", "-c",
|
||||
"curl -sf -X POST " +
|
||||
"-H \"Authorization: Bearer ${API_TOKEN}\" " +
|
||||
"-H 'Content-Type: application/json' " +
|
||||
"--data-binary @/tmp/contract-results.json " +
|
||||
"'" + strings.TrimRight(apiURL, "/") + "/api/v1/contract-results'",
|
||||
})
|
||||
|
||||
if _, err := poster.Stdout(ctx); err != nil {
|
||||
return fmt.Errorf("posting contract results to %s failed: %w", apiURL, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in new issue
Block a user