Skip to content

Commit

Permalink
feat: Support building docker images with GitHub Action (#27)
Browse files Browse the repository at this point in the history
  • Loading branch information
CH3CHO authored Aug 19, 2023
1 parent ffdee88 commit f7ae26a
Show file tree
Hide file tree
Showing 5 changed files with 163 additions and 4 deletions.
72 changes: 72 additions & 0 deletions .github/workflows/build-apiserver-image-and-push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Build API Server Image and Push to Image Registry

on:
push:
tags:
- "apiserver-v*.*.*"
workflow_dispatch: ~

jobs:
build-apiserver-image:
runs-on: ubuntu-latest
env:
IMAGE_REGISTRY: ${{ vars.IMAGE_REGISTRY || 'higress-registry.cn-hangzhou.cr.aliyuncs.com' }}
IMAGE_NAME: ${{ vars.API_SERVER_IMAGE_NAME || 'higress/api-server' }}
steps:
- name: "Checkout ${{ github.ref }}"
uses: actions/checkout@v3
with:
fetch-depth: 1

- name: "Setup Go"
uses: actions/setup-go@v3
with:
go-version: 1.20

- name: Setup Golang Caches
uses: actions/cache@v3
with:
path: |-
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ github.run_id }}
restore-keys: ${{ runner.os }}-go

- name: Calculate Docker metadata
id: docker-meta
uses: docker/metadata-action@v4
with:
images: |
${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha
type=match,pattern=apiserver-v(.*),group=1
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
- name: Login to Docker Registry
uses: docker/login-action@v2
with:
registry: ${{ vars.IMAGE_REGISTRY }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build Docker Image and Push
run: |
cd src/apiserver/
readarray -t IMAGES <<< "${{ steps.docker-meta.outputs.tags }}"
baseImage=""
for image in ${IMAGES[@]}; do
echo "Image: $image"
if [ -z "$baseImage" ]; then
GOPROXY="https://proxy.golang.org,direct" IMG="${image}" make docker-buildx-push
baseImage="$image"
else
docker buildx imagetools create "$baseImage" --tag "$image"
fi
done
72 changes: 72 additions & 0 deletions .github/workflows/build-runner-image-and-push.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
name: Build Runner Image and Push to Image Registry

on:
push:
tags:
- "runner-v*.*.*"
workflow_dispatch: ~

jobs:
build-runner-image:
runs-on: ubuntu-latest
env:
IMAGE_REGISTRY: ${{ vars.IMAGE_REGISTRY || 'higress-registry.cn-hangzhou.cr.aliyuncs.com' }}
IMAGE_NAME: ${{ vars.RUNNER_IMAGE_NAME || 'higress/runner' }}
steps:
- name: "Checkout ${{ github.ref }}"
uses: actions/checkout@v3
with:
fetch-depth: 1

- name: "Setup Go"
uses: actions/setup-go@v3
with:
go-version: 1.20

- name: Setup Golang Caches
uses: actions/cache@v3
with:
path: |-
~/.cache/go-build
~/go/pkg/mod
key: ${{ runner.os }}-go-${{ github.run_id }}
restore-keys: ${{ runner.os }}-go

- name: Calculate Docker metadata
id: docker-meta
uses: docker/metadata-action@v4
with:
images: |
${{ env.IMAGE_REGISTRY }}/${{ env.IMAGE_NAME }}
tags: |
type=sha
type=match,pattern=runner-v(.*),group=1
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'main') }}
- name: Login to Docker Registry
uses: docker/login-action@v2
with:
registry: ${{ vars.IMAGE_REGISTRY }}
username: ${{ secrets.REGISTRY_USERNAME }}
password: ${{ secrets.REGISTRY_PASSWORD }}

- name: Set up QEMU
uses: docker/setup-qemu-action@v2

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v2

- name: Build Docker Image and Push
run: |
cd src/runner/
readarray -t IMAGES <<< "${{ steps.docker-meta.outputs.tags }}"
baseImage=""
for image in ${IMAGES[@]}; do
echo "Image: $image"
if [ -z "$baseImage" ]; then
GOPROXY="https://proxy.golang.org,direct" IMG="${image}" make docker-buildx-push
baseImage="$image"
else
docker buildx imagetools create "$baseImage" --tag "$image"
fi
done
2 changes: 1 addition & 1 deletion .github/workflows/run-tests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ jobs:
test:
runs-on: ubuntu-latest
steps:
- name: "checkout ${{ github.ref }}"
- name: "Checkout ${{ github.ref }}"
uses: actions/checkout@v3
with:
fetch-depth: 1
Expand Down
9 changes: 8 additions & 1 deletion src/apiserver/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,14 @@ IMAGE_TAG = $(if $(strip $(IMAGE_VERSION)),${IMAGE_VERSION},${BUILD_TIME}-${COMM
IMG ?= ${REGISTRY}${IMAGE_NAME}:${IMAGE_TAG}

.DEFAULT:
build:
docker-build:
docker build \
-t ${IMG} \
.
@echo ""
@echo "Image: ${IMG}"

docker-buildx-push:
docker buildx build --no-cache \
--platform linux/amd64,linux/arm64 \
-t ${IMG} \
Expand Down
12 changes: 10 additions & 2 deletions src/runner/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,16 @@ IMG ?= ${REGISTRY}${IMAGE_NAME}:${IMAGE_TAG}
YQ_VERSION ?= 4.34.2

.DEFAULT:
build:
DOCKER_BUILDKIT=1 docker buildx build --no-cache \
docker-build:
docker build \
--build-arg YQ_VERSION=${YQ_VERSION} \
-t ${IMG} \
.
@echo ""
@echo "Image: ${IMG}"

docker-buildx-push:
docker buildx build --no-cache \
--build-arg YQ_VERSION=${YQ_VERSION} \
--platform linux/amd64,linux/arm64 \
-t ${IMG} \
Expand Down

0 comments on commit f7ae26a

Please sign in to comment.