@@ -128,56 +128,86 @@ jobs:
128128 needs : build
129129 runs-on : ubuntu-latest
130130 steps :
131- - uses : actions/checkout@v3
131+ - uses : actions/checkout@v4
132132 with :
133133 fetch-depth : 0
134- - name : commit push
135- run : |
136- docker info
137- pwd
138- ls
134+
135+ - name : Use Node.js
136+ uses : actions/setup-node@v4
137+ with :
138+ node-version : ' 20'
139+ cache : ' yarn'
140+
141+ - name : Cache Nx
142+ uses : actions/cache@v4
143+ with :
144+ path : .nx
145+ key : ${{ runner.os }}-nx-${{ hashFiles('**/yarn.lock') }}
146+ restore-keys : |
147+ ${{ runner.os }}-nx-
148+
149+ - run : yarn install --frozen-lockfile
150+
139151 - name : docker login
140152141153 with :
142154 registry : registry.cn-hongkong.aliyuncs.com
143155 username : devops@1148299792416188
144156 password : ${{ secrets.REGISTRY_HONGKONG_SECRET }}
145- - name : Extract branch name
146- shell : bash
147- run : echo "BRANCH=${GITHUB_REF_NAME/\//_}" >> $GITHUB_OUTPUT
148- id : extract_branch
149- - name : Get current date
150- id : date
151- run : echo "TODAY=$(date +'%y%m%d_%H%M')" >> $GITHUB_OUTPUT
152- - name : Get commit
153- id : commitid
154- run : echo "COMMIT_ID=${GITHUB_SHA:0:7}" >> $GITHUB_OUTPUT
155- - name : image tag
156- id : tag
157- run : |
158- # if triggered by tag, use tag as image tag
159- if [[ $GITHUB_REF == refs/tags/* ]]; then
160- echo "TAG=${GITHUB_REF_NAME}" >> $GITHUB_OUTPUT
161- else
162- echo "TAG=${{ steps.extract_branch.outputs.BRANCH }}_${{ steps.date.outputs.TODAY }}_${{ steps.commitid.outputs.COMMIT_ID }}" >> $GITHUB_OUTPUT
163- fi
164- - name : env
157+
158+ - name : Get version from api/package.json
159+ id : get_version
165160 run : |
166- echo "VITE_API_BASE_URL=/devtable-api/" > website/.env
167- echo "VITE_WEBSITE_BASE_URL=/devtable/website/" >> website/.env
161+ VERSION=$(node -p "require('./api/package.json').version")
162+ echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
163+ echo "Using version: ${VERSION}"
164+
165+ - name : Check if API image version is already published
166+ id : check_api
167+ continue-on-error : true
168+ run : yarn exec nx run api:is-docker-published-api
169+
170+ - name : Check if Website image version is already published
171+ id : check_website
172+ continue-on-error : true
173+ run : yarn exec nx run api:is-docker-published-website
174+
168175 - name : Download artifacts
169176 uses : actions/download-artifact@v4
170177 with :
171178 name : dist
179+
180+ - name : env
181+ run : |
182+ echo "VITE_API_BASE_URL=/devtable-api/" > website/.env
183+ echo "VITE_WEBSITE_BASE_URL=/devtable/website/" >> website/.env
184+
172185 - name : Setup buildx
173186 run : docker buildx create --use
174- - name : Build table image
175- run : docker buildx build --platform linux/amd64,linux/arm64 -f Dockerfile-api -t registry.cn-hongkong.aliyuncs.com/merico/table:${{ steps.tag.outputs.TAG }} . --push
176- - name : Build table website image
177- run : docker buildx build --platform linux/amd64,linux/arm64 -f Dockerfile-website -t registry.cn-hongkong.aliyuncs.com/merico/table-website:${{ steps.tag.outputs.TAG }} . --push
187+
188+ # Build and push API image if version is new
189+ - name : Build and push API image
190+ if : steps.check_api.outcome == 'success'
191+ run : |
192+ docker buildx build --platform linux/amd64,linux/arm64 \
193+ -f Dockerfile-api \
194+ -t registry.cn-hongkong.aliyuncs.com/merico/table:${{ steps.get_version.outputs.VERSION }} \
195+ -t registry.cn-hongkong.aliyuncs.com/merico/table:latest \
196+ . --push
197+
198+ # Build and push Website image if version is new
199+ - name : Build and push Website image
200+ if : steps.check_website.outcome == 'success'
201+ run : |
202+ docker buildx build --platform linux/amd64,linux/arm64 \
203+ -f Dockerfile-website \
204+ -t registry.cn-hongkong.aliyuncs.com/merico/table-website:${{ steps.get_version.outputs.VERSION }} \
205+ -t registry.cn-hongkong.aliyuncs.com/merico/table-website:latest \
206+ . --push
207+
178208 - name : Trigger GitLab CI pipeline
179- # only runs if the previous step was successful
180- if : success()
209+ # Only trigger if at least one image was newly published
210+ if : steps.check_api.outcome == ' success' || steps.check_website.outcome == 'success'
181211 uses : eic/trigger-gitlab-ci@v3
182212 with :
183213 token : ${{ secrets.GITLAB_TOKEN }}
0 commit comments