Skip to content

Commit

Permalink
Refactor versioning script
Browse files Browse the repository at this point in the history
  • Loading branch information
gpontesss committed Mar 12, 2023
1 parent 6b8e809 commit 4c44b1e
Show file tree
Hide file tree
Showing 3 changed files with 105 additions and 44 deletions.
65 changes: 52 additions & 13 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,23 +6,26 @@ on:
jobs:
increase-version:
runs-on: ubuntu-latest
outputs:
version: ${{ steps.inc-patch.outputs.version }}
steps:
- uses: actions/checkout@v2
- uses: actions-ecosystem/action-get-latest-tag@v1
id: get-latest-tag

- uses: actions-ecosystem/action-bump-semver@v1
id: bump-semver
with:
current_version: ${{ steps.get-latest-tag.outputs.tag }}
level: patch

- uses: actions-ecosystem/action-push-tag@v1
- uses: actions/checkout@v3
with:
tag: ${{ steps.bump-semver.outputs.new_version }}
fetch-depth: 0
- name: increase patch
id: inc-patch
run: |
echo "version=$(./scripts/version.sh inc patch)" >> "$GITHUB_OUTPUT"
- name: push tag ${{ steps.inc-patch.outputs.version }}
env:
version: ${{ steps.inc-patch.outputs.version }}
run: |
git config user.name "${{ github.actor }}"
git config user.email "${{ github.actor }}@users.noreply.github.com"
git tag "$version"
git push origin "$version"
build:
needs: [increase-version]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
Expand All @@ -36,3 +39,39 @@ jobs:
./scripts/install-font.sh "Gentium Book Plus" "EB Garamond"
- name: build project
run: make
- name: save built document
uses: actions/upload-artifact@v2
with:
name: built-document
path: out/main.pdf
retention-days: 1

release:
needs: [increase-version, build]
runs-on: ubuntu-latest
steps:
- name: retrieve built document
uses: actions/download-artifact@v2
with:
name: built-document

- name: release
uses: actions/create-release@v1
id: create_release
with:
draft: false
prerelease: false
release_name: ${{ needs.increase-version.outputs.version }}
tag_name: ${{ needs.increase-version.outputs.version }}
env:
GITHUB_TOKEN: ${{ github.token }}

- name: upload built document
uses: actions/upload-release-asset@v1
env:
GITHUB_TOKEN: ${{ github.token }}
with:
upload_url: ${{ steps.create_release.outputs.upload_url }}
asset_path: main.pdf
asset_name: prayer-book.pdf
asset_content_type: application/pdf
31 changes: 0 additions & 31 deletions scripts/increase-version.sh

This file was deleted.

53 changes: 53 additions & 0 deletions scripts/version.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
#!/usr/bin/env bash

##? Manages project versioning.
##?
##? Commands:

set -o errexit

declare reset="\e[0m"
declare red="\e[0;31m"

err() { echo -e "${red}$@${reset}" 1>&2 ; }
errexit() { err "$@" ; exit 1 ; }

usage() {
[[ "$#" -gt 0 ]] && err "$@"
sed -n 's:^##? \?::p' "$0"
exit 1
}

tags() { git tag | grep -E 'v[0-9]+\.[0-9]+\.[0-9]+' ; }
last_tag() { tags | sort --reverse | head -n1 ; }

get_major() { echo "$1" | sed 's:v\([0-9]\+\)\..*:\1:' ; }
get_minor() { echo "$1" | sed 's:v[0-9]\+\.\([0-9]\+\)\..*:\1:' ; }
get_patch() { echo "$1" | sed 's:v[0-9]\+\.[0-9]\+\.\([0-9]\+\):\1:' ; }

inc() {
local level="$1"
[[ -z "$level" ]] && usage
local last="${2:-$(last_tag)}"
local patch="$(get_patch "$last")"
local minor="$(get_minor "$last")"
local major="$(get_major "$last")"
local "$level"="$(($level+1))"
case "$level" in
minor) patch="0" ;;
major) patch="0" ; minor="0" ;;
esac
echo "v$major.$minor.$patch"
}

case "$1" in
##? last Returns last version.
last) last_tag ;;
##? list Lists all versions.
list) tags ;;
##? inc (major|minor|patch) <version> Increases a version level. By default,
##? will use the last version.
inc) shift ; inc "$@" ;;
"") usage ;;
*) usage "Unknown command '$1'" ;;
esac

0 comments on commit 4c44b1e

Please sign in to comment.