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

Automate version bumping #56

Merged
merged 3 commits into from
Oct 13, 2023
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 24 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -26,25 +26,26 @@ OVERRIDE_IMAGE_NAME?=$(ADAPTER_TEST_IMAGE)

LDFLAGS=-w -X main.version=$(VERSION) -X main.commit=$(GIT_COMMIT)

.PHONY: all test verify-gofmt gofmt verify
.PHONY: all

all: build

.PHONY: fmt
fmt:
find . -type f -name "*.go" | grep -v "./vendor*" | xargs gofmt -s -w

.PHONY: build
build:
CGO_ENABLED=0 GOARCH=$(GOARCH) go build -ldflags "$(LDFLAGS)" -a -tags netgo -o build/$(GOOS)/$(GOARCH)/$(BINARY_NAME) ./cmd/wavefront-adapter/


.PHONY: test
test:
CGO_ENABLED=0 go test ./pkg/...

.PHONY: lint
lint:
go vet -composites=false ./...


BUILDER_SUFFIX=$(shell echo $(PREFIX) | cut -d '/' -f1)

.PHONY: publish
Expand All @@ -53,5 +54,24 @@ publish:
CGO_ENABLED=0 GOOS=linux GOARCH=arm64 make build -o fmt -o vet
docker buildx create --use --node wavefront_k8s_adapter_builder_$(BUILDER_SUFFIX)
docker buildx build --platform linux/amd64,linux/arm64 --push --pull -t $(DOCKER_REPO)/$(DOCKER_IMAGE):$(VERSION) -t $(DOCKER_REPO)/$(DOCKER_IMAGE):latest -f Dockerfile build

.PHONY: clean
clean:
rm -rf $(OUT_DIR)
rm -rf $(OUT_DIR)

# create a new bump version branch
.PHONY: branch
branch:
git stash
git checkout master
git pull
git checkout -b bump-version-$(VERSION)

.PHONY: update-version
update-version:
@if [ -z "$(NEW_VERSION)" ]; then echo "Need to set NEW_VERSION" && exit 1; fi
git add Makefile
git add release/VERSION
git add deploy/manifests/05-custom-metrics-apiserver-deployment.yaml
git commit -m "Bump version to $(NEW_VERSION)"
git push --set-upstream origin bump-version-$(NEW_VERSION)
59 changes: 59 additions & 0 deletions bump-version.Jenkinsfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
pipeline {
agent any

tools { go 'Go 1.18' }

environment {
PATH = "${env.WORKSPACE}/bin:${env.HOME}/go/bin:${env.PATH}"
GIT_CREDENTIAL_ID = 'wf-jenkins-github'
GITHUB_TOKEN = credentials('GITHUB_TOKEN')
REPO_NAME = 'wavefront-kubernetes-adapter'
}

parameters {
choice(name: 'BUMP_COMPONENT', choices: ['patch', 'minor', 'major'], description: 'Specify a semver component to bump.')
}

stages {
stage('Build and Run Tests') {
steps {
sh 'make fmt lint build test'
}
}

stage('Create Bump Version PR') {
environment {
BUMP_COMPONENT = "${params.BUMP_COMPONENT}"
}

steps {
sh 'git config --global user.email "svc.wf-jenkins@vmware.com"'
sh 'git config --global user.name "svc.wf-jenkins"'
sh 'git remote set-url origin https://${GITHUB_TOKEN}@github.com/wavefronthq/${REPO_NAME}.git'
sh 'CGO_ENABLED=0 go install github.com/davidrjonas/semver-cli@latest'
sh './scripts/update_release_version.sh -v $(cat release/VERSION) -s ${BUMP_COMPONENT}'
sh './scripts/create_bump_version_pr.sh -v $(cat release/VERSION) -t ${GITHUB_TOKEN}'
}
}
}

// Notify only on null->failure or success->failure or failure->success
post {
failure {
script {
if(currentBuild.previousBuild == null) {
slackSend (channel: '#tobs-k8po-team', color: '#FF0000', message: "Bump version failed: <${env.BUILD_URL}|${env.JOB_NAME} [${env.BUILD_NUMBER}]>")
}
}
}
regression {
slackSend (channel: '#tobs-k8po-team', color: '#FF0000', message: "Bump version failed: <${env.BUILD_URL}|${env.JOB_NAME} [${env.BUILD_NUMBER}]>")
}
fixed {
slackSend (channel: '#tobs-k8po-team', color: '#008000', message: "Bump version fixed: <${env.BUILD_URL}|${env.JOB_NAME} [${env.BUILD_NUMBER}]>")
}
always {
cleanWs()
}
}
}
1 change: 1 addition & 0 deletions release/VERSION
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.9.12
62 changes: 62 additions & 0 deletions scripts/create_bump_version_pr.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
#!/usr/bin/env bash
set -eou pipefail

REPO_ROOT="$(git rev-parse --show-toplevel)"

function create_pull_request() {
local version="$1"
local token="$2"
local branch_name="$3"

curl -fsSL -X 'POST' \
-H 'Accept: application/vnd.github+json' \
-H "Authorization: Bearer ${token}" \
-H 'X-GitHub-Api-Version: 2022-11-28' \
-d "{\"head\":\"${branch_name}\",\"base\":\"master\",\"title\":\"Bump version to ${version}\"}" \
https://api.github.com/repos/wavefrontHQ/wavefront-kubernetes-adapter/pulls
}

function check_required_argument() {
local required_arg="$1"
local failure_msg="$2"

if [[ -z "${required_arg}" ]]; then
print_usage_and_exit "${failure_msg}"
fi
}

function print_usage_and_exit() {
echo "Failure: $1"
echo "Usage: $0 [flags] [options]"
echo -e "\t-v bump version (required)"
echo -e "\t-t github token (required)"
exit 1
}

function main() {
cd "${REPO_ROOT}"

local VERSION=''
local GITHUB_TOKEN=''

while getopts ":v:t:" opt; do
case $opt in
v) VERSION="$OPTARG";;
t) GITHUB_TOKEN="$OPTARG";;
\?) print_usage_and_exit "Invalid option: -$OPTARG";;
esac
done

check_required_argument "${VERSION}" "-v <VERSION> is required"
check_required_argument "${GITHUB_TOKEN}" "-t <GITHUB_TOKEN> is required"

local GIT_BUMP_BRANCH_NAME="bump-version-${VERSION}"
git branch -D "$GIT_BUMP_BRANCH_NAME" &>/dev/null || true
git checkout -b "$GIT_BUMP_BRANCH_NAME"

make update-version NEW_VERSION="${VERSION}"

create_pull_request "${VERSION}" "${GITHUB_TOKEN}" "${GIT_BUMP_BRANCH_NAME}"
}

main "$@"
68 changes: 68 additions & 0 deletions scripts/update_release_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/usr/bin/env bash
set -e

REPO_ROOT=$(git rev-parse --show-toplevel)

function get_bumped_version() {
local version="$1"
local bump_component="$2"

semver-cli inc "${bump_component}" "${version}"
}

function check_required_argument() {
local required_arg="$1"
local failure_msg="$2"

if [[ -z "${required_arg}" ]]; then
print_usage_and_exit "${failure_msg}"
fi
}

function print_usage_and_exit() {
echo "Failure: $1"
echo "Usage: $0 [flags] [options]"
echo -e "\t-v version to bump (required)"
echo -e "\t-s semver component to bump (required, ex: major, minor, patch)"
exit 1
}

function main() {
cd "${REPO_ROOT}"

local VERSION=''
local BUMP_COMPONENT=''
local NEXT_RELEASE_VERSION=''
local FUTURE_RELEASE_VERSION=''

while getopts ":v:s:" opt; do
case $opt in
v) VERSION="$OPTARG";;
s) BUMP_COMPONENT="$OPTARG";;
\?) print_usage_and_exit "Invalid option: -$OPTARG";;
esac
done

check_required_argument "${VERSION}" "-v <VERSION> is required"
check_required_argument "${BUMP_COMPONENT}" "-s <BUMP_COMPONENT> is required"

echo "Current release version: ${VERSION}"

NEXT_RELEASE_VERSION="$(get_bumped_version "${VERSION}" "${BUMP_COMPONENT}")"
echo "Next release version: ${NEXT_RELEASE_VERSION}"

# update the version in the image tag
sed -i.bak "s/wavefront-hpa-adapter:${VERSION}/wavefront-hpa-adapter:${NEXT_RELEASE_VERSION}/g" "${REPO_ROOT}"/deploy/manifests/05-custom-metrics-apiserver-deployment.yaml
rm -f "${REPO_ROOT}"/deploy/manifests/05-custom-metrics-apiserver-deployment.yaml.bak

echo "${NEXT_RELEASE_VERSION}" >"${REPO_ROOT}"/release/VERSION

FUTURE_RELEASE_VERSION="$(get_bumped_version "${NEXT_RELEASE_VERSION}" "patch")"
echo "Future release version: ${FUTURE_RELEASE_VERSION}"

# update the version in the Makefile
sed -i.bak "s/VERSION?=[0-9.]*/VERSION?=${FUTURE_RELEASE_VERSION}/g" "${REPO_ROOT}"/Makefile
rm -f "${REPO_ROOT}"/Makefile.bak
}

main "$@"