Skip to content

Update build pipeline #4

Update build pipeline

Update build pipeline #4

name: Build and Push Docker Image
on:
workflow_dispatch:
inputs:
logLevel:
description: 'Log level'
required: true
default: 'information'
type: choice
options:
- information
- debug
- warning
- critical
tags:
description: 'Purpose of Run This Workflow?'
required: true
type: string
push:
branches: ["main"]
pull_request:
branches: ["main"]
permissions:
contents: write # Grant permission to read and write repository contents
packages: write # Optional: Grant permission to manage packages
deployments: write # Optional: Grant permission to manage deployments
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Full history for accurate diff
- name: Check for changes
id: check_changes
run: |
# Fetch all tags to ensure the latest tag is available
git fetch --tags
# Get the latest tag
LATEST_TAG=$(git describe --tags --abbrev=0)
echo "Latest tag: $LATEST_TAG"
# Compare changes from the latest tag
CHANGED_FILES=$(git diff --name-only $LATEST_TAG HEAD)
echo "Changed files since tag $LATEST_TAG: $CHANGED_FILES"
# Set environment variables for change detection
DOCKERFILE_CHANGED=false
REQUIREMENTS_CHANGED=false
SRC_CHANGED=false
DE_CHANGED=false
# Check if relevant files have changed and set the respective variables
if echo "$CHANGED_FILES" | grep -q '^Dockerfile'; then
DOCKERFILE_CHANGED=true
fi
if echo "$CHANGED_FILES" | grep -q '^requirements.txt'; then
REQUIREMENTS_CHANGED=true
fi
if echo "$CHANGED_FILES" | grep -q '^src/'; then
SRC_CHANGED=true
fi
if echo "$CHANGED_FILES" | grep -q '^de/'; then
DE_CHANGED=true
fi
# If any of the relevant files have changed, set a single build-needed flag
if [[ "$DOCKERFILE_CHANGED" == "true" || "$REQUIREMENTS_CHANGED" == "true" || "$SRC_CHANGED" == "true" || "$DE_CHANGED" == "true" ]]; then
echo "BUILD_NEEDED=true" >> $GITHUB_ENV
else
echo "BUILD_NEEDED=false" >> $GITHUB_ENV
fi
- name: Log in to Docker Hub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}
- name: Build Docker Image
id: build
run: |
# Create the full tag with build number
VERSION="1.0.0"
BUILD_NUMBER=${GITHUB_RUN_NUMBER}
FULL_TAG="${VERSION}-${BUILD_NUMBER}"
# Configure git
git config --global user.email "[email protected]"
git config --global user.name "ActionsBot"
echo "Building Docker image with tag: $FULL_TAG"
# Build and tag the Docker image
docker build -t nweistra/itlportal:${FULL_TAG} .
# Promote versioned package to latest
docker tag nweistra/itlportal:${FULL_TAG} nweistra/itlportal:latest
# Tag commit in git
git tag -a "${FULL_TAG}" -m "Release version ${FULL_TAG}"
git push origin ${FULL_TAG}
# Set the output variable for the full tag
echo "FULL_TAG=${FULL_TAG}" >> $GITHUB_ENV
- name: Push Docker Image
run: |
echo "Pushing Docker image with tag: $FULL_TAG"
docker push nweistra/itlportal:${FULL_TAG}
docker push nweistra/itlportal:latest
- name: Push Docker Image with -release tag (only for main branch)
if: github.ref == 'refs/heads/main'
run: |
RELEASE_TAG="${FULL_TAG}-release"
echo "Tagging commit with release tag"
git tag -a "${FULL_TAG}-release" -m "Release version ${FULL_TAG}"
git push origin ${FULL_TAG}-release
echo "Pushing Docker image with release tag: $RELEASE_TAG"
# Tag the image with the release suffix
docker tag nweistra/itlportal:${FULL_TAG} nweistra/itlportal:${RELEASE_TAG}
docker tag nweistra/itlportal:latest nweistra/itlportal:release-latest
# Push the images with the release tags
docker push nweistra/itlportal:${RELEASE_TAG}
docker push nweistra/itlportal:release-latest
# Set the output variable for the full tag
echo "RELEASE_TAG=${RELEASE_TAG}" >> $GITHUB_ENV
- name: Create GitHub Release
if: github.ref == 'refs/heads/main'
id: create_release
uses: softprops/action-gh-release@v1
with:
tag_name: "${{ env.FULL_TAG }}"
body: |
Docker image version: `${{ env.RELEASE_TAG }}`
Pushed Docker images:
- `nweistra/itlportal:${{ env.RELEASE_TAG }}`
- `nweistra/itlportal:latest`
draft: false
prerelease: false
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # Ensure this secret is available in your repo