Skip to content

Commit 1f517cd

Browse files
Merge pull request #54 from microsoft/feature/build-docker-image-push-registry
ci: build docker image and push to container registry
2 parents 512e0ee + 4974cc8 commit 1f517cd

2 files changed

Lines changed: 121 additions & 0 deletions

File tree

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
name: Build Docker and Optional Push
2+
3+
on:
4+
push:
5+
branches:
6+
- main
7+
- dev
8+
- demo
9+
- hotfix
10+
pull_request:
11+
branches:
12+
- main
13+
- dev
14+
- demo
15+
- hotfix
16+
types:
17+
- opened
18+
- ready_for_review
19+
- reopened
20+
- synchronize
21+
merge_group:
22+
workflow_dispatch:
23+
24+
jobs:
25+
docker-build:
26+
strategy:
27+
matrix:
28+
include:
29+
- app_name: cmsabackend
30+
dockerfile: docker/Backend.Dockerfile
31+
password_secret: DOCKER_PASSWORD
32+
- app_name: cmsafrontend
33+
dockerfile: docker/Frontend.Dockerfile
34+
password_secret: DOCKER_PASSWORD
35+
uses: ./.github/workflows/build-docker.yml
36+
with:
37+
registry: cmsacontainerreg.azurecr.io
38+
username: cmsacontainerreg
39+
password_secret: ${{ matrix.password_secret }}
40+
app_name: ${{ matrix.app_name }}
41+
dockerfile: ${{ matrix.dockerfile }}
42+
push: ${{ github.event_name == 'push' || github.base_ref == 'main' || github.base_ref == 'dev' || github.base_ref == 'demo' || github.base_ref == 'hotfix' }}
43+
secrets: inherit

.github/workflows/build-docker.yml

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
name: Reusable Docker build and push workflow
2+
3+
on:
4+
workflow_call:
5+
inputs:
6+
registry:
7+
required: true
8+
type: string
9+
username:
10+
required: true
11+
type: string
12+
password_secret:
13+
required: true
14+
type: string
15+
app_name:
16+
required: true
17+
type: string
18+
dockerfile:
19+
required: true
20+
type: string
21+
push:
22+
required: true
23+
type: boolean
24+
secrets:
25+
DOCKER_PASSWORD:
26+
required: true
27+
28+
jobs:
29+
docker-build:
30+
runs-on: ubuntu-latest
31+
steps:
32+
33+
- name: Checkout
34+
uses: actions/checkout@v4
35+
36+
- name: Docker Login
37+
if: ${{ inputs.push }}
38+
uses: docker/login-action@v3
39+
with:
40+
registry: ${{ inputs.registry }}
41+
username: ${{ inputs.username }}
42+
password: ${{ secrets[inputs.password_secret] }}
43+
44+
- name: Set up Docker Buildx
45+
uses: docker/setup-buildx-action@v3
46+
47+
- name: Get current date
48+
id: date
49+
run: echo "date=$(date +'%Y-%m-%d')" >> $GITHUB_OUTPUT
50+
51+
- name: Determine Tag Name Based on Branch
52+
id: determine_tag
53+
run: |
54+
if [[ "${{ github.base_ref }}" == "main" ]]; then
55+
echo "tagname=latest" >> $GITHUB_OUTPUT
56+
elif [[ "${{ github.base_ref }}" == "dev" ]]; then
57+
echo "tagname=dev" >> $GITHUB_OUTPUT
58+
elif [[ "${{ github.base_ref }}" == "demo" ]]; then
59+
echo "tagname=demo" >> $GITHUB_OUTPUT
60+
elif [[ "${{ github.base_ref }}" == "hotfix" ]]; then
61+
echo "tagname=hotfix" >> $GITHUB_OUTPUT
62+
elif [[ "${{ github.base_ref }}" == "dependabotchanges" ]]; then
63+
echo "tagname=dependabotchanges" >> $GITHUB_OUTPUT
64+
else
65+
echo "tagname=default" >> $GITHUB_OUTPUT
66+
fi
67+
68+
69+
- name: Build Docker Image and optionally push
70+
uses: docker/build-push-action@v6
71+
with:
72+
context: .
73+
file: ${{ inputs.dockerfile }}
74+
push: ${{ inputs.push }}
75+
cache-from: type=registry,ref=${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.determine_tag.outputs.tagname }}
76+
tags: |
77+
${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.determine_tag.outputs.tagname }}
78+
${{ inputs.registry }}/${{ inputs.app_name}}:${{ steps.determine_tag.outputs.tagname }}_${{ steps.date.outputs.date }}_${{ github.run_number }}

0 commit comments

Comments
 (0)