Skip to content

Commit

Permalink
Merge pull request #265 from grafana/set-build-version
Browse files Browse the repository at this point in the history
Set version from runtime build info
  • Loading branch information
pablochacin authored Jul 28, 2023
2 parents 6973276 + 9e1211b commit 1d5cf39
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 25 deletions.
16 changes: 11 additions & 5 deletions build-package.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#!/bin/bash
#!/bin/bash

ARCH=$(go env GOARCH)
BUILD="build"
Expand All @@ -7,7 +7,7 @@ DIST="dist"
PKG=""
NAME="xk6-disruptor"
OS=$(go env GOOS)
VERSION="latest"
VERSION=""

function usage() {
cat << EOF
Expand All @@ -33,7 +33,6 @@ options:
-v, --version: package version in semver formatf
-y, --binary: name of the binary (default is name-os-arch)
EOF
}

Expand All @@ -59,6 +58,13 @@ function build() {
binary="$NAME-$os-$arch"
fi

# set disruptor version to use for build
mod=$(go list -m)
replace="."
if [[ ! -z $version ]]; then
replace=${mod}@${version}
fi

#start sub shell to create its own environment
(
if [[ $os == "linux" ]]; then # disable cross-compiling for linux
Expand All @@ -67,8 +73,8 @@ function build() {

export GOARCH=$arch
export GOOS=$os
export XK6_BUILD_FLAGS='-ldflags "-w -s -X github.com/grafana/xk6-disruptor/pkg/internal/consts.Version='${version}'"'
xk6 build --with $(go list -m)=. --output $BUILD/$binary
export XK6_BUILD_FLAGS='-ldflags "-w -s'
xk6 build --with $mod=${replace} --output $BUILD/$binary
)
}

Expand Down
4 changes: 2 additions & 2 deletions pkg/disruptors/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import (
"sync"
"time"

"github.com/grafana/xk6-disruptor/pkg/internal/consts"
"github.com/grafana/xk6-disruptor/pkg/internal/version"

"github.com/grafana/xk6-disruptor/pkg/kubernetes/helpers"

Expand Down Expand Up @@ -44,7 +44,7 @@ func (c *agentController) InjectDisruptorAgent(ctx context.Context) error {
agentContainer := corev1.EphemeralContainer{
EphemeralContainerCommon: corev1.EphemeralContainerCommon{
Name: "xk6-agent",
Image: consts.AgentImage(),
Image: version.AgentImage(),
ImagePullPolicy: corev1.PullIfNotPresent,
SecurityContext: &corev1.SecurityContext{
Capabilities: &corev1.Capabilities{
Expand Down
18 changes: 0 additions & 18 deletions pkg/internal/consts/const.go

This file was deleted.

39 changes: 39 additions & 0 deletions pkg/internal/version/version.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// Package version provide information about the build version
package version

import (
"runtime/debug"
)

const xk6DisruptorPath = "github.com/grafana/xk6-disruptor"

// DisruptorVersion returns the version of the currently executed disruptor
func DisruptorVersion() string {
if bi, ok := debug.ReadBuildInfo(); ok {
for _, d := range bi.Deps {
if d.Path == xk6DisruptorPath {
if d.Replace != nil {
return d.Replace.Version
}
return d.Version
}
}
}

return ""
}

// AgentImage returns the name of the agent image that corresponds to
// this version of the extension.
func AgentImage() string {
tag := "latest"

// if a specific version of the disruptor was built, use it for agent's tag
// (go test sets version to "")
dv := DisruptorVersion()
if dv != "" && dv != "(devel)" {
tag = dv
}

return "ghcr.io/grafana/xk6-disruptor-agent:" + tag
}

0 comments on commit 1d5cf39

Please sign in to comment.