简体   繁体   English

为不同的 GitHub Actions 作业共享相同的步骤

[英]Share same steps for different GitHub Actions jobs

I have a cross-platform project which is to be built on 2 platforms: mac and linux(ubuntu).我有一个跨平台项目,将在 2 个平台上构建:mac 和 linux(ubuntu)。

My pipeline contains 3 jobs:我的管道包含 3 个作业:

  1. prepare docker image with all nessesary too to build the project.准备 docker 图像以及所有必要的构建项目。
  2. build on ubuntu in prepared docker container, depends on step 1在准备好的 docker 容器中构建 ubuntu,取决于步骤 1
  3. build on MacOS, needs nothing在 MacOS 上构建,不需要任何东西

Steps for linux and macos are definitely the same. linux和macos的步骤肯定是一样的。 But matrixes differs much, and linux build is run inside container.但矩阵差异很大,linux 构建在容器内运行。

Is there a way to share steps between two different jobs?有没有办法在两个不同的工作之间共享步骤?

I tried YAML anchors but GitHub does not support them.我尝试了 YAML 锚点,但 GitHub 不支持它们。

Full workflow完整的工作流程

on:
  push:
    branches: [ main, support/1.2.x ]
  pull_request:
    branches: [ main, support/1.2.x ]

jobs:
  Docker-iroha-builder:
    runs-on: ubuntu-latest
    steps:
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Set up Docker Buildx
        uses: docker/setup-buildx-action@v1
      -
        name: Cache Docker layers
        uses: actions/cache@v2
        with:
          path: /tmp/.buildx-cache
          key: ${{ runner.os }}-buildx-${{ github.sha }}
          restore-keys: |
            ${{ runner.os }}-buildx-
      -
        name: Login to DockerHub
        uses: docker/login-action@v1 
        with:
          username: ${{ secrets.DOCKERHUB_USERNAME }}
          password: ${{ secrets.DOCKERHUB_TOKEN }}
      -
        name: Build and push
        uses: docker/build-push-action@v2
        with:
          file: docker/develop/Dockerfile.builder
          # context: .
          push: true
          tags: ${{ secrets.DOCKERHUB_ORG }}/iroha:builder
          cache-from: type=local,src=/tmp/.buildx-cache
          cache-to: type=local,dest=/tmp/.buildx-cache-new
      -
        # Temp fix
        # https://github.com/docker/build-push-action/issues/252
        # https://github.com/moby/buildkit/issues/1896
        name: Move cache
        run: |
          rm -rf /tmp/.buildx-cache
          mv /tmp/.buildx-cache-new /tmp/.buildx-cache


  build-iroha-ubuntu:
    needs: Docker-iroha-builder
    runs-on: ubuntu-latest
    container: ikyb/iroha:builder
    strategy:
      fail-fast: false
      matrix:
        cc: [ gcc-9, gcc-10, clang ]  ##todo g++-10
        USE_BURROW: [ -DUSE_BURROW=OFF ]
        debrel: [ Debug ] #,Release, RelWithDebInfo
    steps:
      - ## Takes 22 seconds with default github runner
        name: Homebrew
        run: brew install cmake ninja coreutils
        if: ${{ runner.os == 'MacOS' }}
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Cache vcpkg
        uses: actions/cache@v2
        with:
          path: |
            build-vcpkg
            build/vcpkg_installed
            $HOME/.cache/vcpkg
          key:          ${{ runner.os }}-vcpkg-${{ github.sha }}
          restore-keys: ${{ runner.os }}-vcpkg-
      - 
        name: Build Iroha vcpkg dependancies
        run: ./vcpkg/build_iroha_deps.sh $PWD/build-vcpkg
      - 
        name: CMake configure
        run: |
          export CC=${{ matrix.cc }} CXX=$(echo ${{ matrix.cc }} | sed -es,gcc,g++, -es,clang,clang++,)
          cmake -B build -DCMAKE_TOOLCHAIN_FILE=$PWD/build-vcpkg/scripts/buildsystems/vcpkg.cmake \
             ${{ matrix.USE_BURROW }} -GNinja #-DCMAKE_VERBOSE_MAKEFILE=ON
      -
        name: CMake build
        run: cmake --build build --config ${{ matrix.debrel }}

  build-iroha-macos:
    runs-on: macos-latest
    strategy:
      fail-fast: false
      matrix:
        USE_BURROW: [ -DUSE_BURROW=OFF ]
        debrel: [ Debug,Release ]
    steps:
      - ## Takes 22 seconds with default github runner
        name: Homebrew
        run: brew install cmake ninja coreutils
        if: ${{ runner.os == 'MacOS' }}
      -
        name: Checkout
        uses: actions/checkout@v2
      -
        name: Cache vcpkg
        uses: actions/cache@v2
        with:
          path: |
            build-vcpkg
            build/vcpkg_installed
            $HOME/.cache/vcpkg
          key:          ${{ runner.os }}-vcpkg-${{ github.sha }}
          restore-keys: ${{ runner.os }}-vcpkg-
      - 
        name: Build Iroha vcpkg dependancies
        run: ./vcpkg/build_iroha_deps.sh $PWD/build-vcpkg
      - 
        name: CMake configure
        run: |
          export CC=${{ matrix.cc }} CXX=$(echo ${{ matrix.cc }} | sed -es,gcc,g++, -es,clang,clang++,)
          cmake -B build -DCMAKE_TOOLCHAIN_FILE=$PWD/build-vcpkg/scripts/buildsystems/vcpkg.cmake \
             ${{ matrix.USE_BURROW }} -GNinja #-DCMAKE_VERBOSE_MAKEFILE=ON
      -
        name: CMake build
        run: cmake --build build --config ${{ matrix.debrel }}

TL;DR TL;博士

I solved my problem with shell tool yq我用yq工具解决了我的问题

yq eval 'explode(.)' file.yml

Long answer长答案

GitHub Workflow description in YAML does not support anchors. YAML 中的 GitHub 工作流描述不支持锚点。 There are several workarounds => anyway they come to building-editing workflow yaml from source.有几种解决方法 => 无论如何,它们都是从源代码构建编辑工作流 yaml 的。 So I suggest yet another one make-workflows.sh based on YAML tool yq .所以我建议另一个基于make-workflows.sh工具yq的 make-workflows.sh 。

USAGE用法

  1. Move your workflows to .github/*.src.yml将您的工作流程移至.github/*.src.yml
  2. Put make-workflows.sh to directory .github/make-workflows.sh放到目录.github/
  3. (optional) Copy or link pre-commit.sh to .git/hooks/pre-commit Like ln -s../../.github/pre-commit.sh.git/hooks/pre-commit (可选)复制或链接pre-commit.sh.git/hooks/pre-commit就像ln -s../../.github/pre-commit.sh.git/hooks/pre-commit

File make-workflows.sh文件make-workflows.sh

#!/usr/bin/env bash
set -euo pipefail

## The script expands '*.src.yml' from $1(default: script's directory) 
## to $2 (default:subdirectory 'workflows') with corresponding name '*.yml'
## Main goal is to dereference YAML anchors.
## Deals only with Git cached/indexed files
## Set -x to debug

script_dir=$(dirname $(realpath "$0"))
dir_from=${1:-${script_dir}}
dir_to=${2:-workflows}
cd $dir_from

edited=
for f in $(git status -s -- \*.src.yml | sed 's,^.. ,,') ;do
    readonly out=$(echo $f | sed s,.src.yml\$,.yml,)
    readonly wout=$dir_to/$out
    readonly tempout=$(mktemp)
    trap "rm -f $tempout" EXIT
    echo >>$tempout "## DO NOT EDIT"
    echo >>$tempout "## Generated from $f with $(basename $0)"
    echo >>$tempout ""
    yq eval 'explode(.)' $f >>$tempout
    if ! diff -q $wout $tempout &>/dev/null ;then
        mv $tempout $wout
        edited+="'$out' "
    fi
done

if [[ -n "$edited" ]] 
then echo >&2 "make-workflows: these files were edited: $edited"
else echo >&2 "make-workflows: everything is up to date"
fi

File pre-commit.sh文件预提交.sh

#!/usr/bin/env bash
set -euo pipefail

gitroot=$(git rev-parse --show-toplevel)

cd $gitroot
./.github/make-workflows.sh
git add .github/workflows

Links链接

  1. Share same steps for different GitHub Actions jobs 为不同的 GitHub 操作作业共享相同的步骤
  2. https://github.community/t/support-for-yaml-anchors/16128/60https://github.community/t/support-for-yaml-anchors/16128/60
  3. https://github.com/mithro/actions-includes https://github.com/mithro/actions-includes
  4. https://github.com/allejo/gha-workflows https://github.com/allejo/gha-workflows

While github actions does not support YAML anchors directly, one can expand those eg by converting from YAML to JSON and then back to YAML. I am doing this here ( Makefile in .github/workflows ): https://github.com/agda/agda/blob/557681d04aae2100ccde2e045a8afcf30528c3a5/.github/workflows/Makefile而github动作不支持YAML直接锚定,可以通过从8837017528888转换到881569990078588,然后返回到8837017528888.2888888881888818888168888168. I AM(88818888188881888818888818888818. .github/workflowsMakefile ))可以扩展这些EG。 /agda/blob/557681d04aae2100ccde2e045a8afcf30528c3a5/.github/workflows/Makefile

srcpath=../../src/github/workflows
sources=$(wildcard $(srcpath)/*.yml $(srcpath)/*.yaml)
targets=$(sort $(notdir $(sources)))

all : $(targets)

# Normalize YAML files by going via JSON.
# This expands anchors which are not understood by github workflows.

% : $(srcpath)/% 
    yaml2json $< | json2yaml - > $@

An example for a workflow file with anchors is here: https://github.com/agda/agda/blob/557681d04aae2100ccde2e045a8afcf30528c3a5/src/github/workflows/test.yml带有锚点的工作流文件示例如下: https://github.com/agda/agda/blob/557681d04aae2100ccde2e045a8afcf30528c3a5/src/github/workflows/test.yml

jobs:
  build:
    runs-on: &runs_on ubuntu-22.04
    steps:
    - &checkout
      uses: actions/checkout@v3

    - &haskell_setup
      uses: haskell/actions/setup@v2
      with:
        ghc-version: ${{ env.GHC_VER }}
...
  test:
    needs: build
    runs-on: *runs_on

    steps:
    - *checkout
    - *haskell_setup
...

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在同一存储库中的 github 操作之间共享代码? - How to share code between github actions in the same repository? 我可以有一个 GitHub 动作工作流,里面没有任何作业吗? - Can I have a GitHub Actions Workflow without any Jobs inside? Github Actions:如何为多个作业创建多行环境变量 - Github Actions: How to create multiline env variable for serveral jobs 通过将它们部署到不同的文件夹,使用 Github 操作区分同一存储库的两个实例 - Differentiate between two instances of the same repository using Github Actions by deploying them to different folders 如何轻松使 Github 操作在特定条件下跳过后续作业执行? - How can I easily make Github Actions skip subsequent jobs execution on certain condition? 我如何使用 Github 操作的并发来取消其他作业,如果其中任何一个失败? - How can I use Github Actions' concurrency to cancel other jobs, if any of them fail? GitHub 操作无法找到 cdk 命令,尽管它是从以前的作业安装的 - GitHub Actions failed to find cdk command although it was installed from the previous jobs 在 Prometheus 配置中为多个作业重用相同的目标,但不同的端口 - Reuse same targets, but different ports, for multiple jobs in Prometheus config 未检索到 Github Actions 结果 - Github Actions outcome is not retrieved Azure-devops:共享来自运行自己作业的不同代理的输出 - Azure-devops: Share output from different agents running their own jobs
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM