CircleCI构建的胖JAR部署到Azure App Service后体积缩小问题
问题描述
我用Gradle构建Java Spark Web服务,通过CircleCI部署到Azure App Service。CircleCI构建后用ls确认JAR是包含所有依赖的胖包(约7.4MB),但部署后SSH到App Service主机,发现/home/site/wwwroot下的JAR只有10KB左右,仅包含自己的class文件,没有任何依赖。
CircleCI配置
version: 2.1 orbs: gradle: circleci/gradle@3.0.0 jobs: build: docker: - image: circleci/openjdk:11-jdk working_directory: ~/repo steps: - checkout - restore_cache: keys: - gradle-dependencies-{{ checksum "build.gradle" }} - gradle-dependencies- - run: gradle dependencies - save_cache: paths: - ~/.gradle key: gradle-dependencies-{{ checksum "build.gradle" }} - run: gradle test - run: name: "Build and Package JAR" command: | ./gradlew customFatJar ls -rlt ~/repo/build/libs/app.jar - persist_to_workspace: root: ~/repo paths: - build deploy: docker: - image: circleci/python:3.7 steps: - checkout - run: name: Install azure-cli command: | curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash - run: name: Login to Azure command: | az login --service-principal -u $AZURE_CLIENT_ID -p $APP_SECRET --tenant $AZURE_TENANT_ID - attach_workspace: at: ~/repo - run: name: Deploy to Azure App Service command: | az webapp up --sku F1 -n 'CikService' -l 'East US' --runtime 'JAVA:11-java11' --resource-group 'Cik' -p 'P1v2' --os-type 'Linux' - run: name: Deploy Code command: | ls -rlt ~/repo/build/libs/app.jar az webapp deployment source config-zip -g 'Cik' -n 'CikService' --src ~/repo/build/libs/app.jar workflows: version: 2 build-deploy: jobs: - build - deploy: requires: - build filters: branches: only: main
CircleCI构建日志(关键部分)
#!/bin/bash -eo pipefail ls -rlt ~/repo/build/libs/app.jar az webapp deployment source config-zip -g 'Cik' -n 'CikService' --src ~/repo/build/libs/app.jar -rw-r--r-- 1 circleci circleci 7401147 Jun 29 08:03 /home/circleci/repo/build/libs/app.jar Getting scm site credentials for zip deployment Starting zip deployment. This operation can take a while to complete ... Deployment endpoint responded with status code 202 { "active": true, "author": "N/A", "author_email": "N/A", "build_summary": { "errors": [], "warnings": [] }, "complete": true, "deployer": "Push-Deployer", "end_time": "2023-06-29T08:06:30.6786105Z", "id": "ec11ce4a-71d7-42b2-836f-f01c74015916", "is_readonly": true, "is_temp": false, "last_success_end_time": "2023-06-29T08:06:30.6786105Z", "log_url": "https://cikservice.scm.azurewebsites.net/api/deployments/latest/log", "message": "Created via a push deployment", "progress": "", "received_time": "2023-06-29T08:05:22.3944098Z", "site_name": "cikservice", "start_time": "2023-06-29T08:05:23.9412019Z", "status": 4, "status_text": "", "url": "https://cikservice.scm.azurewebsites.net/api/deployments/latest" } CircleCI received exit code 0
App Service主机上的JAR信息
d8af2b6b9e31:/home# ls -rlt /home/site/wwwroot/app.jar -rwxrwxrwx 1 nobody nobody 10562 Jun 28 06:49 /home/site/wwwroot/app.jar d8af2b6b9e31:/home# d8af2b6b9e31:/home# jar -tf /home/site/wwwroot/app.jar Picked up JAVA_TOOL_OPTIONS: -Xmx2300M -Djava.net.preferIPv4Stack=true META-INF/ META-INF/MANIFEST.MF org/ org/example/ org/example/repository/ org/example/repository/CikRepository.class org/example/repository/SECRepository.class org/example/enumeration/ org/example/enumeration/ReportType.class org/example/http/ org/example/http/HttpHelper.class org/example/external/ org/example/external/ExternalUrl.class org/example/Main.class log4j2.xml d8af2b6b9e31:/home#
问题原因及解决方案
核心原因
部署阶段执行的az webapp up命令会自动从当前checkout的代码目录重新构建并部署应用,覆盖了你通过az webapp deployment source config-zip上传的胖JAR。
Azure的内置构建服务只会生成仅包含你的业务代码的瘦JAR,不会打包第三方依赖,这就导致最终服务器上的JAR是瘦包而非你在CircleCI中构建的胖包。
解决方案
方案1:移除az webapp up命令(推荐)
az webapp up主要用于首次快速创建并初始化应用,在持续部署流程中无需重复执行。直接删除部署阶段的Deploy to Azure App Service步骤,仅保留上传胖JAR的步骤即可:
修改后的deploy任务:
deploy: docker: - image: circleci/python:3.7 steps: - checkout - run: name: Install azure-cli command: | curl -sL https://aka.ms/InstallAzureCLIDeb | sudo bash - run: name: Login to Azure command: | az login --service-principal -u $AZURE_CLIENT_ID -p $APP_SECRET --tenant $AZURE_TENANT_ID - attach_workspace: at: ~/repo - run: name: Deploy Code command: | ls -rlt ~/repo/build/libs/app.jar az webapp deployment source config-zip -g 'Cik' -n 'CikService' --src ~/repo/build/libs/app.jar
方案2:修改az webapp up行为(不推荐)
若必须保留az webapp up,可添加--no-build参数跳过自动构建,但需确保胖JAR已放置在checkout后的目录中,这种方式不如方案1简洁。
额外验证建议
- 在部署步骤中添加命令验证胖JAR内容,比如执行
jar -tf ~/repo/build/libs/app.jar | grep spark,确认依赖存在; - 部署后通过Azure提供的
log_url查看部署日志,确认上传的胖JAR被正确部署。
内容的提问来源于stack exchange,提问作者thalaivva
相关产品推荐
相关产品推荐

