【问题标题】:Run GitHub Action on multiple environments在多个环境中运行 GitHub Action
【发布时间】:2020-04-05 07:49:22
【问题描述】:

我想在多个环境中运行 GitHub Actions,比如说在 Windows 和 Linux 上。我设法用 Travis CI 做到了,但我找不到关于如何用 GitHub Actions 做到这一点的信息。

有人试过吗?

这是我的 nodejs.yml。

name: Node CI

on: [push]

jobs:
    build:
        runs-on: ubuntu-latest

        strategy:
            matrix:
                node-version: [12.x]

        steps:
            - uses: actions/checkout@v1
            - name: Use Node.js ${{ matrix.node-version }}
              uses: actions/setup-node@v1
              with:
                  node-version: ${{ matrix.node-version }}
            - name: npm install
              run: |
                  npm ci
            - name: prettier format check
              run: |
                  npm run prettier-check
            - name: lint format check
              run: |
                  npm run lint-check
            - name: build, and test
              run: |
                  npm run build
                  npm test --if-present
              env:
                  CI: true

【问题讨论】:

    标签: github continuous-integration github-actions


    【解决方案1】:

    您可以为此使用策略/矩阵(请参阅https://docs.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategy

    name: Node CI
    
    on: [push]
    
    jobs:
        build:
            runs-on: ${{ matrix.os }}
    
            strategy:
                matrix:
                    os: [ubuntu-latest, windows-latest]
                    node-version: [12.x]
    
            steps:
                - uses: actions/checkout@v1
                - name: Use Node.js ${{ matrix.node-version }}
                  uses: actions/setup-node@v1
                  with:
                      node-version: ${{ matrix.node-version }}
                - name: npm install
                  run: |
                      npm ci
                - name: prettier format check
                  run: |
                      npm run prettier-check
                - name: lint format check
                  run: |
                      npm run lint-check
                - name: build, and test
                  run: |
                      npm run build
                      npm test --if-present
                  env:
                      CI: true
    
    

    【讨论】:

    • 像魅力一样工作 - 并更好地遵守 DRY 原则,而不是仅仅复制相同的工作流程以使其在多个环境中构建。
    • 正是我需要的
    【解决方案2】:

    所以答案其实很简单。您创建了两个具有不同名称的操作文件。在我的例子中:

    nodejs-ubuntu.yml nodejs-windows.yml

    更改他们的名称并修改为所需的平台,例如 windows-lates/mac-latest 或其他任何东西,一切都应该按预期工作。

    【讨论】:

      猜你喜欢
      • 2021-03-23
      • 2020-05-09
      • 1970-01-01
      • 2020-12-10
      • 1970-01-01
      • 2022-10-14
      • 2021-09-24
      • 2021-10-08
      • 2021-06-19
      相关资源
      最近更新 更多