Skip to content

Commit 347a720

Browse files
authored
[Independent Agent Release] New pipeline upload step for agent package for Independent Agent Release (#4222)
* Add alternate upload for independent agent pipeline * use test env var for testing * try split version * fixing up bugs * fix upload command * replace + with _ * PR comments to use version parser * use dash instead of underscore * Use proper buildkite env var * Removing unneeded log message * Add comment for tag replace statement * remove extraneous log * Add RM list only, save commit hash * fix typo * temp set 8.12 * Add step key to hook * temp set to main * try dry run instead * add proper dry run param * file listing * Add in workaround for ironbank * debug ls commands * Replace + in tag with . instead of - * Cleaning up * temp change for testing * revert temp test code * adding test var back * alias pkg/errors as travis_errors * Renaming to pkg_errors * Adding detailed comment about saving build ID in metadata * Replace with fmt.Errorf * Define artifact_paths instead of CLI * fix error linting * better error handling * Cleaning up script * fix proper artifact paths * Removing test branch assignment
1 parent 5d862e2 commit 347a720

File tree

5 files changed

+123
-15
lines changed

5 files changed

+123
-15
lines changed

.buildkite/hooks/pre-command

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ if [[ "$BUILDKITE_PIPELINE_SLUG" == "elastic-agent-package" ]]; then
4141
docker_login
4242
fi
4343

44-
if [[ "$BUILDKITE_STEP_KEY" == "dra-publish" ]]; then
44+
if [[ "$BUILDKITE_STEP_KEY" == "dra-publish" || "$BUILDKITE_STEP_KEY" == "bk-api-publish-independent-agent" ]]; then
4545
release_manager_login
4646
fi
4747
fi

.buildkite/pipeline.elastic-agent-package.yml

+45
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@ steps:
101101
- "build/distributions/**/*"
102102

103103
- label: ":elastic-stack: Publishing to DRA"
104+
if: build.env("BUILDKITE_TRIGGERED_FROM_BUILD_PIPELINE_SLUG") == null || build.env("BUILDKITE_TRIGGERED_FROM_BUILD_PIPELINE_SLUG") != "independent-agent-release-staging"
104105
key: dra-publish
105106
depends_on: package
106107
agents:
@@ -126,3 +127,47 @@ steps:
126127
export DRA_VERSION
127128
fi
128129
.buildkite/scripts/steps/dra-publish.sh
130+
131+
- label: "Publishing via BK API for Independent Agent Release"
132+
if: build.env("BUILDKITE_TRIGGERED_FROM_BUILD_PIPELINE_SLUG") == "independent-agent-release-staging"
133+
key: bk-api-publish-independent-agent
134+
depends_on: package
135+
agents:
136+
provider: "gcp"
137+
env:
138+
DRA_PROJECT_ID: "elastic-agent-package"
139+
DRA_PROJECT_ARTIFACT_ID: "agent-package"
140+
artifact_paths:
141+
- "build/distributions/**/*"
142+
command: |
143+
echo "+++ Restoring Artifacts"
144+
buildkite-agent artifact download "build/**/*" .
145+
echo "+++ Changing permissions for the BK API commands"
146+
sudo chown -R :1000 build/distributions/
147+
echo "--- File listing"
148+
ls -alR build
149+
echo "--- Copy workaround for ironbank container filename"
150+
.buildkite/scripts/steps/ironbank-cp-workaround.sh
151+
echo "--- File listing after workaround"
152+
ls -alR build
153+
echo "+++ Checking artifact validity with release-manager collect dry run"
154+
DRA_DRY_RUN="--dry-run"
155+
export DRA_DRY_RUN
156+
.buildkite/scripts/steps/dra-publish.sh
157+
# Artifacts will be uploaded via the artifact_paths entry above
158+
echo "+++ Set job metadata if TRIGGER_JOB_ID is properly set"
159+
if [[ -z "${TRIGGER_JOB_ID}" ]]; then
160+
echo "TRIGGER_JOB_ID is not set, so not setting metadata"
161+
else
162+
# If a pipeline that triggered this build passes in a "TRIGGER_JOB_ID" env var, that
163+
# is an indicator that it will want us to set some metadata in that calling build
164+
# so that it can reference this specific build ID in order to easily download
165+
# artifacts saved off in this build.
166+
#
167+
# This is a much easier way to pull back artifacts from a triggered build than using
168+
# a Buildkite token that we then have to manage via vault, etc.
169+
# See: https://forum.buildkite.community/t/how-to-download-artifacts-back-from-triggered-pipeline/3480/2
170+
echo "Setting metadata for job that trigger this one"
171+
buildkite-agent meta-data set "triggered_build_id" "$BUILDKITE_BUILD_ID" --job $TRIGGER_JOB_ID
172+
buildkite-agent meta-data set "triggered_commit_hash" "$BUILDKITE_COMMIT" --job $TRIGGER_JOB_ID
173+
fi
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#!/usr/bin/env bash
2+
3+
set -euo pipefail
4+
5+
# This file is a temporary workaround for the Independent Agent Release
6+
# workflow. The ironbank docker image does not currently handle the
7+
# AGENT_PACKAGE_VERSION env var override. This renames that file
8+
# to use that new version.
9+
#
10+
# We will not (at first) be using the ironbank docker image in the
11+
# Independent Agent releases; however, we want to use the release-manager
12+
# container dry-run as a check that all the expected images exist.
13+
#
14+
# This workaround allows the check to proceed without erroring on the file
15+
# that we know won't be named correctly.
16+
#
17+
18+
PACKAGE_VERSION="${AGENT_PACKAGE_VERSION:=""}"
19+
20+
if [[ -z "${PACKAGE_VERSION}" ]]; then
21+
echo "AGENT_PACKAGE_VERSION is not set, exiting"
22+
exit 1
23+
fi
24+
25+
IRONBANK_DOCKER_BLOB_PREFIX="elastic-agent-ironbank"
26+
IRONBANK_DOCKER_BLOB_SUFFIX="docker-build-context.tar.gz"
27+
OUTPUT_DIRNAME="build/distributions"
28+
29+
echo "--- ls ${OUTPUT_DIRNAME}"
30+
ls -al "${OUTPUT_DIRNAME}"
31+
echo "--- ironbank expected path"
32+
echo "${OUTPUT_DIRNAME}"/"${IRONBANK_DOCKER_BLOB_PREFIX}"*"${IRONBANK_DOCKER_BLOB_SUFFIX}"
33+
ls -al "${OUTPUT_DIRNAME}"/"${IRONBANK_DOCKER_BLOB_PREFIX}"*"${IRONBANK_DOCKER_BLOB_SUFFIX}" || true
34+
35+
echo "--- looking for ironbank file to copy to new name"
36+
if ls "${OUTPUT_DIRNAME}"/"${IRONBANK_DOCKER_BLOB_PREFIX}"*"${IRONBANK_DOCKER_BLOB_SUFFIX}" 2>/dev/null; then
37+
# Found the ironbank file
38+
echo "Found the ironbank file"
39+
NEW_IRONBANK_NAME="elastic-agent-ironbank-${PACKAGE_VERSION}-docker-build-context.tar.gz"
40+
echo "Copying to new path: ${OUTPUT_DIRNAME}/${NEW_IRONBANK_NAME}"
41+
cp "${OUTPUT_DIRNAME}"/"${IRONBANK_DOCKER_BLOB_PREFIX}"*"${IRONBANK_DOCKER_BLOB_SUFFIX}" "${OUTPUT_DIRNAME}/${NEW_IRONBANK_NAME}"
42+
else
43+
echo "Error: could not find ironbank file"
44+
exit 1
45+
fi

dev-tools/mage/dockerbuilder.go

+21-12
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ package mage
77
import (
88
"bytes"
99
"compress/gzip"
10+
"errors"
1011
"fmt"
1112
"io"
1213
"os"
@@ -16,7 +17,6 @@ import (
1617
"time"
1718

1819
"github.com/magefile/mage/sh"
19-
"github.com/pkg/errors"
2020
)
2121

2222
type dockerBuilder struct {
@@ -46,15 +46,15 @@ func newDockerBuilder(spec PackageSpec) (*dockerBuilder, error) {
4646

4747
func (b *dockerBuilder) Build() error {
4848
if err := os.RemoveAll(b.buildDir); err != nil {
49-
return errors.Wrapf(err, "failed to clean existing build directory %s", b.buildDir)
49+
return fmt.Errorf("failed to clean existing build directory %s: %w", b.buildDir, err)
5050
}
5151

5252
if err := b.copyFiles(); err != nil {
5353
return err
5454
}
5555

5656
if err := b.prepareBuild(); err != nil {
57-
return errors.Wrap(err, "failed to prepare build")
57+
return fmt.Errorf("failed to prepare build: %w", err)
5858
}
5959

6060
tag, err := b.dockerBuild()
@@ -67,11 +67,11 @@ func (b *dockerBuilder) Build() error {
6767
tries--
6868
}
6969
if err != nil {
70-
return errors.Wrap(err, "failed to build docker")
70+
return fmt.Errorf("failed to build docker: %w", err)
7171
}
7272

7373
if err := b.dockerSave(tag); err != nil {
74-
return errors.Wrap(err, "failed to save docker as artifact")
74+
return fmt.Errorf("failed to save docker as artifact: %w", err)
7575
}
7676

7777
return nil
@@ -88,7 +88,7 @@ func (b *dockerBuilder) modulesDirs() []string {
8888
}
8989

9090
func (b *dockerBuilder) exposePorts() []string {
91-
if ports, _ := b.ExtraVars["expose_ports"]; ports != "" {
91+
if ports := b.ExtraVars["expose_ports"]; ports != "" {
9292
return strings.Split(ports, ",")
9393
}
9494
return nil
@@ -101,7 +101,7 @@ func (b *dockerBuilder) copyFiles() error {
101101
if f.SkipOnMissing && errors.Is(err, os.ErrNotExist) {
102102
continue
103103
}
104-
return errors.Wrapf(err, "failed to copy from %s to %s", f.Source, target)
104+
return fmt.Errorf("failed to copy from %s to %s: %w", f.Source, target, err)
105105
}
106106
}
107107
return nil
@@ -128,7 +128,7 @@ func (b *dockerBuilder) prepareBuild() error {
128128

129129
err = b.ExpandFile(path, target, data)
130130
if err != nil {
131-
return errors.Wrapf(err, "expanding template '%s' to '%s'", path, target)
131+
return fmt.Errorf("expanding template '%s' to '%s': %w", path, target, err)
132132
}
133133
}
134134
return nil
@@ -169,7 +169,7 @@ func (b *dockerBuilder) expandDockerfile(templatesDir string, data map[string]in
169169
path := filepath.Join(templatesDir, file.source)
170170
err := b.ExpandFile(path, target, data)
171171
if err != nil {
172-
return errors.Wrapf(err, "expanding template '%s' to '%s'", path, target)
172+
return fmt.Errorf("expanding template '%s' to '%s': %w", path, target, err)
173173
}
174174
}
175175

@@ -178,10 +178,14 @@ func (b *dockerBuilder) expandDockerfile(templatesDir string, data map[string]in
178178

179179
func (b *dockerBuilder) dockerBuild() (string, error) {
180180
tag := fmt.Sprintf("%s:%s", b.imageName, b.Version)
181+
// For Independent Agent releases, replace the "+" with a "." since the "+" character
182+
// currently isn't allowed in a tag in Docker
183+
// E.g., 8.13.0+build202402191057 -> 8.13.0.build202402191057
184+
tag = strings.Replace(tag, "+", ".", 1)
181185
if b.Snapshot {
182186
tag = tag + "-SNAPSHOT"
183187
}
184-
if repository, _ := b.ExtraVars["repository"]; repository != "" {
188+
if repository := b.ExtraVars["repository"]; repository != "" {
185189
tag = fmt.Sprintf("%s/%s", repository, tag)
186190
}
187191
return tag, sh.Run("docker", "build", "-t", tag, b.buildDir)
@@ -238,9 +242,14 @@ func (b *dockerBuilder) dockerSave(tag string) error {
238242

239243
if err = cmd.Wait(); err != nil {
240244
if errmsg := strings.TrimSpace(stderr.String()); errmsg != "" {
241-
err = errors.Wrap(errors.New(errmsg), err.Error())
245+
err = fmt.Errorf("%w: %s", errors.New(errmsg), err.Error())
242246
}
243247
return err
244248
}
245-
return errors.Wrap(CreateSHA512File(outputFile), "failed to create .sha512 file")
249+
250+
err = CreateSHA512File(outputFile)
251+
if err != nil {
252+
return fmt.Errorf("failed to create .sha512 file: %w", err)
253+
}
254+
return nil
246255
}

dev-tools/mage/manifest/manifest.go

+11-2
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"golang.org/x/sync/errgroup"
2020

2121
"github.com/elastic/elastic-agent/pkg/testing/tools"
22+
"github.com/elastic/elastic-agent/pkg/version"
2223
)
2324

2425
// A backoff schedule for when and how often to retry failed HTTP
@@ -61,7 +62,7 @@ func DownloadManifest(manifest string) (tools.Build, error) {
6162
}
6263
if mg.Verbose() {
6364
log.Printf(">>>> Downloaded manifest %s", manifest)
64-
log.Printf(">>>> Packaing version: %s, build_id: %s, manifest_version:%s", manifestResponse.Version, manifestResponse.BuildID, manifestResponse.ManifestVersion)
65+
log.Printf(">>>> Packaging version: %s, build_id: %s, manifest_version:%s", manifestResponse.Version, manifestResponse.BuildID, manifestResponse.ManifestVersion)
6566
}
6667
return manifestResponse, nil
6768
}
@@ -99,6 +100,14 @@ func DownloadComponentsFromManifest(manifest string, platforms []string, platfor
99100
}
100101
projects := manifestResponse.Projects
101102

103+
parsedManifestVersion, err := version.ParseVersion(manifestResponse.Version)
104+
if err != nil {
105+
return fmt.Errorf("failed to parse manifest version: [%s]", manifestResponse.Version)
106+
}
107+
108+
// For resolving manifest package name and version, just use the Major.Minor.Patch part of the version
109+
majorMinorPatchVersion := fmt.Sprintf("%d.%d.%d", parsedManifestVersion.Major(), parsedManifestVersion.Minor(), parsedManifestVersion.Patch())
110+
102111
errGrp, downloadsCtx := errgroup.WithContext(context.Background())
103112
for component, pkgs := range componentSpec {
104113
for _, platform := range platforms {
@@ -111,7 +120,7 @@ func DownloadComponentsFromManifest(manifest string, platforms []string, platfor
111120

112121
for _, pkg := range pkgs {
113122
reqPackage := platformPackages[platform]
114-
pkgURL := resolveManifestPackage(projects[component], pkg, reqPackage, manifestResponse.Version)
123+
pkgURL := resolveManifestPackage(projects[component], pkg, reqPackage, majorMinorPatchVersion)
115124
if pkgURL != nil {
116125
for _, p := range pkgURL {
117126
log.Printf(">>>>>>>>> Downloading [%s] [%s] ", pkg, p)

0 commit comments

Comments
 (0)