You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在不使用第三方Action的GitHub工作流间共享构件信息?

方案1:通过GitHub构件共享txt文件(兼容原有流程)

构建工作流配置

构建工作流完成镜像推送后,生成txt文件并上传为构件,还可自动触发部署工作流:

name: 后端镜像构建
on:
  push:
    branches: [master, develop]

permissions:
  actions: write # 赋予触发其他工作流的权限

jobs:
  build-image:
    runs-on: ubuntu-latest
    steps:
      - name: 拉取代码
        uses: actions/checkout@v4

      # 省略镜像构建、推送到JFrog的步骤...

      - name: 生成镜像信息文件
        run: echo ${docker_image_name_with_tag} > docker_image_name_backend.txt

      - name: 上传构件
        uses: actions/upload-artifact@v4
        with:
          name: docker-image-backend
          path: docker_image_name_backend.txt
          retention-days: 7 # 根据需求设置构件保留天数

      # 自动触发部署工作流
      - name: 触发部署流程
        uses: actions/github-script@v7
        with:
          script: |
            github.rest.actions.createWorkflowDispatch({
              owner: context.repo.owner,
              repo: context.repo.repo,
              workflow_id: 'deploy-backend.yml',
              ref: context.ref,
              inputs: {
                build_run_id: context.runId
              }
            })

部署工作流配置

部署工作流接收构建Run ID,下载对应构件并读取内容用于K8s部署:

name: 后端服务部署
on:
  workflow_dispatch:
    inputs:
      build_run_id:
        description: 构建工作流的Run ID
        required: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: 下载构建构件
        uses: actions/download-artifact@v4
        with:
          name: docker-image-backend
          run-id: ${{ github.event.inputs.build_run_id }}
          path: ./

      - name: 读取镜像信息
        id: get-image
        run: echo "image_name=$(cat docker_image_name_backend.txt)" >> $GITHUB_OUTPUT

      # 省略K8s集群认证步骤...

      - name: 部署到K8s
        run: kubectl apply -f deployment.yml --set image=${{ steps.get-image.outputs.image_name }}
方案2:直接传递镜像参数(无需txt文件)

跳过文件生成环节,构建工作流直接把镜像URL和标签作为参数传递给部署工作流:

构建工作流配置

name: 后端镜像构建
on:
  push:
    branches: [master, develop]

permissions:
  actions: write

jobs:
  build-image:
    runs-on: ubuntu-latest
    steps:
      - name: 拉取代码
        uses: actions/checkout@v4

      # 省略镜像构建、推送到JFrog的步骤...

      - name: 设置镜像信息输出
        id: set-image
        run: echo "docker_image_tag=${docker_image_name_with_tag}" >> $GITHUB_OUTPUT

      - name: 触发部署流程
        uses: actions/github-script@v7
        with:
          script: |
            github.rest.actions.createWorkflowDispatch({
              owner: context.repo.owner,
              repo: context.repo.repo,
              workflow_id: 'deploy-backend.yml',
              ref: context.ref,
              inputs: {
                docker_image_tag: '${{ steps.set-image.outputs.docker_image_tag }}'
              }
            })

部署工作流配置

name: 后端服务部署
on:
  workflow_dispatch:
    inputs:
      docker_image_tag:
        description: JFrog镜像完整URL和标签
        required: true

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      # 省略K8s集群认证步骤...

      - name: 部署到K8s
        run: kubectl apply -f deployment.yml --set image=${{ github.event.inputs.docker_image_tag }}
补充说明
  • 两种方案仅使用GitHub官方Action,符合不使用第三方Action的限制。
  • 若无需自动触发,部署工作流可手动启动,手动输入构建Run ID(方案1)或镜像标签(方案2)。

内容的提问来源于stack exchange,提问作者sergei

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.11 07:11:13