如何在特定条件下触发GitHub可重复使用工作流

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

How To Trigger GitHub Reusable Workflow On Specific Condition

问题

我有一个可重用的工作流,在每次提交被推送后运行单元测试。然而,我现在不想在每次提交推送时触发它。是否有一种方法可以在**标签创建**或**拉取请求打开 | 当新的提交推送到拉取请求时**时运行可重用工作流?

# 可重用单元测试工作流

    name: 可重用单元测试
    
    env:
        # 这将抑制任何对依赖项和插件的下载或会混淆控制台日志的上传消息。
        # `showDateTime`将显示以毫秒为单位的经过的时间。您需要指定`--batch-mode`以使其工作。
        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 3.3.0开始,您可以在`.mvn/maven.config`中定义这些选项,以便从命令行运行时使用相同的配置。
        MAVEN_CLI_OPTS: "--batch-mode --errors --fail-at-end --show-version"
    
        # 当需要额外的目标时进行覆盖(例如`package assembly:single`)
        MAVEN_ARTIFACT_GOALS: "test"
    
        # 重要提示:仅在客户项目中在严格必要时进行覆盖
        # 注意:这里的顺序很重要。在运行测试之前必须执行prepare-agent
        MAVEN_GOALS: "clean org.jacoco:jacoco-maven-plugin:prepare-agent $MAVEN_ARTIFACT_GOALS"
    
    on:
        # 不适用于此处
        push:
            tags:
                - '*'
        pull_request:
            types: [opened, synchronize, reopened]
        # 到此为止
        
        workflow_call:
            inputs:
                java-version:
                    description: Java版本
                    required: true
                    type: string
                java-distribution:
                    description: Java分发
                    required: true
                    type: string
                java-cache:
                    type: string
                maven-image:
                    description: 用于运行单元测试的Maven镜像
                    required: true
                    type: string
    jobs:
        unit-test:
            runs-on: docker-runner
            container:
                image: ${{ inputs.maven-image }}
            steps:
                -   uses: actions/checkout@v3
    
                -   name: 设置JDK ${{ inputs.java-version }}
                    uses: actions/setup-java@v3
                    with:
                        java-version: ${{ inputs.java-version }}
                        distribution: ${{ inputs.java-distribution }}
                        cache: ${{ inputs.java-cache }}
    
                -   name: 使用Maven构建
                    run: mvn -B ${{ env.MAVEN_GOALS }}
    
                -   name: 发布测试报告
                    uses: actions/upload-artifact@v3
                    if: success() || failure() # 即使上一步失败,也始终运行
                    with:
                        name: unit-test-result
                        path: 'target/surefire-reports/TEST-*.xml'

# 主工作流

    name: GitHub操作演示
    on: [push]
    
    jobs:
        unit-test:
            name: 单元测试
            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: 集成测试
            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

我需要使用`jobs.<job_id>.if`吗?如果需要,应该如何使用?

谢谢
英文:

I have a reusable workflow to run unit tests after each commit is pushed. However, I don't want to trigger it on each commit push now. Is there a way to run reused workflow when Tag Created or Pull Request Opened | When New Commit Pushed To Pull Request

Reusable Unit Test Workflow

name: Reusable Unit Test

env:
    # This will suppress any download for dependencies and plugins or upload messages which would clutter the console log.
    # `showDateTime` will show the passed time in milliseconds. You need to specify `--batch-mode` to make this work.
    MAVEN_OPTS: &quot;-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&quot;

    # As of Maven 3.3.0 instead of this you may define these options in `.mvn/maven.config` so the same config is used
    # when running from the command line.
    MAVEN_CLI_OPTS: &quot;--batch-mode --errors --fail-at-end --show-version&quot;

    # Override when additional goals are necessary (such as `package assembly:single`)
    MAVEN_ARTIFACT_GOALS: &quot;test&quot;

    # IMPORTANT: Only override in client project when strictly necessary
    # NOTE: The order here is important. prepare-agent must be done before running tests
    MAVEN_GOALS: &quot;clean org.jacoco:jacoco-maven-plugin:prepare-agent $MAVEN_ARTIFACT_GOALS&quot;

on:
    # does not work from here
    push:
        tags:
            - &#39;*&#39;
    pull_request:
        types: [ opened, synchronize, reopened ]
    # to here
    
    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: &#39;target/surefire-reports/TEST-*.xml&#39;

Main Workflow

name: GitHub Actions Demo
on: [ push ]

jobs:
    unit-test:
        name: Unit Tests
        uses: ./.github/workflows/run-unit-tests.yml
        with:
            java-version: &#39;17&#39;
            java-distribution: &#39;corretto&#39;
            java-cache: &#39;maven&#39;
            maven-image: &#39;maven:3-amazoncorretto-17&#39;
    integration-test:
        name: Integration Tests
        uses: ./.github/workflows/run-integration-tests.yml
        with:
            java-version: &#39;17&#39;
            java-distribution: &#39;corretto&#39;
            java-cache: &#39;maven&#39;
            maven-image: &#39;maven:3-amazoncorretto-17&#39;
            skip-unit-tests: true

Do I need to use jobs.&lt;job_id&gt;.if? If so, how?

Thanks

答案1

得分: 0

我找到了解决方案。移除workflow_call不是正确使用重用工作流的方式。如果需要为不同事件创建单独的作业,我们应该使用jobs.<job_id>.if,或者使用类似以下方式来调用重用工作流

on:
    pull_request:
        types: [opened, reopened, synchronize]
    push:
        tags:
            - '*'
英文:

I found the solution. Removing workflow_call is not the correct way to use reused workflows. If you need separate jobs for different events, we should use jobs.&lt;job_id&gt;.if or use something like this where you call reused workflows

on:
    pull_request:
        types: [ opened, reopened, synchronize ]
    push:
        tags:
            - &#39;*&#39;

huangapple
  • 本文由 发表于 2023年3月7日 04:39:13
  • 转载请务必保留本文链接:https://go.coder-hub.com/75655606.html
匿名

发表评论

匿名网友

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

确定