Github操作:如何在脚本中使用策略/矩阵

编程入门 行业动态 更新时间:2024-10-28 00:27:27
本文介绍了Github操作:如何在脚本中使用策略/矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我的工作流程需要循环执行这些步骤,这非常适合策略/矩阵.

I have workflow that needs to have a loop for the steps, which is perfect with strategy/matrix.

唯一的问题是策略/矩阵需要由常量设置.

The only problem is that strategy/matrix needs to be set by a constant.

是否可以将策略矩阵与脚本输出一起使用?

Is it possible to use strategy matrix with a output of a script?

name: tests on: [push] jobs: test: runs-on: ${{ ubuntu-latest }} strategy: fail-fast: false matrix: versions: $(./script.py) steps: - uses: actions/checkout@v2 .......

推荐答案

您可以在一个作业中以JSON格式生成矩阵并将其设置为第二个作业.

You can generate matrix in JSON in one job and set it to the second job.

GitHub在4月添加了此功能: github.blog/changelog/2020-04-15-github-actions-new-workflow-features/

GitHub added this feature in April: github.blog/changelog/2020-04-15-github-actions-new-workflow-features/

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:

First job sets output variable matrix to JSON that contains two configurations:

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

.yml中的等效值:

Equivalent in .yml:

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

别忘了对引号 \'''进行转义并在一行中打印JSON.

Do not forget to escape quotes \" and print JSON in one line.

它检测更改的文件,并为更改的目录运行构建作业.如果目录名称以OS名称开头,则使用该名称作为运行文件.

It detects changed files and runs build job for changed directories. If directory name starts with OS name, it uses that name as runs-on.

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 githubmunity/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 \n with %0A) echo "::set-output name=diff::$( echo "$DIFF" | sed ':a;N;$!ba;s/\n/%0A/g' )" - name: Set matrix for build id: set-matrix run: | # See stackoverflow/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 "::set-output name=matrix::$( echo "$JSON" )" 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操作:如何在脚本中使用策略/矩阵

本文发布于:2023-10-29 09:30:50,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1539341.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:矩阵   脚本   策略   操作   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!