简体   繁体   中英

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. I'm getting an error "can't find the project".

Can I call actions/checkout @ v3 once for two jobs?

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. You are actually runing the 2 jobs on 2 different agents. 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'. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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