reade me updated #3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Otomi Tools Build and Versioning | |
on: | |
push: | |
branches: | |
- 'main' | |
env: | |
NAMESPACE: otomi | |
REPO: tools | |
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 '^v[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 "\[TOOLS\]\[MAJOR\]"; then | |
# If a "[TOOLS][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 "\[TOOLS\]\[MINOR\]"; then | |
# If a "[TOOLS][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"; then | |
# No special tag found, but changes were made to the tools/Dockerfile 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 . | |
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/tools -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 }} |