@@ -3,18 +3,38 @@ name: Docker Build & Publish
33on :
44 push :
55 branches : [main]
6+ pull_request :
7+ branches : [main]
68 workflow_dispatch :
79
810env :
911 REGISTRY : ghcr.io
1012 IMAGE_NAME : ${{ github.repository }}
1113
1214jobs :
13- build :
14- name : Build and Push Docker Image
15- runs-on : ubuntu-latest
15+ meta :
16+ name : Prepare metadata
17+ runs-on : ubuntu-24.04
18+ outputs :
19+ datetime : ${{ steps.datetime.outputs.datetime }}
20+ steps :
21+ - name : Get current date and time
22+ id : datetime
23+ run : echo "datetime=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT
24+
25+ image-build :
26+ name : Build and Push (${{ matrix.arch }})
27+ needs : meta
28+ strategy :
29+ matrix :
30+ include :
31+ - arch : amd64
32+ - arch : arm64
33+ runs-on : ubuntu-24.04-arm
34+ runs-on : ${{ matrix.runs-on || 'ubuntu-24.04' }}
1635 permissions :
1736 contents : read
37+ actions : write
1838 packages : write
1939
2040 steps :
2343 with :
2444 fetch-depth : 0
2545
26- - name : Get current date and time
27- id : datetime
28- run : echo "datetime=$(date +'%Y%m%d%H%M%S')" >> $GITHUB_OUTPUT
29-
30- - name : Set up QEMU
31- uses : docker/setup-qemu-action@v3
32-
3346 - name : Set up Docker Buildx 🚀
3447 uses : docker/setup-buildx-action@v3
3548
@@ -46,44 +59,119 @@ jobs:
4659 with :
4760 images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
4861 tags : |
49- type=raw,value=${{ steps.datetime.outputs.datetime }}
62+ type=raw,value=${{ needs.meta.outputs.datetime }}
63+ type=raw,value=latest
5064 type=sha
5165 type=ref,event=branch
5266 type=ref,event=tag
5367 flavor : |
54- latest=true
68+ suffix=-${{ matrix.arch }}
5569
5670 - name : Build and push 🏗️
5771 uses : docker/build-push-action@v5
5872 with :
5973 context : .
6074 file : ./Dockerfile
61- platforms : linux/amd64
62- push : true
75+ platforms : linux/${{ matrix.arch }}
76+ push : ${{ github.event_name != 'pull_request' }}
6377 tags : ${{ steps.meta.outputs.tags }}
6478 labels : ${{ steps.meta.outputs.labels }}
79+ cache-from : type=gha,scope=${{ matrix.arch }}
80+ cache-to : type=gha,mode=max,scope=${{ matrix.arch }}
6581 build-args : |
6682 NEXT_PUBLIC_APP_URL=${{ secrets.NEXT_PUBLIC_APP_URL }}
6783 NEXT_PUBLIC_OPEN_SOURCE_URL=${{ secrets.NEXT_PUBLIC_OPEN_SOURCE_URL }}
6884 NEXT_PUBLIC_DEFAULT_LOCALE=${{ secrets.NEXT_PUBLIC_DEFAULT_LOCALE }}
6985
70- outputs :
71- tags : ${{ steps.datetime.outputs.datetime }}
72-
73- update-image :
74- needs : build
75- runs-on : ubuntu-latest
76- if : github.repository == 'labring/sealos.io'
86+ merge :
87+ name : Create multi-arch manifest
88+ needs : [meta, image-build]
89+ if : ${{ github.event_name != 'pull_request' }}
90+ runs-on : ubuntu-24.04
91+ permissions :
92+ contents : read
93+ packages : write
7794 steps :
78- - name : Checkout code
79- uses : actions/checkout @v3
80- - uses : actions-hub/kubectl@master
81- env :
82- KUBE_CONFIG : ${{ secrets.KUBE_CONFIG }}
95+ - name : Set up Docker Buildx 🚀
96+ uses : docker/setup-buildx-action @v3
97+
98+ - name : Login to GitHub Container Registry 🚢
99+ uses : docker/login-action@v3
83100 with :
84- args : set image deployment/sealos-docs sealos-docs=ghcr.io/labring/sealos.io:${{ needs.build.outputs.tags }}
85- - uses : actions-hub/kubectl@master
86- env :
87- KUBE_CONFIG : ${{ secrets.KUBE_CONFIG }}
101+ registry : ${{ env.REGISTRY }}
102+ username : ${{ github.actor }}
103+ password : ${{ secrets.GITHUB_TOKEN }}
104+
105+ - name : Extract metadata (tags, labels) for Docker
106+ id : meta
107+ uses : docker/metadata-action@v5
88108 with :
89- args : annotate deployment/sealos-docs originImageName="ghcr.io/labring/sealos.io:${{ needs.build.outputs.tags }}" --overwrite
109+ images : ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}
110+ tags : |
111+ type=raw,value=${{ needs.meta.outputs.datetime }}
112+ type=sha
113+ type=ref,event=branch
114+ type=ref,event=tag
115+ flavor : |
116+ latest=true
117+
118+ - name : Create manifest list
119+ run : |
120+ tags="${{ steps.meta.outputs.tags }}"
121+ for tag in $tags; do
122+ docker buildx imagetools create -t "$tag" "${tag}-amd64" "${tag}-arm64"
123+ done
124+ docker buildx imagetools inspect "$tag"
125+
126+ deploy :
127+ name : Deploy to cluster
128+ needs : [meta, merge]
129+ if : ${{ github.event_name != 'pull_request' }}
130+ runs-on : ubuntu-24.04
131+ permissions :
132+ contents : read
133+ env :
134+ DEPLOY_NAMESPACE : ${{ vars.DEPLOY_NAMESPACE }}
135+ DEPLOYMENT_NAME : ${{ vars.DEPLOYMENT_NAME }}
136+ DEPLOYMENT_CONTAINER : ${{ vars.DEPLOYMENT_CONTAINER }}
137+ DEPLOY_IMAGE_TAG : ${{ vars.DEPLOY_IMAGE_TAG }}
138+ steps :
139+ - name : Set up kubectl
140+ uses : azure/setup-kubectl@v4
141+
142+ - name : Configure kubeconfig
143+ env :
144+ KUBE_BASE64 : ${{ secrets.KUBE_BASE64 }}
145+ run : |
146+ if [ -z "${KUBE_BASE64}" ]; then
147+ echo "KUBE_BASE64 is required." >&2
148+ exit 1
149+ fi
150+ mkdir -p ~/.kube
151+ echo "${KUBE_BASE64}" | base64 -d > ~/.kube/config
152+
153+ - name : Validate deployment settings
154+ run : |
155+ if [ -z "${DEPLOY_NAMESPACE}" ]; then
156+ echo "DEPLOY_NAMESPACE is required (repo variable)." >&2
157+ exit 1
158+ fi
159+ if [ -z "${DEPLOYMENT_NAME}" ]; then
160+ echo "DEPLOYMENT_NAME is required (repo variable)." >&2
161+ exit 1
162+ fi
163+ if [ -z "${DEPLOYMENT_CONTAINER}" ]; then
164+ echo "DEPLOYMENT_CONTAINER is required (repo variable)." >&2
165+ exit 1
166+ fi
167+
168+ - name : Update deployment image
169+ run : |
170+ IMAGE_TAG="${DEPLOY_IMAGE_TAG}"
171+ if [ -z "$IMAGE_TAG" ]; then
172+ IMAGE_TAG="${{ needs.meta.outputs.datetime }}"
173+ fi
174+ IMAGE="${REGISTRY}/${IMAGE_NAME}:${IMAGE_TAG}"
175+ echo "Deploying ${IMAGE} to ${DEPLOYMENT_NAME} (${DEPLOYMENT_CONTAINER}) in ${DEPLOY_NAMESPACE}"
176+ kubectl -n "${DEPLOY_NAMESPACE}" set image "deployment/${DEPLOYMENT_NAME}" "${DEPLOYMENT_CONTAINER}=${IMAGE}"
177+ kubectl -n "${DEPLOY_NAMESPACE}" rollout status "deployment/${DEPLOYMENT_NAME}"
0 commit comments