From fc3485a5c5d965020336158fb766e504ac379387 Mon Sep 17 00:00:00 2001 From: priyanka-TL Date: Wed, 23 Jul 2025 14:02:58 +0530 Subject: [PATCH 1/4] Docker image workflow --- .github/workflows/docker-image.yml | 118 +++++++++++++++++++++++++++++ 1 file changed, 118 insertions(+) create mode 100644 .github/workflows/docker-image.yml diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml new file mode 100644 index 0000000..2e54bdc --- /dev/null +++ b/.github/workflows/docker-image.yml @@ -0,0 +1,118 @@ +name: Build and Push Docker Image + +on: + push: + branches: + - docker-image # Configure this to your target branch + pull_request: + branches: + - docker-image + types: [closed] + +env: + DOCKER_IMAGE_NAME: elevate-entity-management # Configure your image name here + DOCKER_REGISTRY: docker.io + DOCKER_NAMESPACE: shikshalokamqa + +jobs: + build-and-push: + if: github.event_name == 'push' || (github.event.pull_request.merged == true) + runs-on: ubuntu-latest + + steps: + - name: Checkout code + uses: actions/checkout@v4 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to Docker Hub + uses: docker/login-action@v3 + with: + username: ${{ secrets.DOCKERHUB_USERNAME }} + password: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Fetch latest version from Docker Hub + id: fetch-version + run: | + # Fetch tags from Docker Hub API + TAGS=$(curl -s "https://hub.docker.com/v2/namespaces/${{ env.DOCKER_NAMESPACE }}/repositories/${{ env.DOCKER_IMAGE_NAME }}/tags?page_size=100" | jq -r '.results[].name') + # Filter semantic version tags (e.g., 3.3.3) and sort to get the latest + LATEST_TAG=$(echo "$TAGS" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1) + if [ -z "$LATEST_TAG" ]; then + echo "No valid semver tag found, defaulting to 1.0.0" + echo "current_version=1.0.0" >> $GITHUB_OUTPUT + else + echo "current_version=$LATEST_TAG" >> $GITHUB_OUTPUT + fi + env: + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Calculate new version + id: increment-version + run: | + CURRENT_VERSION=${{ steps.fetch-version.outputs.current_version }} + # Split version into major, minor, patch + IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" + if [ -z "$MAJOR" ] || [ -z "$MINOR" ] || [ -z "$PATCH" ]; then + echo "Invalid version format, defaulting to 1.0.0" + NEW_VERSION="1.0.0" + else + # Increment patch, or minor if patch reaches 10 + if [ "$PATCH" -ge 9 ]; then + NEW_MINOR=$((MINOR + 1)) + NEW_PATCH=0 + else + NEW_MINOR=$MINOR + NEW_PATCH=$((PATCH + 1)) + fi + NEW_VERSION="$MAJOR.$NEW_MINOR.$NEW_PATCH" + fi + echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT + echo "Calculated new version: $NEW_VERSION" + shell: bash + + - name: Check if new version exists on Docker Hub + id: check-version + run: | + NEW_VERSION=${{ steps.increment-version.outputs.new_version }} + # Check if the new version tag exists + RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "https://hub.docker.com/v2/namespaces/${{ env.DOCKER_NAMESPACE }}/repositories/${{ env.DOCKER_IMAGE_NAME }}/tags/$NEW_VERSION") + if [ "$RESPONSE" -eq 200 ]; then + echo "Error: Tag $NEW_VERSION already exists on Docker Hub" + exit 1 + else + echo "Tag $NEW_VERSION does not exist, proceeding with build" + echo "version_exists=false" >> $GITHUB_OUTPUT + fi + env: + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + + - name: Extract metadata + id: meta + uses: docker/metadata-action@v5 + with: + images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_NAMESPACE }}/${{ env.DOCKER_IMAGE_NAME }} + tags: | + type=raw,value=${{ steps.increment-version.outputs.new_version }} + + - name: Build and push Docker image + id: build + uses: docker/build-push-action@v5 + with: + context: . + file: ./Dockerfile + push: true + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + platforms: linux/amd64,linux/arm64 + cache-from: type=gha + cache-to: type=gha,mode=max + + - name: Image digest + run: echo "Image pushed with digest ${{ steps.build.outputs.digest }}" + + - name: Print pushed tags + run: | + echo "Pushed tags:" + echo "${{ steps.meta.outputs.tags }}" | tr ',' '\n' \ No newline at end of file From 19576648301081ee5528ef84821e12fa8698a364 Mon Sep 17 00:00:00 2001 From: priyanka-TL Date: Wed, 23 Jul 2025 19:04:15 +0530 Subject: [PATCH 2/4] Updated the docker image for manual push --- .github/workflows/docker-image.yml | 81 +++++++++--------------------- 1 file changed, 25 insertions(+), 56 deletions(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 2e54bdc..4ec02a8 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -1,13 +1,11 @@ name: Build and Push Docker Image on: - push: - branches: - - docker-image # Configure this to your target branch - pull_request: - branches: - - docker-image - types: [closed] + workflow_dispatch: + inputs: + tag_version: + description: 'Docker image tag version (e.g., 3.3.11)' + required: true env: DOCKER_IMAGE_NAME: elevate-entity-management # Configure your image name here @@ -15,10 +13,10 @@ env: DOCKER_NAMESPACE: shikshalokamqa jobs: - build-and-push: + docker-image-build-and-push: if: github.event_name == 'push' || (github.event.pull_request.merged == true) runs-on: ubuntu-latest - + steps: - name: Checkout code uses: actions/checkout@v4 @@ -32,61 +30,32 @@ jobs: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} - - name: Fetch latest version from Docker Hub - id: fetch-version - run: | - # Fetch tags from Docker Hub API - TAGS=$(curl -s "https://hub.docker.com/v2/namespaces/${{ env.DOCKER_NAMESPACE }}/repositories/${{ env.DOCKER_IMAGE_NAME }}/tags?page_size=100" | jq -r '.results[].name') - # Filter semantic version tags (e.g., 3.3.3) and sort to get the latest - LATEST_TAG=$(echo "$TAGS" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+$' | sort -V | tail -n 1) - if [ -z "$LATEST_TAG" ]; then - echo "No valid semver tag found, defaulting to 1.0.0" - echo "current_version=1.0.0" >> $GITHUB_OUTPUT - else - echo "current_version=$LATEST_TAG" >> $GITHUB_OUTPUT - fi - env: - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Calculate new version - id: increment-version + - name: Get Docker tag version (fail if not provided) + id: get-version run: | - CURRENT_VERSION=${{ steps.fetch-version.outputs.current_version }} - # Split version into major, minor, patch - IFS='.' read -r MAJOR MINOR PATCH <<< "$CURRENT_VERSION" - if [ -z "$MAJOR" ] || [ -z "$MINOR" ] || [ -z "$PATCH" ]; then - echo "Invalid version format, defaulting to 1.0.0" - NEW_VERSION="1.0.0" + # Use workflow_dispatch input if provided + if [ ! -z "${{ github.event.inputs.tag_version }}" ]; then + VERSION="${{ github.event.inputs.tag_version }}" + # Or use TAG_VERSION env if set (for push/PR) + elif [ ! -z "${TAG_VERSION}" ]; then + VERSION="${TAG_VERSION}" else - # Increment patch, or minor if patch reaches 10 - if [ "$PATCH" -ge 9 ]; then - NEW_MINOR=$((MINOR + 1)) - NEW_PATCH=0 - else - NEW_MINOR=$MINOR - NEW_PATCH=$((PATCH + 1)) - fi - NEW_VERSION="$MAJOR.$NEW_MINOR.$NEW_PATCH" + echo "Error: Docker image version must be provided as workflow_dispatch input or TAG_VERSION env." + exit 1 fi - echo "new_version=$NEW_VERSION" >> $GITHUB_OUTPUT - echo "Calculated new version: $NEW_VERSION" - shell: bash + echo "version=$VERSION" >> $GITHUB_OUTPUT - - name: Check if new version exists on Docker Hub + - name: Check if version exists on Docker Hub id: check-version run: | - NEW_VERSION=${{ steps.increment-version.outputs.new_version }} - # Check if the new version tag exists - RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "https://hub.docker.com/v2/namespaces/${{ env.DOCKER_NAMESPACE }}/repositories/${{ env.DOCKER_IMAGE_NAME }}/tags/$NEW_VERSION") + VERSION=${{ steps.get-version.outputs.version }} + RESPONSE=$(curl -s -o /dev/null -w "%{http_code}" "https://hub.docker.com/v2/namespaces/${{ env.DOCKER_NAMESPACE }}/repositories/${{ env.DOCKER_IMAGE_NAME }}/tags/$VERSION") if [ "$RESPONSE" -eq 200 ]; then - echo "Error: Tag $NEW_VERSION already exists on Docker Hub" + echo "Error: Tag $VERSION already exists on Docker Hub" exit 1 else - echo "Tag $NEW_VERSION does not exist, proceeding with build" - echo "version_exists=false" >> $GITHUB_OUTPUT + echo "Tag $VERSION does not exist, proceeding with build" fi - env: - DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} - name: Extract metadata id: meta @@ -94,7 +63,7 @@ jobs: with: images: ${{ env.DOCKER_REGISTRY }}/${{ env.DOCKER_NAMESPACE }}/${{ env.DOCKER_IMAGE_NAME }} tags: | - type=raw,value=${{ steps.increment-version.outputs.new_version }} + type=raw,value=${{ steps.get-version.outputs.version }} - name: Build and push Docker image id: build @@ -105,7 +74,7 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - platforms: linux/amd64,linux/arm64 + platforms: linux/amd64,linux/arm64,windows/amd64 cache-from: type=gha cache-to: type=gha,mode=max From 5325779d8e5949c3c55941288642b5fbdd5eae0f Mon Sep 17 00:00:00 2001 From: priyanka-TL Date: Wed, 23 Jul 2025 19:17:25 +0530 Subject: [PATCH 3/4] update workflow condition --- .github/workflows/docker-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index 4ec02a8..ef44ae2 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -14,7 +14,7 @@ env: jobs: docker-image-build-and-push: - if: github.event_name == 'push' || (github.event.pull_request.merged == true) + if: github.event_name == 'workflow_dispatch' && github.ref == 'refs/heads/docker-image' runs-on: ubuntu-latest steps: From 4694f1d53bfced24ca9c0fb1785ec48301b1fd80 Mon Sep 17 00:00:00 2001 From: priyanka-TL Date: Wed, 23 Jul 2025 19:21:02 +0530 Subject: [PATCH 4/4] Removed windows --- .github/workflows/docker-image.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/docker-image.yml b/.github/workflows/docker-image.yml index ef44ae2..8c6afba 100644 --- a/.github/workflows/docker-image.yml +++ b/.github/workflows/docker-image.yml @@ -74,7 +74,7 @@ jobs: push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} - platforms: linux/amd64,linux/arm64,windows/amd64 + platforms: linux/amd64,linux/arm64 cache-from: type=gha cache-to: type=gha,mode=max