Skip to content

Update Version and Build Docker Image #108

Update Version and Build Docker Image

Update Version and Build Docker Image #108

# trunk-ignore-all(checkov/CKV2_GHA_1)
name: Update Version and Build Docker Image
on:
push:
branches:
- main
paths:
- version.txt
workflow_dispatch:
inputs:
force_tag: # trunk-ignore(checkov/CKV_GHA_7)
description: "Force tag creation"
type: choice
default: "no"
options:
- "no"
- "yes"
jobs:
update-version:
name: Update version
runs-on: ubuntu-latest
outputs:
sha_tag: ${{ steps.set_outputs_for_next_jobs.outputs.sha }}
version_tag: ${{ steps.set_outputs_for_next_jobs.outputs.version }}
branch_name: ${{ steps.commit_new_version.outputs.branch_name }}
env:
SHA_TAG: ${{ github.sha }}
VERSION_TAG: local #default
steps:
- name: Checkout code
uses: actions/checkout@v4
- name: Setup with python environment
uses: ./.github/include/setup_python/
with:
module: .
- name: Read version.txt
id: read_version
run: |
echo "VERSION_TAG=$(cat version.txt)" >> $GITHUB_ENV
- name: Update version in pyproject.toml & poetry.lock
run: |
python -m pip install --upgrade pip poetry
echo "Updating all pyproject.toml with version ${{ env.VERSION_TAG}}"
find . -name 'pyproject.toml' -type f -exec sed -i "s/^version.*/version = \"${{ env.VERSION_TAG}}\"/g" {} +
for folder in */; do
if [ -f "${folder}poetry.lock" ]; then
echo "Found pyproject.toml in folder: ${folder}"
(cd "${folder}" && poetry install)
fi
done
poetry install
- id: commit_new_version
name: Commit and push modified files
run: |
MODIFIED_FILES=$(git diff --name-only HEAD)
if [ -z "$MODIFIED_FILES" ]; then
echo "No changes to commit."
else
git config --global user.email "actions@github.com"
git config --global user.name "Github Actions"
# TODO check to see that only **/pyproject.toml or ./version.txt were modified
git add .
if [ "${{ github.ref_name }}" == "main" ]; then
BRANCH_NAME="${{ github.ref_name }}_v${{ env.VERSION_TAG}}"
git checkout -b $BRANCH_NAME
git commit -m "Update version number to ${{ env.VERSION_TAG}} by creating new branch: $BRANCH_NAME "
else
BRANCH_NAME=${{ github.ref_name }}
git commit -m "Update version number to ${{ env.VERSION_TAG}} on ${{ github.ref_name }}"
fi
git push origin $BRANCH_NAME
echo "SHA_TAG=$(git rev-parse HEAD)" >> $GITHUB_ENV
fi
echo "branch_name=${BRANCH_NAME}" >> $GITHUB_OUTPUT
- id: set_outputs_for_next_jobs
name: Set output for next jobs
run: |
echo "version=${{ env.VERSION_TAG}}" >> $GITHUB_OUTPUT
echo "sha=${{ env.SHA_TAG }}" >> $GITHUB_OUTPUT
build-test-docker-images:
name: Build docker images, test images and push to Github container registry
needs: update-version
runs-on: ubuntu-latest
environment: dev
strategy:
matrix:
include:
- {
module: 03_uns_graphdb,
image: uns/graphdb,
container_description: "Stores MQTT messages to the graph database. Supports both UNS and SparkplugB",
}
- {
module: 04_uns_historian,
image: uns/historian,
container_description: "Stores MQTT messages to the historian database. Supports both UNS and SparkplugB",
}
- {
module: 05_sparkplugb,
image: uns/spb_mapper,
container_description: "Listens to the SparkplugB name space and translates them to UNS messages, then publishes to the UNS Namespace",
}
- {
module: 06_uns_kafka,
image: uns/kafka_mapper,
container_description: "Listens to the UNS namespace, converts UNS topic to Kafka topic and publishes the message to Kafka",
}
- {
module: 07_uns_graphql,
image: uns/graphql,
container_description: "Provided GraphQL query capabilities to the UNS system",
}
max-parallel: 1 # need to have this as 1 if we want to use the cloud build
env:
SHA_TAG: ${{ needs.update-version.outputs.sha_tag }}
VERSION_TAG: ${{ needs.update-version.outputs.version_tag }}
steps:
- name: Login to Github Container Registry
uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Checkout Code
uses: actions/checkout@v4
with:
ref: ${{ needs.update-version.outputs.sha_tag }}
- name: Compile and Test Dockerfile
uses: ./.github/include/test_docker_builds/
with:
module: ${{ matrix.module }}
image_name: ${{ matrix.image }}
# commenting local QEMU builder as it was crashing the runner for some arm builds. Now using cloud build from docker
# - name: Set up QEMU
# # Add support for more platforms with QEMU (optional)
# # https://github.com/docker/setup-qemu-action
# uses: docker/setup-qemu-action@v3
# - name: Set up Docker Buildx
# uses: docker/setup-buildx-action@v3
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
with:
version: "lab:latest"
driver: cloud
endpoint: "mkashwin/github-mkashwin"
- name: Build Docker Image - Branches
if: ${{ ! startsWith( github.ref_name , 'main') }}
uses: docker/build-push-action@v6
with:
# spell-checker:disable
context: .
push: true
platforms: linux/amd64,linux/arm64 # linux/arm/v7 not working, causes error: command 'gcc' failed: No such file or directory
tags: |
"ghcr.io/${{ github.repository }}/${{ matrix.image }}:${{ env.VERSION_TAG }}"
"ghcr.io/${{ github.repository }}/${{ matrix.image }}:${{env.SHA_TAG }}"
labels: |
"annotations": { "org.opencontainers.image.description": ${{ matrix.container_description }} }
"annotations": { "org.opencontainers.image.source" : https://github.com/mkashwin/unifiednamespace/tree/main/${{ matrix.image }} }
"annotations": { "org.opencontainers.image.licenses" : MIT }
file: ./${{ matrix.module }}/Dockerfile
cache-from: type=gha
cache-to: type=gha,mode=max
# spell-checker:enable
- name: Build Docker Image - Main
if: ${{ startsWith( github.ref_name , 'main') }}
uses: docker/build-push-action@v6
with:
context: .
push: true
platforms: linux/amd64,linux/arm64 # linux/arm/v7 not working, causes error: command 'gcc' failed: No such file or directory
tags: |
"ghcr.io/${{ github.repository }}/${{ matrix.image }}:${{ env.VERSION_TAG }}"
"ghcr.io/${{ github.repository }}/${{ matrix.image }}:${{env.SHA_TAG }}"
"ghcr.io/${{ github.repository }}/${{ matrix.image }}:latest"
labels: |
"annotations": { "org.opencontainers.image.description": ${{ matrix.container_description }} }
"annotations": { "org.opencontainers.image.source" : https://github.com/mkashwin/unifiednamespace/tree/main/${{ matrix.image }} }
"annotations": { "org.opencontainers.image.licenses" : MIT }
file: ./${{ matrix.module }}/Dockerfile
cache-from: type=gha
cache-to: type=gha,mode=max
create_tag:
name: Tag the repository
needs: [update-version, build-test-docker-images]
runs-on: ubuntu-latest
steps:
- name: Checkout Code
uses: actions/checkout@v4
with:
ref: ${{ needs.update-version.outputs.sha_tag }}
- name: Tag repository
run: |
if [ "${{ github.ref_name }}" == "main" ]; then
# No changes made. We just need to tag (probably force tag)
version="v${{ needs.update-version.outputs.version_tag }}"
elif [ "${{ github.ref_name }}" == "main_v${{ needs.update-version.outputs.version_tag }}" ]; then
version="v${{ needs.update-version.outputs.version_tag }}"
else
version=${{ github.ref_name }}_v${{ needs.update-version.outputs.version_tag }}
fi
git config --global user.email "actions@github.com"
git config --global user.name "Github Actions"
if [[ "${{ inputs.force_tag }}" == "yes" ]]; then
git tag "$version" -m "Forced Tagging $version"
git push origin "$version" --force
else
git tag "$version" -m "Tagging $version"
git push origin "$version"
fi
create_pull_req:
name: Create Pull Request if the tag was on main branch
needs: [update-version, build-test-docker-images]
runs-on: ubuntu-latest
steps:
- name: Checkout Code
if: startsWith(needs.update-version.outputs.branch_name, 'main')
uses: actions/checkout@v4
with:
ref: ${{ needs.update-version.outputs.sha_tag }}
fetch-depth: 0
- name: Raise Pull Request
if: startsWith(needs.update-version.outputs.branch_name, 'main')
uses: devops-infra/action-pull-request@v0.5.5
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
title: "Updating version to ${{ needs.update-version.outputs.version_tag }} "
label: "v${{ needs.update-version.outputs.version_tag }},version-update"
source_branch: "${{ needs.update-version.outputs.branch_name }}"
target_branch: "main"
get_diff: true