1+ name : Build + Push + Deploy
2+
3+ env :
4+ DOCKER_REGISTRY_NAME : ${{ secrets.DOCKER_REGISTRY_NAME }}
5+ DOCKER_REGISTRY_IMAGE : ${{ secrets.DOCKER_REGISTRY_NAME }}/codesark/codesark-portfolio-next
6+
7+ on :
8+ workflow_dispatch :
9+ push :
10+ branches :
11+ - main
12+ - dev
13+ tags :
14+ - v*
15+
16+ jobs :
17+ build :
18+ runs-on : ubuntu-latest
19+ strategy :
20+ fail-fast : false
21+ matrix :
22+ platform :
23+ - linux/amd64
24+ # - linux/arm/v6
25+ # - linux/arm/v7
26+ - linux/arm64
27+ steps :
28+ - name : Checkout
29+ uses : actions/checkout@v4
30+ - name : Configure Vars
31+ id : conf
32+ shell : bash
33+ run : |
34+ echo "branch=$(echo ${GITHUB_REF#refs/heads/})" >> $GITHUB_OUTPUT
35+ echo "sha_short=$(git rev-parse --short HEAD)" >> $GITHUB_OUTPUT
36+ - name : Docker meta
37+ id : meta
38+ uses : docker/metadata-action@v5
39+ with :
40+ images : ${{ env.DOCKER_REGISTRY_IMAGE }}
41+ tags : |
42+ type=raw,value=latest
43+ type=sha,
44+ type=raw,value=${{ steps.conf.outputs.branch }}
45+ type=raw,value=${{ steps.conf.outputs.branch }}-${{ steps.conf.outputs.sha_short }}
46+ - name : Set up QEMU
47+ uses : docker/setup-qemu-action@v3
48+ - name : Set up Docker Buildx
49+ uses : docker/setup-buildx-action@v3
50+ - name : Login to Docker Hub
51+ uses : docker/login-action@v3
52+ with :
53+ registry : ${{ env.DOCKER_REGISTRY_NAME }}
54+ username : ${{ secrets.DOCKER_REGISTRY_USERNAME }}
55+ password : ${{ secrets.DOCKER_REGISTRY_TOKEN }}
56+ - name : Build and push by digest
57+ id : build
58+ uses : docker/build-push-action@v5
59+ with :
60+ context : .
61+ platforms : ${{ matrix.platform }}
62+ labels : ${{ steps.meta.outputs.labels }}
63+ outputs : type=image,name=${{ env.DOCKER_REGISTRY_IMAGE }},push-by-digest=true,name-canonical=true,push=true
64+ cache-from : type=gha,scope=${{ github.workflow }}-${{ matrix.platform }}
65+ cache-to : type=gha,mode=max,scope=${{ github.workflow }}-${{ matrix.platform }}
66+ - name : Export digest
67+ run : |
68+ mkdir -p /tmp/digests
69+ digest="${{ steps.build.outputs.digest }}"
70+ touch "/tmp/digests/${digest#sha256:}"
71+ - name : Upload digest
72+ uses : actions/upload-artifact@v3
73+ with :
74+ name : digests
75+ path : /tmp/digests/*
76+ if-no-files-found : error
77+ retention-days : 1
78+ outputs :
79+ branch : ${{ steps.conf.outputs.branch }}
80+ sha_short : ${{ steps.conf.outputs.sha_short }}
81+ digest : ${{ steps.build.outputs.digest }}
82+
83+ push :
84+ runs-on : ubuntu-latest
85+ needs :
86+ - build
87+ steps :
88+ - name : Download digests
89+ uses : actions/download-artifact@v3
90+ with :
91+ name : digests
92+ path : /tmp/digests
93+ - name : Set up Docker Buildx
94+ uses : docker/setup-buildx-action@v3
95+ - name : Docker meta
96+ id : meta
97+ uses : docker/metadata-action@v5
98+ with :
99+ images : ${{ env.DOCKER_REGISTRY_IMAGE }}
100+ tags : |
101+ type=raw,value=latest
102+ type=sha,
103+ type=raw,value=${{ needs.build.outputs.branch }}
104+ type=raw,value=${{ needs.build.outputs.branch }}-${{ needs.build.outputs.sha_short }}
105+ - name : Login to Docker Hub
106+ uses : docker/login-action@v3
107+ with :
108+ registry : ${{ env.DOCKER_REGISTRY_NAME }}
109+ username : ${{ secrets.DOCKER_REGISTRY_USERNAME }}
110+ password : ${{ secrets.DOCKER_REGISTRY_TOKEN }}
111+ - name : Create manifest list and push
112+ working-directory : /tmp/digests
113+ run : |
114+ docker buildx imagetools create $(jq -cr '.tags | map("-t " + .) | join(" ")' <<< "$DOCKER_METADATA_OUTPUT_JSON") \
115+ $(printf '${{ env.DOCKER_REGISTRY_IMAGE }}@sha256:%s ' *)
116+ - name : Inspect image
117+ run : |
118+ docker buildx imagetools inspect ${{ env.DOCKER_REGISTRY_IMAGE }}:${{ steps.meta.outputs.version }}
119+ deploy :
120+ needs :
121+ - build
122+ - push
123+ runs-on : ubuntu-latest
124+ # only run if branch is main or commit have "release" tag
125+ if : ${{ needs.build.outputs.branch == 'main' || contains(github.event.head_commit.message, 'release') }}
126+ steps :
127+ - name : SSH Deploy
128+ 129+ env :
130+ IMAGE_WITH_TAG : ' ${{ env.DOCKER_REGISTRY_IMAGE }}:${{ needs.build.outputs.branch }}-${{ needs.build.outputs.sha_short }}'
131+ with :
132+ host : ${{ secrets.DEPLOYMENT_HOST }}
133+ username : ${{ secrets.DEPLOYMENT_USER }}
134+ key : ${{ secrets.DEPLOYMENT_KEY }}
135+ envs : IMAGE_WITH_TAG
136+ # script: |
137+ # kubectl set image deployment/codesark-portfolio \
138+ # codesark-portfolio=cr.skpd.io/codesark/codesark-portfolio-frontend:$IMAGE_TAG \
139+ # -n codesark
140+ script : |
141+ cd ~/compose-apps/codesark-portfolio && \
142+ rm -f compose.yml comopse.yaml && \
143+ nextappimage=$IMAGE_WITH_TAG envsubst '$nextappimage' < compose.template.yaml > compose.yaml && \
144+ cat compose.yaml && \
145+ docker compose pull && \
146+ docker compose up -d
0 commit comments