Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: VHD release automation to go #5125

Merged
merged 11 commits into from
Oct 21, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 14 additions & 9 deletions .pipelines/.vsts-vhd-automation.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@ pool:
name: $(1ES_DevInfra_Auto_TearDown_Pool_Name)

parameters:
- name: VHD_BUILD_ID
displayName: VHD Build ID
type: string
- name: ImageBump
displayName: Image Bump + Create Official Branch
type: boolean
Expand Down Expand Up @@ -31,15 +34,15 @@ stages:
ADO_PAT: $(PAT-aksdevassistant)
- template: ./templates/.set-image-version-template.yaml
parameters:
VHD_BUILD_ID: $(VHD_BUILD_ID)
IMAGE_VERSION: $(IMAGE_VERSION) # pass in the image version in case one was supplied
VHD_BUILD_ID: ${{ parameters.VHD_BUILD_ID }}
IMAGE_VERSION_OVERRIDE: $(IMAGE_VERSION_OVERRIDE) # pass in the image version in case one was supplied
- bash: |
echo "PR for Image Bumping, Official Branch Cutting"
/bin/bash vhdbuilder/scripts/automate_version_bump.sh
env:
GITHUB_PAT: $(GITHUB_PAT)
IMAGE_VERSION: $(IMAGE_VERSION)
VHD_BUILD_ID: $(VHD_BUILD_ID)
VHD_BUILD_ID: ${{ parameters.VHD_BUILD_ID }}
displayName: 'Bump image version and create official branch'
- stage: generate_release_notes
dependsOn: []
Expand All @@ -53,15 +56,15 @@ stages:
ADO_PAT: $(PAT-aksdevassistant)
- template: ./templates/.set-image-version-template.yaml
parameters:
VHD_BUILD_ID: $(VHD_BUILD_ID)
IMAGE_VERSION: $(IMAGE_VERSION)
VHD_BUILD_ID: ${{ parameters.VHD_BUILD_ID }}
IMAGE_VERSION_OVERRIDE: $(IMAGE_VERSION_OVERRIDE)
- bash: |
echo "PR for Release Notes"
/bin/bash vhdbuilder/scripts/automate_release_notes.sh
env:
GITHUB_PAT: $(GITHUB_PAT)
IMAGE_VERSION: $(IMAGE_VERSION)
VHD_BUILD_ID: $(VHD_BUILD_ID)
VHD_BUILD_ID: ${{ parameters.VHD_BUILD_ID }}
SKIP_LATEST: $(SKIP_LATEST_RELEASE_NOTES_UPDATE)
displayName: 'Generate release notes'
- stage: create_release
Expand All @@ -76,9 +79,11 @@ stages:
ADO_PAT: $(PAT-aksdevassistant)
- bash: |
set +x
echo "Creating EV2 artifacts and corresponding SIG release"
/bin/bash vhdbuilder/scripts/automate_release.sh
set -e
pushd vhdbuilder/automation/cmd
go run main.go
popd
env:
ADO_PAT: $(PAT-aksdevassistant)
VHD_BUILD_ID: $(VHD_BUILD_ID)
VHD_BUILD_ID: ${{ parameters.VHD_BUILD_ID }}
displayName: 'Create SIG release'
6 changes: 3 additions & 3 deletions .pipelines/templates/.set-image-version-template.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
parameters:
- name: VHD_BUILD_ID
type: string
- name: IMAGE_VERSION
- name: IMAGE_VERSION_OVERRIDE
type: string
default: ""

steps:
- bash: |
[ -n "$IMAGE_VERSION" ] && echo "IMAGE_VERSION is already set to $IMAGE_VERSION" && exit 0
[ -n "$IMAGE_VERSION_OVERRIDE" ] && echo "IMAGE_VERSION_OVERRIDE is set to $IMAGE_VERSION_OVERRIDE" && exit 0
source vhdbuilder/scripts/automate_generate_version.sh
echo "setting image version to $GENERATED_IMAGE_VERSION"
echo "##vso[task.setvariable variable=IMAGE_VERSION]$GENERATED_IMAGE_VERSION"
env:
VHD_BUILD_ID: ${{ parameters.VHD_BUILD_ID }}
IMAGE_VERSION: ${{ parameters.IMAGE_VERSION }}
displayName: 'Set image version'
- bash: |
echo "removing dangling publishing-info.json artifacts..."
Expand Down
35 changes: 35 additions & 0 deletions vhdbuilder/automation/cmd/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package main

import (
"context"
"log"

"github.com/Azure/agentbaker/vhdbuilder/automation/internal/ado"
"github.com/Azure/agentbaker/vhdbuilder/automation/internal/env"
)

func main() {
if env.Variables.ADOPAT == "" {
log.Fatal("expected to find non-empty ADO_PAT from environment")
}
if env.Variables.VHDBuildID == "" {
log.Fatal("expected to find non-empty VHD_BUILD_ID from environment")
}

ctx := context.Background()
adoClient, err := ado.NewClient(ctx, env.Variables.ADOPAT)
if err != nil {
log.Fatalf("constructing ADO client: %s", err)
}

log.Println("building EV2 artifacts...")
build, err := adoClient.BuildEV2Artifacts(ctx, env.Variables.VHDBuildID, nil)
if err != nil {
log.Fatalf("building EV2 artifacts: %s", err)
}

log.Println("creating SIG release...")
if err := adoClient.CreateSIGRelease(ctx, build); err != nil {
log.Fatalf("creating SIG release: %s", err)
}
}
17 changes: 17 additions & 0 deletions vhdbuilder/automation/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
module github.com/Azure/agentbaker/vhdbuilder/automation

go 1.22.3

require (
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0
github.com/caarlos0/env/v11 v11.2.2
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0
github.com/stretchr/testify v1.9.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/google/uuid v1.1.1 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
18 changes: 18 additions & 0 deletions vhdbuilder/automation/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0 h1:JZg6HRh6W6U4OLl6lk7BZ7BLisIzM9dG1R50zUk9C/M=
github.com/Azure/azure-sdk-for-go/sdk/azcore v1.16.0/go.mod h1:YL1xnZ6QejvQHWJrX/AvhFl4WW4rqHVoKspWNVwFk0M=
github.com/caarlos0/env/v11 v11.2.2 h1:95fApNrUyueipoZN/EhA8mMxiNxrBwDa+oAZrMWl3Kg=
github.com/caarlos0/env/v11 v11.2.2/go.mod h1:JBfcdeQiBoI3Zh1QRAWfe+tpiNTmDtcCj/hHHHMx0vc=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/google/uuid v1.1.1 h1:Gkbcsh/GbpXz7lPftLA3P6TYMwjCLYm83jiFQZF/3gY=
github.com/google/uuid v1.1.1/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0 h1:mmJCWLe63QvybxhW1iBmQWEaCKdc4SKgALfTNZ+OphU=
github.com/microsoft/azure-devops-go-api/azuredevops/v7 v7.1.0/go.mod h1:mDunUZ1IUJdJIRHvFb+LPBUtxe3AYB5MI6BMXNg8194=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
170 changes: 170 additions & 0 deletions vhdbuilder/automation/internal/ado/ado.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,170 @@
package ado

import (
"context"
"fmt"
"log"
"time"

"github.com/Azure/azure-sdk-for-go/sdk/azcore/to"
"github.com/microsoft/azure-devops-go-api/azuredevops/v7"
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/pipelines"
"github.com/microsoft/azure-devops-go-api/azuredevops/v7/release"
)

const (
sigReleaseArtifactBuildPipelineID = 319030
sigReleaseDefinitionID = 494
)

const (
projectName = "CloudNativeCompute"
)

const (
sigReleaseArtifactsAlias = "ev2_artifacts"
)

type Client struct {
pipelines pipelines.Client
releases release.Client
}

func NewClient(ctx context.Context, pat string) (*Client, error) {
conn := azuredevops.NewPatConnection("https://dev.azure.com/msazure", pat)
pipelinesClient := pipelines.NewClient(ctx, conn)
releaseClient, err := release.NewClient(ctx, conn)
if err != nil {
return nil, fmt.Errorf("constructing new ADO release client: %w", err)
}
return &Client{
pipelines: pipelinesClient,
releases: releaseClient,
}, nil
}

type PollingConfig struct {
BuildCompletionPollInterval *time.Duration
}

func (c *PollingConfig) getBuildCompletionPollInterval() time.Duration {
if c == nil || c.BuildCompletionPollInterval == nil {
return 30 * time.Second
}
return *c.BuildCompletionPollInterval
}

func (c *Client) BuildEV2Artifacts(ctx context.Context, vhdBuildID string, pollConfig *PollingConfig) (*ArtifactBuild, error) {
run, err := c.pipelines.RunPipeline(ctx, pipelines.RunPipelineArgs{
Project: to.Ptr(projectName),
PipelineId: to.Ptr(sigReleaseArtifactBuildPipelineID),
RunParameters: &pipelines.RunPipelineParameters{
Variables: to.Ptr(getArtifactBuildVariables(vhdBuildID)),
},
})
if err != nil {
return nil, fmt.Errorf("running EV2 artifact build pipeline: %w", err)
}
if isTerminal(*run.State) {
return nil, fmt.Errorf("new pipeline run %d has unexpected state: %s", *run.Id, *run.State)
}

build := &ArtifactBuild{
ID: *run.Id,
Name: *run.Name,
}
log.Printf("EV2 artifact build started; ID: %d, URL: %s", build.ID, build.URL())
log.Printf("will poll build status every %s", pollConfig.getBuildCompletionPollInterval())

ticker := time.NewTicker(pollConfig.getBuildCompletionPollInterval())
for !isTerminal(*run.State) {
select {
case <-ticker.C:
run, err = c.pipelines.GetRun(ctx, pipelines.GetRunArgs{
Project: to.Ptr(projectName),
PipelineId: to.Ptr(sigReleaseArtifactBuildPipelineID),
RunId: &build.ID,
})
if err != nil {
return nil, fmt.Errorf("getting EV2 artifact build pipeline run: %w", err)
}
log.Printf("EV2 artifact build %d is in state %q", build.ID, *run.State)
case <-ctx.Done():
return nil, fmt.Errorf("waiting for EV2 artifact build to finish: %w", ctx.Err())
}
}

log.Printf("SIG release EV2 artifact build %d finished with result: %s", build.ID, *run.State)
return build, nil
}

func (c *Client) CreateSIGRelease(ctx context.Context, source *ArtifactBuild) error {
sigRelease, err := c.releases.CreateRelease(ctx, release.CreateReleaseArgs{
Project: to.Ptr(projectName),
ReleaseStartMetadata: &release.ReleaseStartMetadata{
DefinitionId: to.Ptr(sigReleaseDefinitionID),
Artifacts: &[]release.ArtifactMetadata{
{
Alias: to.Ptr(sigReleaseArtifactsAlias),
InstanceReference: &release.BuildVersion{
Id: to.Ptr(fmt.Sprintf("%d", source.ID)),
Name: to.Ptr(source.Name),
},
},
},
},
})
if err != nil {
return fmt.Errorf("creating SIG release: %w", err)
}
releaseURL, err := extractReleaseURL(sigRelease)
if err != nil {
return fmt.Errorf("extracting release URL: %w", err)
}
log.Printf("created SIG release: %s", releaseURL)
return nil
}

type ArtifactBuild struct {
ID int
Name string
}

func (b *ArtifactBuild) URL() string {
if b.ID == 0 {
return ""
}
return fmt.Sprintf("https://msazure.visualstudio.com/%s/_build/results?buildId=%d&view=results", projectName, b.ID)
}

func getArtifactBuildVariables(vhdBuildID string) map[string]pipelines.Variable {
return map[string]pipelines.Variable{
"VHD_PIPELINE_RUN_ID": {
IsSecret: to.Ptr(false),
Value: to.Ptr(vhdBuildID),
},
}
}

func isTerminal(state pipelines.RunState) bool {
return state != pipelines.RunStateValues.InProgress && state != "NotStarted"
}

func extractReleaseURL(r *release.Release) (string, error) {
linkMap, ok := r.Links.(map[string]interface{})
if !ok {
return "", fmt.Errorf("unable to convert release links to map[string]interface{}")
}
webLinkMap, ok := linkMap["web"].(map[string]interface{})
if !ok {
return "", fmt.Errorf("unable to convert release web links to map[string]string")
}
if _, ok := webLinkMap["href"]; !ok {
return "", fmt.Errorf("failed to find href key in release web links")
}
releaseURL, ok := webLinkMap["href"].(string)
if !ok {
return "", fmt.Errorf("failed to convert release URL to string")
}
return releaseURL, nil
}
Loading
Loading