GitHub工作流程 – 在工作流程中间添加手动步骤

huangapple go评论113阅读模式
英文:

Github Workflows - Add manual step in the middle of the workflow

问题

这是我的工作流程的样子:

它并行运行单元测试和集成测试,如果两者都通过,就会进行分阶段和生产部署。

我想知道是否有一种方法只在部署阶段添加手动步骤。我不想每次都部署到分阶段。

有没有办法实现这个目标?

test.yml

name: 测试、构建

on:
  workflow_call:

jobs:
  all-tests:
    name: 所有测试
    runs-on: [self-hosted]
    services:
      postgres:
        image: postgres:11.5-alpine
        env:
          POSTGRES_USER: ''
          POSTGRES_PASSWORD: ''
          POSTGRES_DB: ''
        ports:
          - 5445:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
      - name: 检出仓库
        uses: actions/checkout@master
      - name: 运行迁移
        env:
          ENVIRONMENT: 'test'
          DATABASE_URL: ''
        run: ./gradlew flywayMigrate
      - name: 集成测试
        env:
          ENVIRONMENT: 'test'
        run: ./gradlew test

deploy.yml

name: 部署

on:
  push:
    branches:
      - main

jobs:
  test:
    uses: ./.github/workflows/test.yml
  deploy-staging:
    name: 部署到分阶段
    runs-on: [self-hosted]
    needs: [test]
    steps:
      - name: 检出仓库
        uses: actions/checkout@master
      - name: 迁移
        env:
          ENVIRONMENT: 'staging'
          DATABASE_URL: ${{ secrets.DATABASE_URL_STAGING_MIGRATION }}
        run: ./gradlew flywayMigrate
      - name: 认证
        with:
          credentials_json: '${{ secrets.GCP_SA_KEY_STAGING }}'
        uses: 'google-github-actions/auth@v0'
      - name: 打印配置文件
        env:
          ENVIRONMENT: 'staging'
        run: ./gradlew appengineShowConfiguration
      - name: 部署到分阶段
        env:
          ENVIRONMENT: 'staging'
          DATABASE_URL: ${{ secrets.DATABASE_URL_STAGING }}
        run: ./gradlew appengineDeploy
  deploy-prod:
    name: 部署到生产环境
    runs-on: [self-hosted]
    needs: [test]
    steps:
      - name: 检出仓库
        uses: actions/checkout@master
      - name: 迁移
        env:
          ENVIRONMENT: 'production'
          DATABASE_URL: ${{ secrets.DATABASE_URL_MIGRATION }}
        run: ./gradlew flywayMigrate
      - name: 认证
        with:
          credentials_json: '${{ secrets.GCP_SA_KEY }}'
        uses: 'google-github-actions/auth@v0'
      - name: 打印配置文件
        env:
          ENVIRONMENT: 'production'
        run: ./gradlew appengineShowConfiguration
      - name: 部署
        env:
          ENVIRONMENT: 'production'
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
        run: ./gradlew appengineDeploy

GitHub工作流程 – 在工作流程中间添加手动步骤

英文:

This is how my Worfklow looks like right now:

It runs unit and integration tests in parallel, and if both passes, the Staging and Production deploy run.

I'm wondering if there's a way to add a manual step only to the deploy step. I don't want to deploy to Staging every single time.

Is there any way of achieving it?

test.yml

name: Test, Build

on:
  workflow_call:

jobs:
  all-tests:
    name: All Tests
    runs-on: [self-hosted]
    services:
      postgres:
        image: postgres:11.5-alpine
        env:
          POSTGRES_USER: ''
          POSTGRES_PASSWORD: ''
          POSTGRES_DB: ''
        ports:
          - 5445:5432
        options: --health-cmd pg_isready --health-interval 10s --health-timeout 5s --health-retries 5
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master
      - name: Run migrations
        env:
          ENVIRONMENT: 'test'
          DATABASE_URL: ''
        run: ./gradlew flywayMigrate
      - name: Integration Test
        env:
          ENVIRONMENT: 'test'
        run: ./gradlew test

deploy.yml

name: Deploy

on:
  push:
    branches:
      - main

jobs:
  test:
    uses: ./.github/workflows/test.yml
  deploy-staging:
    name: Deploy Staging
    runs-on: [self-hosted]
    needs: [test]
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master
      - name: Migrations
        env:
          ENVIRONMENT: 'staging'
          DATABASE_URL: ${{ secrets.DATABASE_URL_STAGING_MIGRATION }}
        run: ./gradlew flywayMigrate
      - name: Authenticate
        with:
          credentials_json: '${{ secrets.GCP_SA_KEY_STAGING }}'
        uses: 'google-github-actions/auth@v0'
      - name: Print Config Files
        env:
          ENVIRONMENT: 'staging'
        run: ./gradlew appengineShowConfiguration
      - name: Stage Deploy
        env:
          ENVIRONMENT: 'staging'
          DATABASE_URL: ${{ secrets.DATABASE_URL_STAGING }}
        run: ./gradlew appengineDeploy
  deploy-prod:
    name: Deploy Prod
    runs-on: [self-hosted]
    needs: [test]
    steps:
      - name: Checkout Repo
        uses: actions/checkout@master
      - name: Migrations
        env:
          ENVIRONMENT: 'production'
          DATABASE_URL: ${{ secrets.DATABASE_URL_MIGRATION }}
        run: ./gradlew flywayMigrate
      - name: Authenticate
        with:
          credentials_json: '${{ secrets.GCP_SA_KEY }}'
        uses: 'google-github-actions/auth@v0'
      - name: Print Config Files
        env:
          ENVIRONMENT: 'production'
        run: ./gradlew appengineShowConfiguration
      - name: Deploy
        env:
          ENVIRONMENT: 'production'
          DATABASE_URL: ${{ secrets.DATABASE_URL }}
        run: ./gradlew appengineDeploy

GitHub工作流程 – 在工作流程中间添加手动步骤

答案1

得分: 1

For manual deploys you need workflow dispatch event.


jobs:
  deploy-staging:
    name: Deploy Staging
    runs-on: ubuntu-latest
    steps:
      - name: Deploy
        run: <your_staging_deploy_command>

Then you need to manually run a workflow using the Github interface.

英文:

For manual deploys you need workflow dispatch event.

on: workflow_dispatch

jobs:
  deploy-staging:
    name: Deploy Staging
    runs-on: ubuntu-latest
    steps:
      - name: Deploy
        run: <your_staging_deploy_command>

Then you need to manually run a workflow using Github interface.

huangapple
  • 本文由 发表于 2023年5月17日 07:31:27
  • 转载请务必保留本文链接:https://go.coder-hub.com/76267704.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定