Build and Push Docker Image #13
This file contains hidden or 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: Build and Push Docker Image | |
| on: | |
| 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 | |
| DOCKER_REGISTRY: docker.io | |
| DOCKER_NAMESPACE: shikshalokamqa | |
| jobs: | |
| docker-image-build-and-push: | |
| if: github.event_name == 'workflow_dispatch' | |
| 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: Get Docker tag version (fail if not provided) | |
| id: get-version | |
| run: | | |
| # 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 | |
| echo "Error: Docker image version must be provided as workflow_dispatch input or TAG_VERSION env." | |
| exit 1 | |
| fi | |
| echo "version=$VERSION" >> $GITHUB_OUTPUT | |
| - name: Check if version exists on Docker Hub | |
| id: check-version | |
| run: | | |
| 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 $VERSION already exists on Docker Hub" | |
| exit 1 | |
| else | |
| echo "Tag $VERSION does not exist, proceeding with build" | |
| fi | |
| - 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.get-version.outputs.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' |