1+ name : Build docker image
2+
3+ on :
4+ push :
5+ branches :
6+ - " main"
7+ tags :
8+ - " v*"
9+ pull_request :
10+
11+ jobs :
12+ build :
13+ name : Build & test docker image
14+ runs-on : ubuntu-latest
15+ steps :
16+ - name : Checkout repository
17+ uses : actions/checkout@v4
18+ - name : Create image tag
19+ id : image_tag
20+ run : |
21+ # Define cache dir
22+ ARCH=${{ runner.arch }}
23+ CACHE_PATH="/tmp/docker_cache_${ARCH,,}"
24+ # Get Dockerfile hash for image cache
25+ IMAGE_HASH="${{ hashFiles('./Dockerfile') }}"
26+ # Create image tag
27+ VARIANT="$(TZ=UTC-9 date +%Y%m%d)_${IMAGE_HASH:0:7}"
28+ IMAGE_NAME="geo_cb_surge_${ARCH,,}"
29+ TAG="${IMAGE_NAME}:${VARIANT}"
30+ # Cache dir setting
31+ TAR_NAME="${IMAGE_NAME}_${VARIANT}.tar"
32+ TAR_PATH="${CACHE_PATH}/${TAR_NAME}"
33+ echo "TAG=${TAG}" >> $GITHUB_OUTPUT
34+ echo "IMAGE_NAME=${IMAGE_NAME}" >> $GITHUB_OUTPUT
35+ echo "TAR_PATH=${TAR_PATH}" >> $GITHUB_OUTPUT
36+ echo "CACHE_PATH=${CACHE_PATH}" >> $GITHUB_OUTPUT
37+ echo "CACHE_KEY=${IMAGE_NAME}_${VARIANT}" >> $GITHUB_OUTPUT
38+ - name : Enable cache
39+ id : cache
40+ uses : actions/cache@v4
41+ with :
42+ path : ${{ steps.image_tag.outputs.CACHE_PATH }}
43+ key : ${{ steps.image_tag.outputs.CACHE_KEY }}
44+
45+ - name : Load image from cache if exists
46+ if : steps.cache.outputs.cache-hit == 'true'
47+ run : |
48+ docker load -i ${{ steps.image_tag.outputs.TAR_PATH }}
49+
50+ - name : Build image if cache does not exist
51+ if : steps.cache.outputs.cache-hit != 'true'
52+ run : |
53+ docker build -t ${{ steps.image_tag.outputs.TAG }} .
54+ CACHE_DIR=$(dirname "${{ steps.image_tag.outputs.TAR_PATH }}")
55+ mkdir -p "$CACHE_DIR"
56+ docker save ${{ steps.image_tag.outputs.TAG }} > ${{ steps.image_tag.outputs.TAR_PATH }}
57+
58+ - name : Run tests in container
59+ run : |
60+ # Change owner of workspace to ubuntu user
61+ sudo chown -R 1000:1000 ${{ github.workspace }}
62+ docker run --rm -v ${{ github.workspace }}:/app -w /app ${{ steps.image_tag.outputs.TAG }} make test
63+
64+ deploy-acr :
65+ name : Build and deploy to Azure Container Registry
66+ runs-on : ubuntu-latest
67+ environment :
68+ name : azure container registry
69+ env :
70+ IMAGE_NAME : ${{ github.repository}}
71+ permissions :
72+ contents : read
73+ packages : write
74+ steps :
75+ - name : Extract Docker metadata
76+ id : meta
77+ uses : docker/metadata-action@v4
78+ with :
79+ images : ${{ secrets.ACR_ENDPOINT }}/${{ env.IMAGE_NAME }}
80+ - name : Checkout repository
81+ uses : actions/checkout@v4
82+ - name : Log into ACR
83+ uses : docker/login-action@v3
84+ with :
85+ registry : ${{ secrets.ACR_ENDPOINT }}
86+ username : ${{ secrets.ACR_USERNAME }}
87+ password : ${{ secrets.ACR_PASSWORD }}
88+ - name : Build and push
89+ uses : docker/build-push-action@v5
90+ with :
91+ push : ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')}}
92+ context : .
93+ file : Dockerfile
94+ tags : ${{ steps.meta.outputs.tags }}
95+ labels : ${{ steps.meta.outputs.labels }}
96+
97+ deploy-github :
98+ name : Build and deploy to GitHub container registry
99+ runs-on : ubuntu-latest
100+ environment :
101+ name : github container registry
102+ env :
103+ REGISTRY : ghcr.io
104+ IMAGE_NAME : ${{ github.repository}}
105+ permissions :
106+ contents : write
107+ packages : write
108+ steps :
109+ - name : Checkout repository
110+ uses : actions/checkout@v4
111+
112+ - name : Log in to the Container registry
113+ uses : docker/login-action@v3
114+ with :
115+ registry : ${{ env.REGISTRY }}
116+ username : ${{ github.actor }}
117+ password : ${{ secrets.GITHUB_TOKEN }}
118+
119+ - name : Extract metadata (tags, labels) for Docker
120+ id : meta
121+ uses : docker/metadata-action@v5
122+ with :
123+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
124+
125+ - name : Build and push Docker image
126+ uses : docker/build-push-action@v6
127+ with :
128+ context : .
129+ file : Dockerfile
130+ push : ${{ github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')}}
131+ tags : ${{ steps.meta.outputs.tags }}
132+ labels : ${{ steps.meta.outputs.labels }}
0 commit comments