简体   繁体   English

GitHub 共享相同代码的操作矩阵 CheckOut

[英]GitHub Actions Matrix sharing the Same Code CheckOut

I tried to perform step actions/checkout@v3 once on chained jobs, but it seems like the "build" job does not get the code.我尝试对链式作业执行一次步骤操作/checkout@v3,但似乎“构建”作业没有获得代码。 I'm getting an error "can't find the project".我收到错误消息“找不到项目”。

Can I call actions/checkout @ v3 once for two jobs?我可以为两个工作调用 actions/checkout @ v3 一次吗?

It works when I call the code checkout twice.当我两次调用代码结帐时它起作用了。

name: publish-nuget
on:
  push:
    branches:
      - main

jobs:
  prepare:
    runs-on: ubuntu-latest
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Get package version
      id: get_package_version
      uses: kzrnm/get-net-sdk-project-versions-action@v1.3.0
      with:
        proj-path: ProjectOne.csproj
    
  build:
    needs: prepare
    runs-on: ubuntu-latest
    steps:
    - name: Checkout code
      uses: actions/checkout@v3

    # Add the projects path below
    strategy:
      matrix:
        projects: [
        'ProjectOne.csproj',
        'ProjectTwo.csproj',
        ]

    steps:
    - name: Pack NuGet
      run: dotnet pack ${{ matrix.projects }} -p:PackageVersion=${{ env.PACKAGE_VERSION }} --configuration Release

It does not work when I call the code checkout once (on the 'prepare' job).当我调用一次代码结帐时(在“准备”工作中),它不起作用。

name: publish-nuget
on:
  push:
    branches:
      - main

jobs:
  prepare:
    runs-on: ubuntu-latest
    - name: Checkout code
      uses: actions/checkout@v3

    - name: Get package version
      id: get_package_version
      uses: kzrnm/get-net-sdk-project-versions-action@v1.3.0
      with:
        proj-path: ProjectOne.csproj
    
  build:
    needs: prepare
    runs-on: ubuntu-latest
    steps:
    # Add the projects path below
    strategy:
      matrix:
        projects: [
        'ProjectOne.csproj',
        'ProjectTwo.csproj',
        ]

    steps:
    - name: Pack NuGet
      run: dotnet pack ${{ matrix.projects }} -p:PackageVersion=${{ env.PACKAGE_VERSION }} --configuration Release

Having a job being dependent on another job, is just for logical purposes and not state or artifact dependency sharing.让一份工作依赖于另一份工作,只是出于逻辑目的,而不是 state 或工件依赖共享。 You are actually runing the 2 jobs on 2 different agents.您实际上是在 2 个不同的代理上运行 2 个作业。 If you want to share something from the prepare job, you can use the cache or artifact API. Eg using the cache API to cache the path 'somePath'.如果你想从准备作业中共享一些东西,你可以使用缓存或工件 API。例如,使用缓存 API 来缓存路径“somePath”。 Same step for downloading the cache.下载缓存的步骤相同。

- name: Cached build artifacts
  uses: actions/cache@v2
  id: artifactcache
  with:
    path: somePath
    key: buildArtifacts${{ github.run_number}}

As you are not gaining anything form splitting this up into 2 jobs, I would run everything in a single job instead.由于将其拆分为 2 个作业不会获得任何好处,因此我会在一个作业中运行所有内容。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM