【问题标题】:GitHub actions: How to switch conditionally a run command?GitHub 操作:如何有条件地切换运行命令?
【发布时间】:2020-08-31 23:41:01
【问题描述】:

我创建了一个包含这 2 个作业的工作流:

  build-and-test-with-Java-8-and-later:
    name: Build -> Test (JVM ${{ matrix.java }}, ${{ matrix.architecture }}, ${{ matrix.os }})
    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        java: [8, 9, 10, 11, 12, 13, 14]
        architecture: [x32, x64]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java }}
          architecture: ${{ matrix.architecture }}  
      - name: Build and test
        run: mvn -B clean test -Dproject_jdk_version=${{ matrix.java }} -DskipTests=false --file pom.xml

  build-and-test-with-Java-15-ea:
    name: Build -> Test (JVM ${{ matrix.java }}, ${{ matrix.architecture }}, ${{ matrix.os }})
    strategy:
      fail-fast: true
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        java: [15-ea]
        architecture: [x32, x64]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v1
        with:
          java-version: ${{ matrix.java }}
          architecture: ${{ matrix.architecture }}  
      - name: Build and test
        run: mvn -B clean test -Dproject_jdk_version=15 -DskipTests=false --file pom.xml

...我想将这两个作业合并到一个独特的作业中,当迭代 ${{ matrix.java }} 变量包含“-ea”后缀时切换运行元素。特别是我想创建这样的东西:

  build-and-test-with-Java-8-and-later:
    name: Build -> Test (JVM ${{ matrix.java }}, ${{ matrix.architecture }}, ${{ matrix.os }})
    strategy:
      fail-fast: true
      max-parallel: 10
      matrix:
        os: [ubuntu-latest, macOS-latest, windows-latest]
        java: [8, 9, 10, 11, 12, 13, 14, 15-ea]
        architecture: [x32, x64]
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Set up JDK ${{ matrix.java }}
        uses: actions/setup-java@v1
        with:
          java-version: if${{ matrix.java }}
          architecture: ${{ matrix.architecture }}  
      - name: Build and test
        if: ${{ matrix.java }} != '15-ea'
        run: mvn -B clean test -Dproject_jdk_version=${{ matrix.java }} -DskipTests=false --file pom.xml
      - name: Build and test
        if: ${{ matrix.java }} == '15-ea'
        run: mvn -B clean test -Dproject_jdk_version=15 -DskipTests=false --file pom.xml

【问题讨论】:

    标签: workflow jobs github-actions


    【解决方案1】:

    您可以使用来自 Githu Actions 的可用function 之一。

    在你的情况下,这会起作用:

    - name: Build and test
       if: contains(matrix.java, '15-ea')
       run: mvn -B clean test -Dproject_jdk_version=${{ matrix.java }} -DskipTests=false --file pom.xml
    

    【讨论】:

      猜你喜欢
      • 2022-12-29
      • 2022-01-12
      • 2022-06-14
      • 2021-06-08
      • 2020-11-20
      • 2020-07-06
      • 1970-01-01
      • 1970-01-01
      • 2021-09-08
      相关资源
      最近更新 更多