English 中文(简体)
Github 行动: 如何使用战略/矩阵和文字
原标题:Github Actions: How use strategy/matrix with script

我的工作流程需要为这些步骤留出余地,这完全符合战略/矩阵。

唯一的问题是,战略/矩阵需要不断确定。

能否使用具有文字产出的战略矩阵?

name: tests
on: [push]

jobs:
  test:
    runs-on: ${{ ubuntu-latest }}
    strategy:
      fail-fast: false
      matrix:
        versions: $(./script.py)

    steps:
    - uses: actions/checkout@v2
 .......
最佳回答

您可以在一个岗位上形成一个矩阵,并安排第二个工作。

吉特霍在4月添加了这一特征:https://github.blog/changelog/2020-04-15-github-actions-new-workflow-features/

Workflow example

name: build
on: push
jobs:
  job1:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
    - id: set-matrix
      run: echo "::set-output name=matrix::{"include":[{"project":"foo","config":"Debug"},{"project":"bar","config":"Release"}]}"
  job2:
    needs: job1
    runs-on: ubuntu-latest
    strategy:
      matrix: ${{fromJson(needs.job1.outputs.matrix)}}
    steps:
    - run: echo ${{ matrix.project }}
    - run: echo ${{ matrix.config }}

第一份工作组产出变量matrix至JSON,其中包含两个组合:

{
  "include": [
    {
      "project": "foo",
      "config": "Debug"
    },
    {
      "project": "bar",
      "config": "Release"
    }
  ]
}

等值:

  job2:
    strategy:
      matrix:
        include:
        - project: foo
          config: Debug
        - project: bar
          config: Release

不要忘记一行的“<代码>”和“数字”的打印。

More complex Workflow example

。 如果名录名称以非洲顾问办名称为首,则使用该名称为代号。

name: Build
on: [push, pull_request]

jobs:

  generate-matrix:
    name: Generate matrix for build
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - uses: actions/checkout@v2
      - name: Check changed files
        id: diff
        run: |
          # See https://github.community/t/check-pushed-file-changes-with-git-diff-tree-in-github-actions/17220/10
          if [ $GITHUB_BASE_REF ]; then
            # Pull Request
            git fetch origin $GITHUB_BASE_REF --depth=1
            export DIFF=$( git diff --name-only origin/$GITHUB_BASE_REF $GITHUB_SHA )
            echo "Diff between origin/$GITHUB_BASE_REF and $GITHUB_SHA"
          else
            # Push
            git fetch origin ${{ github.event.before }} --depth=1
            export DIFF=$( git diff --name-only ${{ github.event.before }} $GITHUB_SHA )
            echo "Diff between ${{ github.event.before }} and $GITHUB_SHA"
          fi
          echo "$DIFF"
          # Escape newlines (replace 
 with %0A)
          echo "diff=$( echo "$DIFF" | sed  :a;N;$!ba;s/
/%0A/g  )" >> $GITHUB_OUTPUT
      - name: Set matrix for build
        id: set-matrix
        run: |
          # See https://stackoverflow.com/a/62953566/11948346
          DIFF="${{ steps.diff.outputs.diff }}"
          JSON="{"include":["

          # Loop by lines
          while read path; do
            # Set $directory to substring before /
            directory="$( echo $path | cut -d /  -f1 -s )"

            if [ -z "$directory" ]; then
              continue # Exclude root directory
            elif [ "$directory" == docs ]; then
              continue # Exclude docs directory
            elif [ "$path" == *.rst ]; then
              continue # Exclude *.rst files
            fi

            # Set $os. "ubuntu-latest" by default. if directory starts with windows, then "windows-latest"
            os="ubuntu-latest"
            if [ "$directory" == windows* ]; then
              os="windows-latest"
            fi

            # Add build to the matrix only if it is not already included
            JSONline="{"directory": "$directory", "os": "$os"},"
            if [[ "$JSON" != *"$JSONline"* ]]; then
              JSON="$JSON$JSONline"
            fi
          done <<< "$DIFF"

          # Remove last "," and add closing brackets
          if [[ $JSON == *, ]]; then
            JSON="${JSON%?}"
          fi
          JSON="$JSON]}"
          echo $JSON

          # Set output
          echo "matrix=$( echo "$JSON" )" >> $GITHUB_OUTPUT

  build:
    name: Build "${{ matrix.directory }}" on ${{ matrix.os }}
    needs: generate-matrix
    strategy:
      matrix: ${{fromJson(needs.generate-matrix.outputs.matrix)}}
    runs-on: ${{ matrix.os }}
    steps:
      - uses: actions/checkout@v2
      - name: Build
        run: |
          cd ${{ matrix.directory }}
          echo "${{ matrix.directory }} ${{ matrix.os }}"
问题回答

添加一个新例子,是一个真正有益的答案。 谢谢你!

其中一个用途是:Github strategy.matrix of Github Actions with from Json ,仅收集Pul Request中的名录,并作改动,并用

---
name: Check Syntax
on: [pull_request]

jobs:
  generate-matrix:
    name: Generate matrix for build
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.set-matrix.outputs.matrix }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2
        with:
          fetch-depth: 0
      - name: Check changed files
        id: diff
        run: |
          # See https://github.community/t/check-pushed-file-changes-with-git-diff-tree-in-github-actions/17220/10
          export DIFF=$( git diff --dirstat=files,0,cumulative ${{ github.event.pull_request.base.sha }} | awk -F      {print $2}  )
          echo "$DIFF"
          # Escape newlines (replace 
 with %0A)
          echo "::set-output name=diff::$( echo "$DIFF" | sed  :a;N;$!ba;s/
/%0A/g  )"
      - name: Set matrix for build
        id: set-matrix
        run: |
          # See https://stackoverflow.com/a/62953566/11948346
          DIFF="${{ steps.diff.outputs.diff }}"
          JSON="{"tfpaths":["

          # Loop by lines
          while read path; do
          # Add item to the matrix only if it is not already included
          JSONline=""$path","
          if [[ "$JSON" != *"$JSONline"* ]]; then
          JSON="$JSON$JSONline"
          fi
          done <<< "$DIFF"

          # Remove last "," and add closing brackets
          if [[ $JSON == *, ]]; then
          JSON="${JSON%?}"
          fi
          JSON="$JSON]}"
          echo $JSON
          # Set output
          echo "::set-output name=matrix::$( echo "$JSON" )"

  validate:
    name: Check Terraform syntax on "${{ matrix.tfpaths }}"
    needs: generate-matrix
    strategy:
      matrix: ${{fromJson(needs.generate-matrix.outputs.matrix)}}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: terraform validate
        uses: dflook/terraform-validate@v1
        with:
          path: ${{ matrix.tfpaths }}

  check-format:
    name: Check Terraform format on "${{ matrix.tfpaths }}"
    needs: generate-matrix
    strategy:
      matrix: ${{fromJson(needs.generate-matrix.outputs.matrix)}}
    runs-on: ubuntu-latest
    steps:
      - name: Checkout
        uses: actions/checkout@v2
      - name: terraform fmt
        uses: dflook/terraform-fmt-check@v1
        with:
          path: ${{ matrix.tfpaths }}

I used @ArtemSBulgakov s 解决办法作为起点,但不愿生成matrix。 某些其他产出,而不是直截了当。

如果您与我一样,希望能从其他<代码>输入>。 见下文。


在这种例子中,我想引用Gite Hub最近提出的拉动请求,使用octokit/request-action。 然后对每项拉动要求进行一些检查。 声音很简单,但改变了产出(即<代码>步骤s.fetch.outputs.) ......

{
  "includes": [{ "number": 1, "title": "my first pr ?" }]
}

......比我预期要困难得多。 可通过https://docs.github.com/en/actions/vis/workflow-syntax-for-github-actions#using-a-specific-shell”rel=“nofollow noreferer”查询shell s。 吉特霍特提供,但你仍不得不通过output。 <代码>run 则重报。 如果有人知道这样做的容易,我会很高兴看到。

因此,我决定创建nickofthyme/object-remap。 吉特霍行动使这种行动变得稍微容易。 我在所有使用中都获得了一定数据(见README.md),但使用<代码>matrix.includes的事例有:

name:  PR Check 

on:
  schedule:
    - cron:   0 0 * * *  # once a day

jobs:
  fetch:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.save.outputs.json }}
    steps:
      - name: Fetch GH pulls
        id: fetch
        uses: octokit/[email protected]
        with:
          route: GET /repos/{repo}/pulls?state=open
          repo: ${{ github.repository }}
        env:
          GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
      - name: Store matrix
        id: save
        uses: nickofthyme/object-remap@v1
        with:
          include.*.number: ${{ toJSON(fromJSON(steps.fetch.outputs.data).*.number) }}
          include.*.title: ${{ toJSON(fromJSON(steps.fetch.outputs.data).*.title) }}
          include.*.username: ${{ toJSON(fromJSON(steps.fetch.outputs.data).*.user.login) }}

  pr-checks:
    name: "PR #${{ matrix.number }} - ${{ matrix.title }}"
    runs-on: ubuntu-latest
    needs: fetch
    strategy:
      matrix: ${{ fromJSON(needs.fetch.outputs.matrix) }}
      fail-fast: false
    steps:
      - name: Echo pr number
        run: echo "pr number: ${{ matrix.number }}"
      - name: Echo title
        run: echo "title: ${{ matrix.title }}"
      - name: Echo username
        run: echo "username: ${{ matrix.username }}"

例如,如果这项工作在上进行,react。 第2条

curl https://api.github.com/repos/facebook/react/pulls?per_page=2&direction=asc&state=all

<代码>s.fetch.outputs.data, omitting head,base and _links for brevity, be...

[
  {
      "url": "https://api.github.com/repos/facebook/react/pulls/1",
      "id": 6001916,
      "node_id": "MDExOlB1bGxSZXF1ZXN0NjAwMTkxNg==",
      "html_url": "https://github.com/facebook/react/pull/1",
      "diff_url": "https://github.com/facebook/react/pull/1.diff",
      "patch_url": "https://github.com/facebook/react/pull/1.patch",
      "issue_url": "https://api.github.com/repos/facebook/react/issues/1",
      "number": 1,
      "state": "closed",
      "locked": false,
      "title": "Run each test in its own <iframe>",
      "user": {
          "login": "benjamn",
          "id": 5750,
          "node_id": "MDQ6VXNlcjU3NTA=",
          "avatar_url": "https://avatars.githubusercontent.com/u/5750?v=4",
          "gravatar_id": "",
          "url": "https://api.github.com/users/benjamn",
          "html_url": "https://github.com/benjamn",
          "followers_url": "https://api.github.com/users/benjamn/followers",
          "following_url": "https://api.github.com/users/benjamn/following{/other_user}",
          "gists_url": "https://api.github.com/users/benjamn/gists{/gist_id}",
          "starred_url": "https://api.github.com/users/benjamn/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/benjamn/subscriptions",
          "organizations_url": "https://api.github.com/users/benjamn/orgs",
          "repos_url": "https://api.github.com/users/benjamn/repos",
          "events_url": "https://api.github.com/users/benjamn/events{/privacy}",
          "received_events_url": "https://api.github.com/users/benjamn/received_events",
          "type": "User",
          "site_admin": false
      },
      "body": "This is not blocking the initial launch, so feel free to put it on the back-burner for now.

The Jasmine test harness still runs in the parent window and reports to PhantomJS via `window.callPhantom`, but each test `<iframe>` has its own copy of `react-test.js` and each individual test module is required in the global context of a separate `<iframe>`.

This gives us a significant approximation of the benefits of mocking, at least in terms of isolating tests from one another.

cr @jeffmo @zpao
",
      "created_at": "2013-05-29T20:20:53Z",
      "updated_at": "2014-07-16T22:39:07Z",
      "closed_at": "2013-06-03T17:58:02Z",
      "merged_at": "2013-06-03T17:58:02Z",
      "merge_commit_sha": "7a72883d48e00854a41a1cdff99a2544c1721dcc",
      "assignee": null,
      "assignees": [],
      "requested_reviewers": [],
      "requested_teams": [],
      "labels": [],
      "milestone": null,
      "draft": false,
      "commits_url": "https://api.github.com/repos/facebook/react/pulls/1/commits",
      "review_comments_url": "https://api.github.com/repos/facebook/react/pulls/1/comments",
      "review_comment_url": "https://api.github.com/repos/facebook/react/pulls/comments{/number}",
      "comments_url": "https://api.github.com/repos/facebook/react/issues/1/comments",
      "statuses_url": "https://api.github.com/repos/facebook/react/statuses/603c9ef6a8d70d3cf29ee9d0a9d7969abce48ac4",
      "head": {},
      "base": {},
      "_links": {},
      "author_association": "CONTRIBUTOR",
      "auto_merge": null,
      "active_lock_reason": null
  },
  {
      "url": "https://api.github.com/repos/facebook/react/pulls/2",
      "id": 6002192,
      "node_id": "MDExOlB1bGxSZXF1ZXN0NjAwMjE5Mg==",
      "html_url": "https://github.com/facebook/react/pull/2",
      "diff_url": "https://github.com/facebook/react/pull/2.diff",
      "patch_url": "https://github.com/facebook/react/pull/2.patch",
      "issue_url": "https://api.github.com/repos/facebook/react/issues/2",
      "number": 2,
      "state": "closed",
      "locked": false,
      "title": "[docs] Fix button links on bottom of home",
      "user": {
          "login": "paulshen",
          "id": 2266187,
          "node_id": "MDQ6VXNlcjIyNjYxODc=",
          "avatar_url": "https://avatars.githubusercontent.com/u/2266187?v=4",
          "gravatar_id": "",
          "url": "https://api.github.com/users/paulshen",
          "html_url": "https://github.com/paulshen",
          "followers_url": "https://api.github.com/users/paulshen/followers",
          "following_url": "https://api.github.com/users/paulshen/following{/other_user}",
          "gists_url": "https://api.github.com/users/paulshen/gists{/gist_id}",
          "starred_url": "https://api.github.com/users/paulshen/starred{/owner}{/repo}",
          "subscriptions_url": "https://api.github.com/users/paulshen/subscriptions",
          "organizations_url": "https://api.github.com/users/paulshen/orgs",
          "repos_url": "https://api.github.com/users/paulshen/repos",
          "events_url": "https://api.github.com/users/paulshen/events{/privacy}",
          "received_events_url": "https://api.github.com/users/paulshen/received_events",
          "type": "User",
          "site_admin": false
      },
      "body": "The buttons on the index were pointing at wrong paths.
",
      "created_at": "2013-05-29T20:31:39Z",
      "updated_at": "2014-06-27T04:39:06Z",
      "closed_at": "2013-05-29T20:32:25Z",
      "merged_at": "2013-05-29T20:32:25Z",
      "merge_commit_sha": "9aa4d2bc27c38b01c9c8f3436bd729d5e656cb1b",
      "assignee": null,
      "assignees": [],
      "requested_reviewers": [],
      "requested_teams": [],
      "labels": [],
      "milestone": null,
      "draft": false,
      "commits_url": "https://api.github.com/repos/facebook/react/pulls/2/commits",
      "review_comments_url": "https://api.github.com/repos/facebook/react/pulls/2/comments",
      "review_comment_url": "https://api.github.com/repos/facebook/react/pulls/comments{/number}",
      "comments_url": "https://api.github.com/repos/facebook/react/issues/2/comments",
      "statuses_url": "https://api.github.com/repos/facebook/react/statuses/c5b4fe9e88a9a3b43cfd9b7e5383096bd9e213ef",
      "head": {},
      "base": {},
      "_links": {},
      "author_association": "CONTRIBUTOR",
      "auto_merge": null,
      "active_lock_reason": null
  }
]

And the value of steps.save.outputs.json (aka needs.fetch.outputs.matrix) would be...


{ 
  "includes": [
    {
        "state": "closed",
        "title": "Run each test in its own <iframe>",
        "username": "benjamn"
    },
    {
        "number": 2,
        "title": "[docs] Fix button links on bottom of home",
        "username": "paulshen"
    }
  ]
}

......可轻易通过https://docs.github.com/en/actions/vis/workflow-syntax-for-github-actions#jobsjob_idstrategymatrix rel=“nofollow noreferer”jobs.<job_id>strategy.matrix 触发2个<代码>pr-checks 工作。

一份最后说明: 我试图将一系列的价值观仅仅通过到<代码>jobs.<job_id>.strategy.matrix.includes,但这是因为matrix.includes确实not接受Gite Hub expressions作为价值。 因此,在<代码>includes上加固的数值是走到!

这里是表态不平衡的另一个聪明的si。

https://github.com/nickofthyme/object-remap”rel=“nofollow noreferer”>https://github.com/nickofthyme/object-remap

...
env:
  PYTHON_VERSION:  [3.10, 3.11, 3.12] 
  OS_TYPE:  ["ubuntu-latest", "windows-latest", "macos-latest"] 
...
jobs:
  matrix_config:
    runs-on: ubuntu-latest
    outputs:
      matrix: ${{ steps.prepare.outputs.json }}

    steps:
      - name: Prepare matrix JSON Object
        id: prepare
        uses: nickofthyme/[email protected]
        with:
          __case: kebab
          os: ${{ env.OS_TYPE }}
          python_version: ${{ env.PYTHON_VERSION }}
...
  someJob:
    runs-on: ${{ matrix.os }}
    needs: [ matrix_config ]

    strategy:
      matrix: ${{ fromJSON(needs.matrix_config.outputs.matrix) }}

    steps:
...
      - name: Set-Up Python ${{ matrix.python-version }}
        uses: actions/setup-python@v4
        with:
          python-version: ${{ matrix.python-version }}
...




相关问题
Unable to Setup Edge browser through github actions

I have an Selenium Automation project locally. As per project requirement I have to run the selenium project into Linux host machine using github actions for that I was trying to install the Edge ...

如何按顺序管理吉布工作流程?

我想按顺序安排我的基特霍工作流程。 为了更好地解释这一点,一旦以前的工作流程完成,下一个工作流程就会启动,而不是同时运行。

Building docker image is failing

I have created below docker file to install JDK 16.0.2 version and Jmeter 5.5. While pushing changes in GitHub the "Build Jmeter Docker Image" steps are getting failed: I could see following ...

热门标签