From cf0b82891e9e717cad4552a965982995814a7636 Mon Sep 17 00:00:00 2001 From: James Buncle Date: Thu, 5 Aug 2021 15:14:39 +0100 Subject: [PATCH] Split binaries for git tagging and version calculation to separate scripts --- .github/workflows/build.yml | 37 +++++++++++++++++++++ bin/git-tag | 46 ++++++++++++++++++++++++++ bin/next-version | 24 ++++++++++++++ bin/tag | 65 +++---------------------------------- 4 files changed, 111 insertions(+), 61 deletions(-) create mode 100644 .github/workflows/build.yml create mode 100644 bin/git-tag create mode 100644 bin/next-version diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml new file mode 100644 index 0000000..05ea17f --- /dev/null +++ b/.github/workflows/build.yml @@ -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 }} diff --git a/bin/git-tag b/bin/git-tag new file mode 100644 index 0000000..0fac746 --- /dev/null +++ b/bin/git-tag @@ -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 diff --git a/bin/next-version b/bin/next-version new file mode 100644 index 0000000..a23de09 --- /dev/null +++ b/bin/next-version @@ -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} \ No newline at end of file diff --git a/bin/tag b/bin/tag index 66517b8..64b1f15 100755 --- a/bin/tag +++ b/bin/tag @@ -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} \ No newline at end of file