如何在特定条件下触发GitHub可复用工作流
解决可复用单元测试工作流的触发条件问题
你现在的核心问题是:可复用工作流被workflow_call调用时,自身的on配置不会生效——触发逻辑完全由调用它的主工作流决定。要实现“仅在创建Tag或PR打开/向PR推送新提交时运行单元测试”,可以通过两种方式调整:
方案一:直接修改主工作流的触发条件
如果主工作流里的所有Job(包括单元测试、集成测试)都只需要在目标场景下运行,直接把主工作流的on限定为Tag推送和PR相关事件即可:
name: GitHub Actions Demo on: push: tags: ['*'] # 仅在推送Tag时触发 pull_request: types: [opened, synchronize, reopened] # PR打开、推送新提交、重新打开时触发 jobs: unit-test: name: Unit Tests uses: ./.github/workflows/run-unit-tests.yml with: java-version: '17' java-distribution: 'corretto' java-cache: 'maven' maven-image: 'maven:3-amazoncorretto-17' integration-test: name: Integration Tests uses: ./.github/workflows/run-integration-tests.yml with: java-version: '17' java-distribution: 'corretto' java-cache: 'maven' maven-image: 'maven:3-amazoncorretto-17' skip-unit-tests: true
方案二:给单元测试Job添加条件判断(保留主工作流原有触发)
如果主工作流里的其他Job(比如集成测试)需要在普通push时也运行,只给单元测试Job加if条件,限定它仅在目标场景下执行:
name: GitHub Actions Demo on: [push] jobs: unit-test: name: Unit Tests uses: ./.github/workflows/run-unit-tests.yml # 条件判断:要么是推送Tag,要么是PR的指定动作 if: > (github.event_name == 'push' && startsWith(github.ref, 'refs/tags/')) || (github.event_name == 'pull_request' && contains(fromJSON('["opened", "synchronize", "reopened"]'), github.event.action)) with: java-version: '17' java-distribution: 'corretto' java-cache: 'maven' maven-image: 'maven:3-amazoncorretto-17' integration-test: name: Integration Tests uses: ./.github/workflows/run-integration-tests.yml with: java-version: '17' java-distribution: 'corretto' java-cache: 'maven' maven-image: 'maven:3-amazoncorretto-17' skip-unit-tests: true
额外优化:清理可复用工作流的冗余配置
可复用工作流里的push和pull_request配置在被workflow_call调用时不会生效,建议删掉这些冗余内容,只保留workflow_call部分,避免混淆:
name: Reusable Unit Test env: MAVEN_OPTS: "-Dmaven.repo.local=.m2/repository -Dorg.slf4j.simpleLogger.log.org.apache.maven.cli.transfer.Slf4jMavenTransferListener=WARN -Dorg.slf4j.simpleLogger.showDateTime=true -Djava.awt.headless=true" MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version" MAVEN_ARTIFACT_GOALS: "test" MAVEN_GOALS: "clean org.jacoco:jacoco-maven-plugin:prepare-agent $MAVEN_ARTIFACT_GOALS" on: workflow_call: inputs: java-version: description: Java Version required: true type: string java-distribution: description: Java Distribution required: true type: string java-cache: type: string maven-image: description: Maven Image To Run Unit Tests required: true type: string jobs: unit-test: runs-on: docker-runner container: image: ${{ inputs.maven-image }} steps: - uses: actions/checkout@v3 - name: Set up JDK ${{ inputs.java-version }} uses: actions/setup-java@v3 with: java-version: ${{ inputs.java-version }} distribution: ${{ inputs.java-distribution }} cache: ${{ inputs.java-cache }} - name: Build with Maven run: mvn -B ${{ env.MAVEN_GOALS }} - name: Publish Test Report uses: actions/upload-artifact@v3 if: success() || failure() # always run even if the previous step fails with: name: unit-test-result path: 'target/surefire-reports/TEST-*.xml'
内容的提问来源于stack exchange,提问作者Berkin
相关产品推荐
相关产品推荐

