Skip to content

Otomi baseline code added #1

Otomi baseline code added

Otomi baseline code added #1

name: CloudTTY Build and Versioning
on:
push:
branches:
- 'main'
env:
NAMESPACE: otomi
REPO: tty
jobs:
build-and-version:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
fetch-depth: '2'
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2
- name: Determine version
id: determine-version
run: |
set -e
# # Set the first image version to '0.1.0' if the repo does not exists.
# if ! curl -s -L --fail "https://hub.docker.com/v2/repositories/${{ env.NAMESPACE }}/${{ env.REPO }}"; then echo "NEW_VERSION=0.1.0" >> $GITHUB_ENV && exit 0; fi
# Get data for latest 10 versions of the image and filter the ones matching our semver pattern. Set the OLD_VERSION environment variable to the latest version.
# The grep command matches the strings following this pattern: starts with an up to 2 digits number, a dot, an up to 3 digit number, a dot, ends with an up to 4 digits number
REPO_URL="https://hub.docker.com/v2/repositories/${{ env.NAMESPACE }}/${{ env.REPO }}/tags/?page_size=10"
OLD_VERSION="$(curl -s -L --fail ${REPO_URL} | jq '.results[].name' -r | grep -E '^[0-9]{0,2}.[0-9]{0,3}.[0-9]{0,4}$' | head -n 1)"
echo "OLD_VERSION=${OLD_VERSION}" >> $GITHUB_ENV
if git log --format=%B -n 1 ${{ github.sha }} | grep -q "\[TTY\]\[MAJOR\]"; then
# If a "[TTY][MAJOR]" commit is found, increment the major version by one and reset the minor and patch version to '0'.
NEW_VERSION="$(echo ${OLD_VERSION} | awk -F. -v OFS=. '{$1 = $1 + 1; $2 = 0; $3 = 0} {print $0}')"
echo "NEW_VERSION=${NEW_VERSION}" >> $GITHUB_ENV
echo OLD_VERSION = ${OLD_VERSION}
echo NEW_VERSION = ${NEW_VERSION}
elif git log --format=%B -n 1 ${{ github.sha }} | grep -q "\[TTY\]\[MINOR\]"; then
# If a "[TTY][MINOR]" commit is found, increment the minor version by one and reset the patch version to '0'.
NEW_VERSION="$(echo ${OLD_VERSION} | awk -F. -v OFS=. '{$2 = $2 + 1; $3 = 0} {print $0}')"
echo "NEW_VERSION=${NEW_VERSION}" >> $GITHUB_ENV
echo OLD_VERSION = ${OLD_VERSION}
echo NEW_VERSION = ${NEW_VERSION}
elif git diff-tree --no-commit-id --name-only ${{ github.sha }} -r | grep -q "tools\/Dockerfile-tty"; then
# No special tag found, but changes were made to the tools/Dockerfile-tty so will upgrade the patch version.
NEW_VERSION="$(echo ${OLD_VERSION} | awk -F. -v OFS=. '{$3 = $3 + 1} {print $0}')"
echo "NEW_VERSION=${NEW_VERSION}" >> $GITHUB_ENV
echo OLD_VERSION = ${OLD_VERSION}
echo NEW_VERSION = ${NEW_VERSION}
fi
echo "No need to bump the version. Will skip next steps."
- name: Build and tag Docker image
if: ${{ env.NEW_VERSION != null }}
run: |
docker buildx build --load -t ${{ env.NAMESPACE }}/${{ env.REPO }} -f tools/Dockerfile-tty .
docker tag ${{ env.NAMESPACE }}/${{ env.REPO }} ${{ env.NAMESPACE }}/${{ env.REPO }}:${{ env.NEW_VERSION }}
- name: Squash image
if: ${{ env.NEW_VERSION != null }}
run: |
pip3 install docker-squash
docker-squash otomi/tty -t ${{ env.NAMESPACE }}/${{ env.REPO }}:${{ env.NEW_VERSION }}
- name: Login to GitHub Container Registry
if: ${{ env.NEW_VERSION != null }}
uses: docker/[email protected]
with:
username: 'otomi'
password: '${{ secrets.DOCKERHUB_OTOMI_TOKEN }}'
- name: Push Docker image
if: ${{ env.NEW_VERSION != null }}
run: |
docker push ${{ env.NAMESPACE }}/${{ env.REPO }}:${{ env.NEW_VERSION }}