Skip to content

Commit

Permalink
Split binaries for git tagging and version calculation to separate sc…
Browse files Browse the repository at this point in the history
…ripts
  • Loading branch information
jbuncle committed Aug 5, 2021
1 parent 8c775c0 commit cf0b828
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 61 deletions.
37 changes: 37 additions & 0 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
name: Publish Docker image

on:
push:
branches:
- main
- master
release:
types: [published]

jobs:
push_to_registry:
name: Push Docker image to Docker Hub
runs-on: ubuntu-latest
steps:
- name: Check out the repo
uses: actions/checkout@v2

- name: Log in to Docker Hub
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38
with:
images: ${{ github.repository }}

- name: Build and push Docker image
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
46 changes: 46 additions & 0 deletions bin/git-tag
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#!/bin/bash
set -e

# Check that required variables are defined
test -v GITAUTH_USER || (echo "Missing GITAUTH_USER variable" && exit 1)
test -v GITAUTH_PASS || (echo "Missing GITAUTH_PASS variable" && exit 1)

# Check gitlab variables are set
test -v GITLAB_USER_EMAIL || (echo "Missing GITLAB_USER_EMAIL variable" && exit 1)
test -v GITLAB_USER_NAME || (echo "Missing GITLAB_USER_NAME variable" && exit 1)
test -v CI_REPOSITORY_URL || (echo "Missing CI_REPOSITORY_URL variable" && exit 1)

# Setup git details
echo "Setting git author $GITLAB_USER_NAME <$GITLAB_USER_EMAIL>"
git config --global user.email "$GITLAB_USER_EMAIL"
git config --global user.name "$GITLAB_USER_NAME"


if [ -z "$1" ] ; then
echo "Missing version arg $1"
exit 1
fi


VERSION=$1

echo ""
echo "Creating Git tag '$VERSION'"
git tag $VERSION

# Show graph
git log --all --decorate --oneline --graph -10

# Update remote URL so we can push, then push the tag
echo "Adding auth to $CI_REPOSITORY_URL"
NEW_URL=$(addauth $CI_REPOSITORY_URL $GITAUTH_USER $GITAUTH_PASS)

echo "Updating remote"
ORIGINAL_REMOTE=$(git remote get-url origin)
git remote set-url origin $NEW_URL

echo "Pushing tag $VERSION"
git push origin $VERSION

echo "Resetting remote"
git remote set-url origin $ORIGINAL_REMOTE
24 changes: 24 additions & 0 deletions bin/next-version
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
# Calculates next version by looking up the last Git tag and comparing it to head

set -e

# Fetch tags
git fetch --tags --prune >/dev/null

# Check for existing tags
if [ "$(git ls-remote --tags | wc -l)" == "0" ] ; then
# Default version
PREV_VERSION='0.0.0'
COMPARE_TO=$(git log --pretty=format:%H | tail -1)
INC='MAJOR'
else
# Lookup latest version
PREV_VERSION=$(latesttag)
COMPARE_TO=$PREV_VERSION
INC=$(php-autosemver $COMPARE_TO)
fi

VERSION=$(composer-version --inc $PREV_VERSION $INC)

echo ${VERSION}
65 changes: 4 additions & 61 deletions bin/tag
Original file line number Diff line number Diff line change
@@ -1,64 +1,7 @@
#!/bin/bash
set -e

# Check for required variables are defined
test -v GITAUTH_USER || (echo "Missing GITAUTH_USER variable" && exit 1)
test -v GITAUTH_PASS || (echo "Missing GITAUTH_PASS variable" && exit 1)

# Check gitlab variables are set
test -v GITLAB_USER_EMAIL || (echo "Missing GITLAB_USER_EMAIL variable" && exit 1)
test -v GITLAB_USER_NAME || (echo "Missing GITLAB_USER_NAME variable" && exit 1)
test -v CI_REPOSITORY_URL || (echo "Missing CI_REPOSITORY_URL variable" && exit 1)

# Setup git details
echo "Setting git author $GITLAB_USER_NAME <$GITLAB_USER_EMAIL>"
git config --global user.email "$GITLAB_USER_EMAIL"
git config --global user.name "$GITLAB_USER_NAME"

# Fetch tags
echo "Fetching tags"
git fetch --tags --prune
# Check for existing tags
if [ "$(git ls-remote --tags | wc -l)" == "0" ] ; then
# Default version
PREV_VERSION='0.0.0'
COMPARE_TO=$(git log --pretty=format:%H | tail -1)
INC='MAJOR'
else
# Lookup latest version
PREV_VERSION=$(latesttag)
COMPARE_TO=$PREV_VERSION
echo "Analysing for next semver version compared to '$PREV_VERSION'"
INC=$(php-autosemver $COMPARE_TO)
fi

echo "Next inc is '$INC'"

echo "Calculating next version from '$PREV_VERSION'"
VERSION=$(composer-version --inc $PREV_VERSION $INC)

echo ""
echo "Current version: $PREV_VERSION"
echo "Increment: $INC"
echo "New version: $VERSION"

echo ""
echo "Creating Git tag '$VERSION'"
git tag $VERSION

# Show graph
git log --all --decorate --oneline --graph -10

# Update remote URL so we can push, then push the tag
echo "Adding auth to $CI_REPOSITORY_URL"
NEW_URL=$(addauth $CI_REPOSITORY_URL $GITAUTH_USER $GITAUTH_PASS)

echo "Updating remote"
ORIGINAL_REMOTE=$(git remote get-url origin)
git remote set-url origin $NEW_URL

echo "Pushing tag $VERSION"
git push origin $VERSION

echo "Resetting remote"
git remote set-url origin $ORIGINAL_REMOTE
VERSION=$(next-version)
echo "VERSION=${VERSION}"
echo "Tagging"
git-tag ${VERSION}

0 comments on commit cf0b828

Please sign in to comment.