Skip to content

Commit

Permalink
Pre release feature branch workflow (#22)
Browse files Browse the repository at this point in the history
* Install tomlq if not found

* Add pre-release versioning for non-main branches

* build release if pre-release

* install tomlq in the workflow

* install tomlq in the workflow

* Use github_ref for branch name in workflow

* Use yq and log github_ref for branch name in workflow

* Use the correct tomlq args

* Use the correct env var for branch name

* Use the correct env var for branch name

* Remove release build constraint

* Fix release action constraint

* Fix release action constraint

* Check prerelease when deciding if new version

* use function keyword

* Replace any slashes in branch name with underscroe

* Remove latest-dev tag

* Build pre-release on main as well

* Reorder the prelease logic

* Reorder the prelease logic

* Add sane branch name keys

* Remove the unused docker base stage and reorder release

* Pin yq version in workflow
  • Loading branch information
danielcooperxyz authored Jan 18, 2022
1 parent 6c8f0f9 commit b274dee
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 160 deletions.
233 changes: 77 additions & 156 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@ on:
push:
branches:
- main
pull_request:
branches:
- main


permissions:
contents: write
Expand All @@ -18,6 +22,7 @@ jobs:
outputs:
is_new_version: ${{ steps.get_version.outputs.IS_NEW_VERSION }}
version: ${{ steps.get_version.outputs.VERSION }}
sane_branch_name_key: ${{ steps.get_version.outputs.SANE_BRANCH_NAME_KEY }}
build_date: ${{ steps.get_version.outputs.BUILD_DATE }}
is_prerelease: ${{ steps.get_version.outputs.IS_PRERELEASE }}
toolchain: ${{ steps.get_toolchain.outputs.TOOLCHAIN }}
Expand All @@ -27,7 +32,7 @@ jobs:
with:
fetch-depth: 0
- run: git fetch --tags
- run: pip3 install yq
- run: pip3 install yq==2.13.0
- name: Get Build Version
id: get_version
run: ./scripts/check-version.sh
Expand All @@ -37,164 +42,12 @@ jobs:
run: |
echo "##[set-output name=TOOLCHAIN;]$(cat rust-toolchain)"
# Docker build are always performed but are only version tagged on new version
build-docker-build-base:
name: Build Docker Build Base Image
runs-on: ubuntu-latest
needs: [get-version, build-release]
steps:
- uses: actions/checkout@master
- name: Setup QEMU
uses: docker/setup-qemu-action@v1
with:
platforms: linux/amd64
- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
with:
buildkitd-flags: --debug
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: ${{ runner.temp }}/docker-buildx-cache
key: ${{ runner.os }}-docker-buildx-${{ needs.get-version.outputs.toolchain }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-docker-buildx-${{ needs.get-version.outputs.toolchain }}
${{ runner.os }}-docker-buildx-
- name: Build Dependency layers
uses: docker/build-push-action@v2
with:
push: false
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=local,src=${{ runner.temp }}/docker-buildx-cache
cache-to: type=local,dest=${{ runner.temp }}/docker-buildx-cache-new,mode=max
context: .
build-args: |
RUST_TOOLCHAIN=${{ needs.get-version.outputs.toolchain }}
file: ./Dockerfile
platforms: linux/amd64
target: setup
- name: Move cache
env:
CACHE_NEW: ${{ runner.temp }}/docker-buildx-cache-new
CACHE: ${{ runner.temp }}/docker-buildx-cache
run: |
rm -rf $CACHE
mv $CACHE_NEW $CACHE
build-docker:
name: Build Docker
runs-on: ubuntu-latest
needs: [get-version, build-docker-build-base]
steps:
- uses: actions/checkout@master
- name: Setup QEMU
uses: docker/setup-qemu-action@v1
with:
platforms: linux/amd64
- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
with:
buildkitd-flags: --debug
- name: Cache Docker layers
uses: actions/cache@v2
with:
path: ${{ runner.temp }}/docker-buildx-cache
key: ${{ runner.os }}-docker-buildx--${{ needs.get-version.outputs.toolchain }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-docker-buildx--${{ needs.get-version.outputs.toolchain }}
${{ runner.os }}-docker-buildx-
- name: Cache Docker sccache
uses: actions/cache@v2
with:
# this is horrible as you can't set the path but it probably works see:
# https://github.com/moby/buildkit/issues/1474
# https://github.com/moby/buildkit/issues/1512
path: ${{ runner.temp }}/cache.db
key: ${{ runner.os }}-docker-sccache--${{ needs.get-version.outputs.toolchain }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-docker-sccache--${{ needs.get-version.outputs.toolchain }}
${{ runner.os }}-docker-sccache-
- name: Move cache in
env:
CACHE_SOURCE: ${{ runner.temp }}/cache.db
CACHE_DEST: /var/lib/docker/buildkit/cache.db
# need sudo access to move to /var/lib/docker/buildkit/cache.db which is hopefully not going to change 🤞
run: |
if [ -f "$CACHE_SOURCE" ]; then
sudo mv $CACHE_SOURCE $CACHE_DEST
sudo chmod 0777 $CACHE_DEST
fi
- name: Generate tags
id: generate-tags
env:
VERSION: ${{ needs.get-version.outputs.version }}
IS_NEW_VERSION: ${{ needs.get-version.outputs.is_new_version }}
IS_PRERELEASE: ${{ needs.get-version.outputs.is_prerelease }}
# if it's a new non prerelease version tag with hash, version latest-dev and latest
# if it's a new prerelease version tag with hash, version and latest-dev
# if it's a non new version tag with hash and latest-dev
run: |
if [ "$IS_NEW_VERSION" == "true" ]; then
echo "##[set-output name=VERSION_TAG;]ghcr.io/digicatapult/vitalam-node:$VERSION";
if [ "$IS_PRERELEASE" == "false" ]; then
echo "##[set-output name=LATEST_TAG;]ghcr.io/digicatapult/vitalam-node:latest";
else
echo "##[set-output name=LATEST_TAG;]";
fi;
else
echo "##[set-output name=VERSION_TAG;]";
echo "##[set-output name=LATEST_TAG;]";
fi;
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build image
uses: docker/build-push-action@v2
with:
load: true
builder: ${{ steps.buildx.outputs.name }}
cache-from: type=local,src=${{ runner.temp }}/docker-buildx-cache
cache-to: type=local,dest=${{ runner.temp }}/docker-buildx-cache-temp,mode=max
context: .
build-args: |
RUST_TOOLCHAIN=${{ needs.get-version.outputs.toolchain }}
file: ./Dockerfile
platforms: linux/amd64
tags: |
ghcr.io/digicatapult/vitalam-node:${{ github.sha }}
ghcr.io/digicatapult/vitalam-node:latest-dev
${{ steps.generate-tags.outputs.VERSION_TAG }}
${{ steps.generate-tags.outputs.LATEST_TAG }}
labels: |
org.opencontainers.image.title=vitalam-node
org.opencontainers.image.description=${{ github.event.repository.description }}
org.opencontainers.image.source=${{ github.event.repository.html_url }}
org.opencontainers.image.url=${{ github.event.repository.html_url }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.version=${{ needs.get-version.outputs.version }}
org.opencontainers.image.created=${{ needs.get-version.outputs.build_date }}
- name: Publish image
run: docker push --all-tags ghcr.io/digicatapult/vitalam-node
# need sudo access to move from /var/lib/docker/buildkit/cache.db which is hopefully not going to change 🤞
- name: Move cache out
env:
CACHE_SOURCE: /var/lib/docker/buildkit/cache.db
CACHE_DEST: ${{ runner.temp }}/cache.db
run: |
sudo mv $CACHE_SOURCE $CACHE_DEST
sudo chmod 0777 $CACHE_DEST
# Github releases are only performed when the release version changes
build-release:
name: Build release
runs-on: ubuntu-latest
needs: [get-version]
if: ${{ needs.get-version.outputs.is_new_version == 'true' }}
if: ${{ needs.get-version.outputs.is_new_version == 'true' || needs.get-version.outputs.is_prerelease == 'true'}}

steps:
- uses: actions/checkout@master
Expand Down Expand Up @@ -234,9 +87,9 @@ jobs:
uses: actions/cache@v2
with:
path: ${{ runner.temp }}/cache
key: ${{ runner.os }}-cargo-build-cache-release-${{ needs.get-version.outputs.toolchain }}-${{ hashFiles('**/Cargo.lock') }}-${{ github.sha }}
key: ${{ runner.os }}-cargo-build-cache-release-${{ needs.get-version.outputs.toolchain }}-${{ needs.get-version.outputs.sane_branch_name_key }}-${{ github.sha }}
restore-keys: |
${{ runner.os }}-cargo-build-cache-release-${{ needs.get-version.outputs.toolchain }}-${{ hashFiles('**/Cargo.lock') }}
${{ runner.os }}-cargo-build-cache-release-${{ needs.get-version.outputs.toolchain }}-${{ needs.get-version.outputs.sane_branch_name_key }}
${{ runner.os }}-cargo-build-cache-release-${{ needs.get-version.outputs.toolchain }}-
${{ runner.os }}-cargo-build-cache-release-
- name: Cargo build
Expand Down Expand Up @@ -278,3 +131,71 @@ jobs:
prerelease: false
files: |
${{ runner.temp }}/artefacts/vitalam-node-${{ needs.get-version.outputs.version }}-x86_64-unknown-linux-gnu.tar.gz
# Docker build are always performed but are only version tagged on new version
build-docker:
name: Build Docker
runs-on: ubuntu-latest
needs: [get-version, build-release]
steps:
- uses: actions/checkout@master
- name: Setup QEMU
uses: docker/setup-qemu-action@v1
with:
platforms: linux/amd64
- name: Setup Docker Buildx
id: buildx
uses: docker/setup-buildx-action@v1
with:
buildkitd-flags: --debug
- name: Generate tags
id: generate-tags
env:
VERSION: ${{ needs.get-version.outputs.version }}
IS_NEW_VERSION: ${{ needs.get-version.outputs.is_new_version }}
IS_PRERELEASE: ${{ needs.get-version.outputs.is_prerelease }}
# if it's a new non prerelease version tag with hash, version latest-dev and latest
# if it's a new prerelease version tag with hash, version and latest-dev
# if it's a non new version tag with hash and latest-dev
run: |
if [ "$IS_NEW_VERSION" == "true" ]; then
echo "##[set-output name=VERSION_TAG;]ghcr.io/digicatapult/vitalam-node:$VERSION";
if [ "$IS_PRERELEASE" == "false" ]; then
echo "##[set-output name=LATEST_TAG;]ghcr.io/digicatapult/vitalam-node:latest";
else
echo "##[set-output name=LATEST_TAG;]";
fi;
else
echo "##[set-output name=VERSION_TAG;]";
echo "##[set-output name=LATEST_TAG;]";
fi;
- name: Login to GitHub Container Registry
uses: docker/login-action@v1
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Build image
uses: docker/build-push-action@v2
with:
load: true
builder: ${{ steps.buildx.outputs.name }}
context: .
build-args: |
RUST_TOOLCHAIN=${{ needs.get-version.outputs.toolchain }}
file: ./Dockerfile
platforms: linux/amd64
tags: |
ghcr.io/digicatapult/vitalam-node:${{ github.sha }}
${{ steps.generate-tags.outputs.VERSION_TAG }}
${{ steps.generate-tags.outputs.LATEST_TAG }}
labels: |
org.opencontainers.image.title=vitalam-node
org.opencontainers.image.description=${{ github.event.repository.description }}
org.opencontainers.image.source=${{ github.event.repository.html_url }}
org.opencontainers.image.url=${{ github.event.repository.html_url }}
org.opencontainers.image.revision=${{ github.sha }}
org.opencontainers.image.version=${{ needs.get-version.outputs.version }}
org.opencontainers.image.created=${{ needs.get-version.outputs.build_date }}
- name: Publish image
run: docker push --all-tags ghcr.io/digicatapult/vitalam-node
58 changes: 54 additions & 4 deletions scripts/check-version.sh
Original file line number Diff line number Diff line change
Expand Up @@ -23,16 +23,66 @@ function check_version_greater () {
fi
}

# Get published git tags that match semver regex with a "v" prefix then remove the "v" character
function assert_tomlq() {
printf "Checking for presense of tomlq..."
local path_to_executable=$(command -v tomlq)

if [ -z "$path_to_executable" ] ; then
echo -e "Cannot find tomlq executable. Is it on your \$PATH?"
exit 1
fi

if $path_to_executable --version | head -n 1 | grep -E "2.13.0" &> /dev/null ; then
printf "OK\n"
else
echo -e "Incorrect version of tomlq detected, make sure you are using tomlq from the yq package found on pip (i.e. pip3 install yq)"
$path_to_executable --version |head -n 1
exit 1
fi
}

function get_working_copy_version() {
assert_tomlq
CURRENT_VERSION=$(tomlq .package.version ./node/Cargo.toml | sed 's/"//g')

branch_name="$(git symbolic-ref HEAD 2>/dev/null)" ||
branch_name="(unnamed branch)" # detached HEAD (or possibly github workflow?)
branch_name=${branch_name##refs/heads/}

if [ "$GITHUB_HEAD_REF" != "" ]; then
branch_name=$GITHUB_HEAD_REF
fi

if [[ "$branch_name" != "main" ]]; then
IS_PRERELEASE=true;
CURRENT_VERSION=$(printf '%s-%s' "$CURRENT_VERSION" "$branch_name")
elif ! [[ $CURRENT_VERSION =~ ^([0-9]+)\.([0-9]+)\.([0-9]+)$ ]]; then
IS_PRERELEASE=true;
fi

release_type="release"
if [ $IS_PRERELEASE ]; then
release_type="prerelease"
fi

SANE_BRANCH_NAME_KEY=$(echo $branch_name | sed -e 's#/#_#g')
CURRENT_VERSION=$(echo $CURRENT_VERSION | sed -e 's#/#_#g')

printf "Current %s found to be %s\n" "$release_type" "$CURRENT_VERSION"
}

# Get published git tags that match semver regex with a "v" prefixbash then remove the "v" character
PUBLISHED_VERSIONS=$(git tag | grep "^v[0-9]\+\.[0-9]\+\.[0-9]\+\(\-[a-zA-Z-]\+\(\.[0-9]\+\)*\)\{0,1\}$" | sed 's/^v\(.*\)$/\1/')
# Get the current version from node Cargo.toml
CURRENT_VERSION=$(tomlq .package.version ./node/Cargo.toml | sed 's/"//g')

if check_version_greater "$CURRENT_VERSION" "$PUBLISHED_VERSIONS"; then
get_working_copy_version

if check_version_greater "$CURRENT_VERSION" "$PUBLISHED_VERSIONS" || $IS_PRERELEASE; then
echo "##[set-output name=VERSION;]v$CURRENT_VERSION"
echo "##[set-output name=SANE_BRANCH_NAME_KEY;]$SANE_BRANCH_NAME_KEY"
echo "##[set-output name=BUILD_DATE;]$(date -u +'%Y-%m-%dT%H:%M:%SZ')"
echo "##[set-output name=IS_NEW_VERSION;]true"
if [[ $CURRENT_VERSION =~ [-] ]]; then
if [ $IS_PRERELEASE ]; then
echo "##[set-output name=IS_PRERELEASE;]true"
else
echo "##[set-output name=IS_PRERELEASE;]false"
Expand Down

0 comments on commit b274dee

Please sign in to comment.