Skip to content

ci: build docker image and push to container registry #52

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions .github/workflows/build-docker-images.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
name: Build Docker and Optional Push

on:
push:
branches:
- main
- dev
- demo
- hotfix
pull_request:
branches:
- main
- dev
- demo
- hotfix
types:
- opened
- ready_for_review
- reopened
- synchronize
merge_group:
workflow_dispatch:

jobs:
docker-build:
strategy:
matrix:
include:
- app_name: cmsabackend
dockerfile: docker/Backend.Dockerfile
password_secret: DOCKER_PASSWORD
- app_name: cmsafrontend
dockerfile: docker/Frontend.Dockerfile
password_secret: DOCKER_PASSWORD
uses: ./.github/workflows/build-docker.yml
with:
registry: cmsacontainerreg.azurecr.io
username: cmsacontainerreg
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' || github.base_ref == 'hotfix' }}
secrets: inherit
78 changes: 78 additions & 0 deletions .github/workflows/build-docker.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
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 }}" == "hotfix" ]]; then
echo "tagname=hotfix" >> $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 }}
Loading