feat: building the new image #578
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: CloudTTY Build and Versioning | |
on: | |
push: | |
branches: | |
- 'main' | |
- 'APL-369' | |
env: | |
NAMESPACE: linode | |
REPO: apl-tty | |
jobs: | |
build-and-version: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: '2' | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Determine version | |
id: determine-version | |
run: | | |
set -e | |
if ${{ github.ref_name }} != "main" | |
NEW_VERSION=${{ github.ref_name }} | |
exit 0 | |
fi | |
# # 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 }}:${{ env.NEW_VERSION }} -t ${{ env.NAMESPACE }}/${{ env.REPO }}:latest -f tools/Dockerfile-tty . | |
- name: Squash image | |
if: ${{ env.NEW_VERSION != null }} | |
run: | | |
pip3 install docker-squash | |
docker-squash ${{ env.NAMESPACE }}/${{ env.REPO }} -t ${{ env.NAMESPACE }}/${{ env.REPO }}:${{ env.NEW_VERSION }} | |
- name: Login to GitHub Container Registry | |
if: ${{ env.NEW_VERSION != null }} | |
uses: docker/login-action@v3 | |
with: | |
username: ${{ vars.DOCKERHUB_LINODEBOT_USERNAME }} | |
password: ${{ secrets.DOCKERHUB_LINODEBOT_TOKEN }} | |
- name: Push Docker image | |
if: ${{ env.NEW_VERSION != null }} | |
run: | | |
docker push ${{ env.NAMESPACE }}/${{ env.REPO }}:${{ env.NEW_VERSION }} | |
docker push ${{ env.NAMESPACE }}/${{ env.REPO }}:latest |