Skip to content

Commit

Permalink
Update version automatically (#31)
Browse files Browse the repository at this point in the history
Update version automatically and track it through tags.
  • Loading branch information
danielmundi authored Feb 17, 2022
2 parents 77c9fb6 + c3055fd commit 09509a4
Show file tree
Hide file tree
Showing 5 changed files with 119 additions and 16 deletions.
32 changes: 20 additions & 12 deletions .github/workflows/build-image.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,18 @@ name: Build image
on:
# Allow manual runs of workflow from Actions tab
workflow_dispatch:
inputs:
version-bump:
description: Version bump
required: true
type: choice
options:
- Auto
- Major
- Minor
- Patch
- RC
default: Auto

schedule:
- cron: '37 5 * * *'
Expand All @@ -18,8 +30,11 @@ jobs:
steps:
- name: Checkout
uses: actions/checkout@v2
with:
fetch-depth: '0'

- name: Build image
id: build-image
run: |
last_commit_date=$(git log -1 --format=%cd --date=unix)
last_day=$(date -d '-1 day' +%s)
Expand All @@ -32,44 +47,37 @@ jobs:
fi
sudo docker run --rm --privileged multiarch/qemu-user-static --reset -p yes
sudo ./build-docker.sh
- name: Get current date
if: always()
id: date
run: echo "::set-output name=date::$(date +'%Y-%m-%d')"
sudo ./build-docker.sh -v ${{ github.event.input.version-bump }}
- name: Upload image
uses: actions/upload-artifact@v2
with:
name: wlanpi-nightly-release-${{ steps.date.outputs.date }}
name: wlanpi-nightly-release-${{ steps.build-image.outputs.version }}
path: deploy/*.zip

- name: Upload image info
uses: actions/upload-artifact@v2
with:
name: wlanpi-nightly-release-${{ steps.date.outputs.date }}-info
name: wlanpi-nightly-release-${{ steps.build-image.outputs.version }}-info
path: deploy/*.info

- name: Create Pre-Release
if: ${{ github.event.ref == format('refs/heads/{0}', github.event.repository.default_branch) }}
uses: "marvinpinto/action-automatic-releases@latest"
with:
repo_token: "${{ secrets.GITHUB_TOKEN }}"
automatic_release_tag: "latest"
automatic_release_tag: ${{ steps.build-image.outputs.version }}
prerelease: true
title: "Development Build"
files: |
deploy/*.zip
deploy/*.info
deploy/wlanpi-os/os.json
deploy/wlanpi-os/os-list.json
- name: Upload logs
if: always()
uses: actions/upload-artifact@v2
with:
name: wlanpi-nightly-release-${{ steps.date.outputs.date }}-buildlogs
name: wlanpi-nightly-release-${{ steps.build-image.outputs.version }}-buildlogs
path: deploy/*.log

slack-workflow-status:
Expand Down
11 changes: 10 additions & 1 deletion build-docker.sh
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ if [ -f "${DIR}/config" ]; then
CONFIG_FILE="${DIR}/config"
fi

while getopts "c:" flag
while getopts "c:v:" flag
do
case "${flag}" in
c)
Expand Down Expand Up @@ -58,6 +58,9 @@ fi

# Ensure the Git Hash is recorded before entering the docker container
GIT_HASH=${GIT_HASH:-"$(git rev-parse HEAD)"}
LAST_VERSION="$(git describe --tags --abbrev=0 --match="v[0-9].[0-9].[0-9]*")"
LAST_VERSION_HASH="$(git rev-parse "${LAST_VERSION}")"
COMMITS_FROM_LAST="$(git log --oneline "${LAST_VERSION}"..${GIT_HASH})"

CONTAINER_EXISTS=$(${DOCKER} ps -a --filter name="${CONTAINER_NAME}" -q)
CONTAINER_RUNNING=$(${DOCKER} ps --filter name="${CONTAINER_NAME}" -q)
Expand Down Expand Up @@ -95,6 +98,9 @@ if [ "${CONTAINER_EXISTS}" != "" ]; then
${PIGEN_DOCKER_OPTS} \
--volume "${CONFIG_FILE}":/config:ro \
-e "GIT_HASH=${GIT_HASH}" \
-e "LAST_VERSION=${LAST_VERSION}" \
-e "LAST_VERSION_HASH=${LAST_VERSION_HASH}" \
-e "COMMITS_FROM_LAST=${COMMITS_FROM_LAST}" \
--volumes-from="${CONTAINER_NAME}" --name "${CONTAINER_NAME}_cont" \
pi-gen \
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
Expand All @@ -113,6 +119,9 @@ else
${PIGEN_DOCKER_OPTS} \
--volume "${CONFIG_FILE}":/config:ro \
-e "GIT_HASH=${GIT_HASH}" \
-e "LAST_VERSION=${LAST_VERSION}" \
-e "LAST_VERSION_HASH=${LAST_VERSION_HASH}" \
-e "COMMITS_FROM_LAST=${COMMITS_FROM_LAST}" \
pi-gen \
bash -e -o pipefail -c "dpkg-reconfigure qemu-user-static &&
# binfmt_misc is sometimes not mounted with debian bullseye image
Expand Down
10 changes: 9 additions & 1 deletion build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -170,14 +170,17 @@ if [ -f config ]; then
source config
fi

while getopts "c:" flag
while getopts "c:v:" flag
do
case "$flag" in
c)
EXTRA_CONFIG="$OPTARG"
# shellcheck disable=SC1090
source "$EXTRA_CONFIG"
;;
v)
VERSION_BUMP="${OPTARG}"
;;
*)
;;
esac
Expand Down Expand Up @@ -231,6 +234,11 @@ export TIMEZONE_DEFAULT="${TIMEZONE_DEFAULT:-Europe/London}"

export GIT_HASH=${GIT_HASH:-"$(git rev-parse HEAD)"}

export VERSION_BUMP=${VERSION_BUMP:-auto}
export LAST_VERSION=${LAST_VERSION:-"$(git describe --tags --abbrev=0 --match="v[0-9].[0-9].[0-9]*")"}
export LAST_VERSION_HASH=${LAST_VERSION_HASH:-"$(git rev-parse "${LAST_VERSION}")"}
export COMMITS_FROM_LAST=${COMMITS_FROM_LAST:-"$(git log --oneline "${LAST_VERSION}"..${GIT_HASH})"}

export PUBKEY_SSH_FIRST_USER

export CLEAN
Expand Down
14 changes: 12 additions & 2 deletions export-image/04-finalise/01-run.sh
Original file line number Diff line number Diff line change
Expand Up @@ -69,14 +69,24 @@ cp "$ROOTFS_DIR/etc/rpi-issue" "$INFO_FILE"
#kernel="$(curl -s -L "https://github.com/raspberrypi/firmware/raw/$firmware/extra/git_hash")"
#printf "Kernel: https://github.com/raspberrypi/linux/tree/%s\n" "$kernel"

uname="$(curl -s -L "https://github.com/raspberrypi/firmware/raw/$firmware/extra/uname_string7")"
printf "Uname string: %s\n" "$uname"
#uname="$(curl -s -L "https://github.com/raspberrypi/firmware/raw/$firmware/extra/uname_string7")"
fi

if [ -f "$ROOTFS_DIR/usr/share/doc/wlanpi-kernel/changelog.Debian.gz" ]; then
kernel=$(zgrep "Kernel version" \
"$ROOTFS_DIR/usr/share/doc/wlanpi-kernel/changelog.Debian.gz" | \
head -n1 | sed -n 's|.* \([^ ]*\)$|\1|p')
printf "Kernel: %s\n" "$uname"
fi

printf "\nPackages:\n"
dpkg -l --root "$ROOTFS_DIR"
} >> "$INFO_FILE"

new_version=$(source "${SCRIPT_DIR}/update_version.sh" "${VERSION_BUMP}")
echo "VERSION=${new_version}" > "${ROOTFS_DIR}/etc/wlanpi-release"
echo "::set-output name=version::${new_version}"

mkdir -p "${DEPLOY_DIR}"

rm -f "${DEPLOY_DIR}/${ZIP_FILENAME}${IMG_SUFFIX}.zip"
Expand Down
68 changes: 68 additions & 0 deletions scripts/update_version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash -e

REQUEST_BUMP="$1"

setup_version() {
if [ -f "/tmp/semver" ]; then
return 0
fi

wget -q -O /tmp/semver \
https://raw.githubusercontent.com/fsaintjacques/semver-tool/master/src/semver
chmod +x /tmp/semver
}

# Print version with "v" prefix
get_latest_tag() {
tag="$(git describe --tags --abbrev=0 --match="v[0-9].[0-9].[0-9]*")"
echo "${tag}"
}

update_version() {
setup_version

last_version="${LAST_VERSION}"
if [ "${GIT_HASH}" == "${LAST_VERSION_HASH}" ]; then
echo "${last_version}"
exit 0
fi

REQUEST_BUMP="$(echo "${REQUEST_BUMP}" | tr '[:upper:]' '[:lower:]')"

case "${REQUEST_BUMP}" in
break|major) is_breaking=1 ;;
feat|minor) is_feature=1 ;;
rc) is_rc=1 ;;
patch) is_patch=1 ;;
*)
# Guess the bump from commit history
commits="${COMMITS_FROM_LAST}"

is_breaking="$(echo "${commits}" | awk '{ print $2; }' | { grep "BREAK:" || :; })"
is_feature="$(echo "${commits}" | awk '{ print $2; }' | { grep "feat:" || :; })"
is_rc="$(echo "${last_version}" | { grep -- "-rc" || :; })"
;;
esac

if [ -n "${is_breaking}" ]; then
ver_bump="major"
elif [ -n "${is_feature}" ]; then
ver_bump="minor"
elif [ -n "${is_rc}" ]; then
ver_bump="prerel rc."
else
ver_bump="patch"
fi

# Update version according to commit history
new_version=$(/tmp/semver bump ${ver_bump} "${last_version}")

# Add -rc to the new version code
if [ -n "${is_rc}" ] && [ "${ver_bump}" != "prerel rc." ]; then
new_version=$(/tmp/semver bump ${ver_bump} "${new_version}")
fi

echo "v${new_version}"
}

update_version

0 comments on commit 09509a4

Please sign in to comment.