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