GitHub Actions自动构建Docker镜像并部署到ShinyProxy?
解读
国内企业在数字化转型过程中,越来越多地把 R 模型包装成在线服务。ShinyProxy 作为轻量级、开源的容器化 Shiny 托管方案,天然适合私有化部署。面试官抛出“GitHub Actions → Docker → ShinyProxy”这一整条链路,核心想验证三点:
- 你是否能把 R 代码 reproducible 地打包成镜像(国内网络、镜像源、私有依赖、许可证合规)。
- 你是否熟悉 GitHub Actions 在国内的“可用性”与“加速”套路(缓存、代理、镜像仓库)。
- 你是否理解 ShinyProxy 的部署模型(容器发现、认证、SSL、灰度、回滚),并能与 CI/CD 打通。
回答时要体现“工程落地”而非“跑通 demo”——镜像体积、构建时长、安全扫描、多环境配置、回滚策略,一个都不能少。
知识点
- R 可重复环境:renv、RSPM 国内镜像、Bioconductor 中国镜像、私有 Git 包。
- Dockerfile 最佳实践:多阶段构建、RSPM 二进制安装、系统依赖缓存、非 root 用户、健康检查。
- GitHub Actions 国内优化:actions 缓存、self-hosted runner(华为云/阿里云 ECS)、ghcr.io → 阿里云 ACR/腾讯云 TCR 同步。
- 镜像安全:trivy 扫描、镜像签名(cosign)、最小权限 ServiceAccount。
- ShinyProxy 配置:application.yml、容器标签、redis 会话、Keycloak 认证、HTTPS 终止(nginx-ingress)、滚动升级。
- 灰度与回滚:双标签(prod / staging)、镜像版本号与 Git tag 一致、ShinyProxy container-spec 动态拉取。
答案
整体思路:一次 Git push 触发 Actions,流水线分四条并行路径——lint→test→build→deploy,最终把镜像推到国内镜像仓库并滚动更新 ShinyProxy。
- 项目结构
myapp/
├── .github/workflows/cd.yml
├── Dockerfile
├── renv.lock
├── app.R
├── data/
├── tests/
├── nginx.conf
└── shinyproxy/
└── application.yml
- Dockerfile(国内加速、多阶段、非 root)
# 阶段1:依赖安装
FROM rocker/r-ver:4.4.0 AS builder
ENV RSPM="https://packagemanager.rstudio.com/cran/__linux__/jammy/2024-05-01"
RUN echo "options(repos = c(CRAN = '${RSPM}'), download.file.method = 'libcurl')" >> ${R_HOME}/etc/Rprofile.site
COPY renv.lock renv.lock
RUN R -e "install.packages('renv'); renv::restore()"
# 阶段2:运行镜像
FROM rocker/r-ver:4.4.0
LABEL maintainer="xxx@company.com"
ENV RSPM="https://packagemanager.rstudio.com/cran/__linux__/jammy/2024-05-01"
RUN echo "options(repos = c(CRAN = '${RSPM}'))" >> ${R_HOME}/etc/Rprofile.site
COPY --from=builder /usr/local/lib/R/site-library /usr/local/lib/R/site-library
COPY app.R /app/
EXPOSE 3838
USER rstudio
HEALTHCHECK CMD R -e "httr::GET('http://localhost:3838')$status_code == 200" || exit 1
CMD ["R", "-e", "shiny::runApp('/app', host='0.0.0.0', port=3838)"]
- GitHub Actions(.github/workflows/cd.yml)
name: cd
on:
push:
branches: [main]
tags: ['v*']
env:
REGISTRY: registry.cn-hangzhou.aliyuncs.com
NAMESPACE: mycorp
IMAGE_NAME: shiny-demo
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: r-lib/actions/setup-r@v2
- name: Cache renv
uses: actions/cache@v4
with:
path: ~/Library/Application Support/renv
key: ${{ runner.os }}-renv-${{ hashFiles('renv.lock') }}
- run: R -e "renv::restore()"
- run: R -e "testthat::test_dir('tests')"
build:
needs: test
runs-on: ubuntu-latest
outputs:
image-tag: ${{ steps.meta.outputs.tags }}
steps:
- uses: actions/checkout@v4
- uses: docker/setup-buildx-action@v3
- uses: aliyun/acr-login-action@v1
with:
login-server: https://${{ env.REGISTRY }}
username: ${{ secrets.ACR_USERNAME }}
password: ${{ secrets.ACR_PASSWORD }}
- id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.NAMESPACE }}/${{ env.IMAGE_NAME }}
tags: |
type=ref,event=branch
type=semver,pattern={{version}}
- uses: docker/build-push-action@v5
with:
context: .
push: true
tags: ${{ steps.meta.outputs.tags }}
cache-from: type=gha
cache-to: type=gha,mode=max
scan:
needs: build
runs-on: ubuntu-latest
steps:
- run: |
docker run --rm -v /var/run/docker.sock:/var/run/docker.sock \
aquasec/trivy image --severity HIGH,CRITICAL \
${{ needs.build.outputs.image-tag }}
deploy:
needs: [build, scan]
runs-on: self-hosted # 位于阿里云的 runner,已配置 kubectl
steps:
- uses: actions/checkout@v4
- name: Update ShinyProxy container-spec
run: |
TAG=${{ needs.build.outputs.image-tag }}
yq e -i ".spec.containers[0].image = \"$TAG\"" shinyproxy/app-patch.yaml
kubectl patch statefulset shiny-proxy -n shiny --type merge --patch-file shinyproxy/app-patch.yaml
kubectl rollout status statefulset/shiny-proxy -n shiny
- ShinyProxy 关键片段(application.yml)
proxy:
port: 8080
authentication: keycloak
keycloak:
realm: company
auth-server-url: https://keycloak.mycorp.com/auth
docker:
url: unix:///var/run/docker.sock
container-protocol: https
internal-networking: true
specs:
- id: shiny-demo
display-name: 销量预测模型
container-image: registry.cn-hangzhou.aliyuncs.com/mycorp/shiny-demo:latest
container-cmd: ["R", "-e", "shiny::runApp('/app', host='0.0.0.0', port=3838)"]
container-volumes:
- "/data/shiny/logs:/app/logs"
access-groups: ["data-scientist"]
- 灰度与回滚
- Git tag 与镜像 tag 一一对应,回滚只需
git revert后重新跑 Actions。 - ShinyProxy 支持多版本并存,通过
access-groups把新版本先开放给内部测试组,验证无误后修改application.yml把流量切到最新镜像,再删除旧容器。
拓展思考
- 国内网络二次加速:把 rocker 基础镜像同步到阿里云 ACR,构建时
FROM registry.cn-hangzhou.aliyuncs.com/rocker/r-ver:4.4.0,彻底消除 DockerHub 拉取超时。 - 多架构镜像:M 系列 Mac 与鲲鹏 ARM 服务器共存,利用
docker buildx --platform linux/amd64,linux/arm64一次性构建双架构,避免运维两套镜像。 - 零停机热更新:ShinyProxy 2.6+ 支持
container-spec动态加载,结合 Argo CD 做 GitOps,实现“声明式”发布,审计回溯更方便。 - 资源限额与弹性:在 K8s 侧给 Shiny 容器加
resources.limits.cpu/memory,并配置 HPA 根据并发会话数自动扩缩,防止大促期间单容器 OOM。 - 合规与许可证:CRAN 包若依赖 GPL-3,需检查公司政策是否允许对外发布服务;必要时用 RSPM 的“二进制但源码可得”模式,保留源码包备查。