From 770e6d7ffc330fa987e793bc1b2f58c321601162 Mon Sep 17 00:00:00 2001 From: "Priyanka Singhal (Persistent Systems Inc)" Date: Thu, 3 Apr 2025 12:15:08 +0530 Subject: [PATCH 1/3] Build Docker image and push to container registry --- .github/workflows/build-docker-images.yml | 41 ++++++++++++ .github/workflows/build-docker.yml | 76 +++++++++++++++++++++++ 2 files changed, 117 insertions(+) create mode 100644 .github/workflows/build-docker-images.yml create mode 100644 .github/workflows/build-docker.yml diff --git a/.github/workflows/build-docker-images.yml b/.github/workflows/build-docker-images.yml new file mode 100644 index 0000000..0388b4f --- /dev/null +++ b/.github/workflows/build-docker-images.yml @@ -0,0 +1,41 @@ +name: Build Docker and Optional Push + +on: + push: + branches: + - main + - dev + - demo + pull_request: + branches: + - main + - dev + - demo + types: + - opened + - ready_for_review + - reopened + - synchronize + merge_group: + workflow_dispatch: + +jobs: + docker-build: + strategy: + matrix: + include: + - app_name: backend + dockerfile: docker/Backend.Dockerfile + password_secret: DOCKER_PASSWORD + - app_name: webapp + dockerfile: docker/Frontend.Dockerfile + password_secret: DOCKER_PASSWORD + uses: ./.github/workflows/build-docker.yml + with: + registry: codegencontainerregpk.azurecr.io + username: codegencontainerregpk + password_secret: ${{ matrix.password_secret }} + app_name: ${{ matrix.app_name }} + dockerfile: ${{ matrix.dockerfile }} + push: ${{ github.event_name == 'push' || github.base_ref == 'main' || github.base_ref == 'dev' || github.base_ref == 'demo' }} + secrets: inherit \ No newline at end of file diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml new file mode 100644 index 0000000..1d256bc --- /dev/null +++ b/.github/workflows/build-docker.yml @@ -0,0 +1,76 @@ +name: Reusable Docker build and push workflow + +on: + workflow_call: + inputs: + registry: + required: true + type: string + username: + required: true + type: string + password_secret: + required: true + type: string + app_name: + required: true + type: string + dockerfile: + required: true + type: string + push: + required: true + type: boolean + secrets: + DOCKER_PASSWORD: + required: true + +jobs: + docker-build: + runs-on: ubuntu-latest + steps: + + - name: Checkout + uses: actions/checkout@v4 + + - name: Docker Login + if: ${{ inputs.push }} + uses: docker/login-action@v3 + with: + registry: ${{ inputs.registry }} + username: ${{ inputs.username }} + password: ${{ secrets[inputs.password_secret] }} + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Get current date + id: date + run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT + + - name: Determine Tag Name Based on Branch + id: determine_tag + run: | + if [[ "${{ github.base_ref }}" == "main" ]]; then + echo "tagname=latest" >> $GITHUB_OUTPUT + elif [[ "${{ github.base_ref }}" == "dev" ]]; then + echo "tagname=dev" >> $GITHUB_OUTPUT + elif [[ "${{ github.base_ref }}" == "demo" ]]; then + echo "tagname=demo" >> $GITHUB_OUTPUT + elif [[ "${{ github.base_ref }}" == "dependabotchanges" ]]; then + echo "tagname=dependabotchanges" >> $GITHUB_OUTPUT + else + echo "tagname=default" >> $GITHUB_OUTPUT + fi + + + - name: Build Docker Image and optionally push + uses: docker/build-push-action@v6 + with: + context: . + file: ${{ inputs.dockerfile }} + push: ${{ inputs.push }} + cache-from: type=registry,ref=${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.determine_tag.outputs.tagname }} + tags: | + ${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.determine_tag.outputs.tagname }} + ${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.determine_tag.outputs.tagname }}_${{ steps.date.outputs.date }}_${{ github.run_number }} \ No newline at end of file From 1c6b80ffa36e7d18a727a10233f836a5058e7be0 Mon Sep 17 00:00:00 2001 From: "Priyanka Singhal (Persistent Systems Inc)" Date: Thu, 3 Apr 2025 13:15:03 +0530 Subject: [PATCH 2/3] added hotfix branch as well --- .github/workflows/build-docker-images.yml | 4 +++- .github/workflows/build-docker.yml | 2 ++ 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/.github/workflows/build-docker-images.yml b/.github/workflows/build-docker-images.yml index 0388b4f..1c8ed2c 100644 --- a/.github/workflows/build-docker-images.yml +++ b/.github/workflows/build-docker-images.yml @@ -6,11 +6,13 @@ on: - main - dev - demo + - hotfix pull_request: branches: - main - dev - demo + - hotfix types: - opened - ready_for_review @@ -37,5 +39,5 @@ jobs: password_secret: ${{ matrix.password_secret }} app_name: ${{ matrix.app_name }} dockerfile: ${{ matrix.dockerfile }} - push: ${{ github.event_name == 'push' || github.base_ref == 'main' || github.base_ref == 'dev' || github.base_ref == 'demo' }} + push: ${{ github.event_name == 'push' || github.base_ref == 'main' || github.base_ref == 'dev' || github.base_ref == 'demo' || github.base_ref == 'hotfix' }} secrets: inherit \ No newline at end of file diff --git a/.github/workflows/build-docker.yml b/.github/workflows/build-docker.yml index 1d256bc..03deeb7 100644 --- a/.github/workflows/build-docker.yml +++ b/.github/workflows/build-docker.yml @@ -57,6 +57,8 @@ jobs: echo "tagname=dev" >> $GITHUB_OUTPUT elif [[ "${{ github.base_ref }}" == "demo" ]]; then echo "tagname=demo" >> $GITHUB_OUTPUT + elif [[ "${{ github.base_ref }}" == "hotfix" ]]; then + echo "tagname=hotfix" >> $GITHUB_OUTPUT elif [[ "${{ github.base_ref }}" == "dependabotchanges" ]]; then echo "tagname=dependabotchanges" >> $GITHUB_OUTPUT else From 4ab7437020e0f02c22ed6e7f63957c6a01ad643a Mon Sep 17 00:00:00 2001 From: "Priyanka Singhal (Persistent Systems Inc)" Date: Thu, 3 Apr 2025 13:17:20 +0530 Subject: [PATCH 3/3] changed container registry name --- .github/workflows/build-docker-images.yml | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/build-docker-images.yml b/.github/workflows/build-docker-images.yml index 1c8ed2c..4301807 100644 --- a/.github/workflows/build-docker-images.yml +++ b/.github/workflows/build-docker-images.yml @@ -26,16 +26,16 @@ jobs: strategy: matrix: include: - - app_name: backend + - app_name: cmsabackend dockerfile: docker/Backend.Dockerfile password_secret: DOCKER_PASSWORD - - app_name: webapp + - app_name: cmsafrontend dockerfile: docker/Frontend.Dockerfile password_secret: DOCKER_PASSWORD uses: ./.github/workflows/build-docker.yml with: - registry: codegencontainerregpk.azurecr.io - username: codegencontainerregpk + registry: cmsacontainerreg.azurecr.io + username: cmsacontainerreg password_secret: ${{ matrix.password_secret }} app_name: ${{ matrix.app_name }} dockerfile: ${{ matrix.dockerfile }}