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

Implement auto versioning and add commit-msg hook #268

Merged
merged 4 commits into from
Oct 18, 2022
Merged
Show file tree
Hide file tree
Changes from 3 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
81 changes: 81 additions & 0 deletions .githooks/commit-msg
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
#!/bin/bash

# set -xe

script_name=`basename "$0"`

# Make sure stdin is open--it's sometimes open when we come in here
exec 0< /dev/tty

SCRIPT_DIR=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )

# Get user's original commit text.
orig_msg=$(awk '!/^#/{print}' $1);

# Test for commit type/breaking change info already in commit text.
# This can occur if the user entered command using "-m" and the
# pre-commmit hook already handled getting this info.

rgx="^(feat:|fix:|test:|doc:|Merge).*"
if grep -E $rgx <<< "$orig_msg" > /dev/null 2>&1; then
echo "$script_name: Commit message passes commmit-type check."
echo "--------------------------------------------------------------------------------"
echo "$orig_msg"
echo "--------------------------------------------------------------------------------"
exit 0
fi

echo
echo 'Please select the type of your commmit from the following list:'
echo
nl $SCRIPT_DIR/commit_types.txt | awk '{ print $1, $2 }'
echo
count=$(wc -l $SCRIPT_DIR/commit_types.txt | awk '{ print $1 }')
n=0
while true; do
read -p 'Select option: ' n
# If $n is an integer between one and $count...
if [[ "$n" -eq $n ]] &&
[[ "$n" -gt 0 ]] &&
[[ "$n" -le "$count" ]]; then
break
fi
done

commit_type="$(sed -n "${n}p" $SCRIPT_DIR/commit_types.txt | awk '{ print $2 }')"
echo
yesno="n"
if [ $commit_type == "feat" ] || [ $commit_type == "fix" ]; then
while true; do
read -p 'Is this a breaking change [y/n]: ' yn
yesno="$(tr [A-Z] [a-z] <<< "$yn")"
if [ "$yesno" == "y" ] || [ "$yesno" == "yes" ] || \
[ "$yesno" == "n" ] || [ "$yesno" == "no" ]; then
break
fi
done
fi

breaking_commit=0
if [ "$yesno" == "y" ] || [ "$yesno" == "yes" ]; then
breaking_commit=1
fi

NL=$'\n'
if [ "$breaking_commit" -eq 1 ]; then
# Explicitly state it's a breaking change
msg="$msg${NL}${NL}BREAKING CHANGE"
fi

# Prepend commit type to original commit text.
new_msg="${commit_type}: $orig_msg";
echo $new_msg > $1;

echo
echo "Updated message:"
echo
echo "--------------------------------------------------------------------------------"
echo "$new_msg"
echo "--------------------------------------------------------------------------------"
echo
exit 0
4 changes: 4 additions & 0 deletions .githooks/commit_types.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
feature feat
fix fix
test test
documentation doc
49 changes: 19 additions & 30 deletions Jenkinsfile
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
library 'pipeline-utils@master'

CCV = ""

pipeline {
agent {
kubernetes {
Expand Down Expand Up @@ -56,30 +58,33 @@ spec:
GITHUB_CREDS = credentials("${env.GITHUB_CREDS_ID_STR}")
REGISTRY = "${env.REGISTRY}"
REG_OWNER="helxplatform"
REG_APP="dug"
REPO_NAME="dug"
COMMIT_HASH="${sh(script:"git rev-parse --short HEAD", returnStdout: true).trim()}"
VERSION_FILE="src/dug/_version.py"
VERSION="${sh(script:'awk \'{ print $3 }\' src/dug/_version.py | xargs', returnStdout: true).trim()}"
IMAGE_NAME="${REGISTRY}/${REG_OWNER}/${REG_APP}"
TAG1="$BRANCH_NAME"
TAG2="$COMMIT_HASH"
TAG3="$VERSION"
TAG4="latest"
IMAGE_NAME="${REGISTRY}/${REG_OWNER}/${REPO_NAME}"
}
stages {
stage('Build') {
steps {
script {
container(name: 'go', shell: '/bin/bash') {
if (BRANCH_NAME.equals("master")) {
CCV = go.ccv()
}
}
container(name: 'kaniko', shell: '/busybox/sh') {
kaniko.build("./Dockerfile", ["$IMAGE_NAME:$TAG1", "$IMAGE_NAME:$TAG2", "$IMAGE_NAME:$TAG3", "$IMAGE_NAME:$TAG4"])
def tagsToPush = ["$IMAGE_NAME:$BRANCH_NAME", "$IMAGE_NAME:$COMMIT_HASH"]
if (CCV != null && !CCV.trim().isEmpty() && BRANCH_NAME.equals("master")) {
tagsToPush.add("$IMAGE_NAME:$CCV")
tagsToPush.add("$IMAGE_NAME:latest")
} else if (BRANCH_NAME.equals("develop")) {
def now = new Date()
def currTimestamp = now.format("yyyy-MM-dd'T'HH.mm'Z'", TimeZone.getTimeZone('UTC'))
tagsToPush.add("$IMAGE_NAME:$currTimestamp")
}
kaniko.buildAndPush("./Dockerfile", tagsToPush)
}
}
}
post {
always {
archiveArtifacts artifacts: 'image.tar', onlyIfSuccessful: true
}
}
}
stage('Test') {
steps {
Expand All @@ -88,21 +93,5 @@ spec:
'''
}
}
stage('Publish') {
steps {
script {
container(name: 'crane', shell: '/busybox/sh') {
def imageTagsPushAlways = ["$IMAGE_NAME:$TAG1", "$IMAGE_NAME:$TAG2"]
def imageTagsPushForDevelopBranch = ["$IMAGE_NAME:$TAG3"]
def imageTagsPushForMasterBranch = ["$IMAGE_NAME:$TAG4"]
image.publish(
imageTagsPushAlways,
imageTagsPushForDevelopBranch,
imageTagsPushForMasterBranch
)
}
}
}
}
}
}
5 changes: 5 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ export PYTHONPATH = $(shell echo ${PWD})/src
help:
@grep -E '^#[a-zA-Z\.\-]+:.*$$' $(MAKEFILE_LIST) | tr -d '#' | awk 'BEGIN {FS = ": "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

init:
git --version
echo "Please make sure your git version is greater than 2.9.0. If it's not, this command will fail."
git config --local core.hooksPath .githooks/

#clean: Remove old build artifacts and installed packages
clean:
rm -rf build
Expand Down