简体   繁体   中英

Can you have multiple working directories with github actions?

So I have a repo with multiple directories for multiple go projects. Is there a way to run github actions on multiple working directories so I don't have to have redundant yaml with github actions? To automate an error check with golang, I currently have:

  errcheck:
    name: Errcheck
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: check
      uses: grandcolline/golang-github-actions@v1.1.0
      working-directory: ./app1
      with:
        run: errcheck
        token: ${{ secrets.GITHUB_TOKEN }}

But I'd like to have:

  errcheck:
    name: Errcheck
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@master
    - name: check
      uses: grandcolline/golang-github-actions@v1.1.0
      working-directory: [./app1, ./app2]
      with:
        run: errcheck
        token: ${{ secrets.GITHUB_TOKEN }}

In order to run something in more than one working directory, I believe you have two options:

Option 1: Matrix

Use GitHub Action's jobs.<job_id>.strategy.matrix option. This will create multiple jobs, each with its own matrix (directory) value.

Here is a sample workflow:

name: Test
on:
  push: { branches: master }

jobs:
  test:
    name: Matrix test
    runs-on: ubuntu-latest
    strategy:
      matrix: { dir: ['some-dir', 'other-dir'] }

    steps:
    - name: Checkout code
      uses: actions/checkout@v2

    - name: Do something with the matrix value
      working-directory: ${{ matrix.dir }}
      run: pwd

Running this will create two jobs:

在此处输入图像描述

Option 2: Custom Shell Script

If the matrix option is not suitable for your needs, a simple shell script that loops through and tests all your nested applications (directories) might be appropriate. In this case, you ignore the working-direcoty directive in the workflow YAML, and let the script cd to each of them.

For example:

#!/usr/bin/env bash

dirs=( some-dir other-dir )

for dir in "${dirs[@]}"; do
  pushd "$dir"
  pwd    # Do something more significant here
  popd
done

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-2024 STACKOOM.COM