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

如何在GitHub Actions中实现GitLab式手动触发任务流程

实现GitLab迁移到GitHub Actions的半自动化CI/CD流程

一、自动执行Linting与Artifact生成(Tag创建时触发)

创建.github/workflows/auto-lint-artifact.yml,配置推送新Tag时自动触发前置流程:

name: Auto Lint & Generate Artifact
on:
  push:
    tags:
      - 'v*' # 匹配v开头的版本Tag,可根据实际规则调整

jobs:
  lint-and-artifact:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4

      - name: Run Linting
        run: |
          # 替换为你的项目Linting命令,如npm run lint、golangci-lint run等
          npm run lint

      - name: Build & Generate Artifact
        run: |
          # 替换为生成Artifact的实际命令,比如打包前端dist目录
          npm run build

      - name: Upload Artifact
        uses: actions/upload-artifact@v4
        with:
          name: app-dist-${{ github.ref_name }} # Artifact名称关联Tag,方便后续识别
          path: dist/ # 替换为你的Artifact存放目录
          retention-days: 30 # 按需设置Artifact保留天数

二、手动触发镜像构建与部署(后续步骤自动执行)

创建.github/workflows/manual-deploy.yml,配置手动触发的部署流程,关联对应Tag的Artifact:

name: Manual Build Image & Deploy
on:
  workflow_dispatch:
    inputs:
      target_tag:
        description: '要部署的目标Tag(需已完成Lint与Artifact生成)'
        required: true
        type: string

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Checkout code
        uses: actions/checkout@v4
        with:
          ref: ${{ inputs.target_tag }} # 切换到指定Tag的代码版本

      - name: Download Artifact
        uses: actions/download-artifact@v4
        with:
          name: app-dist-${{ inputs.target_tag }} # 下载对应Tag的Artifact
          path: dist/

      - name: Build Docker Image
        run: |
          # 替换为你的镜像构建命令,示例如下
          docker build -t your-image-repo/app:${{ inputs.target_tag }} -f Dockerfile .
          # 若需推送到镜像仓库,添加登录与推送步骤
          echo "${{ secrets.DOCKER_PASSWORD }}" | docker login -u "${{ secrets.DOCKER_USERNAME }}" --password-stdin
          docker push your-image-repo/app:${{ inputs.target_tag }}

      - name: Deploy to Production
        run: |
          # 替换为你的实际部署命令,如kubectl更新、SSH脚本部署等
          kubectl set image deployment/app-deployment app=your-image-repo/app:${{ inputs.target_tag }}

      # 后续自动执行的步骤可继续添加,比如健康检查、通知等
      - name: Post-deployment Health Check
        run: |
          curl -f https://your-app-domain/healthz

流程说明

  1. 推送新Tag(如v1.0.0)时,第一个Workflow自动启动,完成Linting检查并生成、上传关联Tag的Artifact。
  2. 待第一个Workflow执行完成后,进入GitHub仓库「Actions」页面,找到Manual Build Image & Deploy工作流,点击「Run workflow」,输入对应的Tag名称触发手动执行。
  3. 手动触发的Workflow会自动下载对应Tag的Artifact,依次执行镜像构建、部署以及后续配置的所有自动步骤。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.28 14:47:27