Nona
Nona

Reputation: 5462

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/[email protected]
      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/[email protected]
      working-directory: [./app1, ./app2]
      with:
        run: errcheck
        token: ${{ secrets.GITHUB_TOKEN }}

Upvotes: 5

Views: 7647

Answers (1)

DannyB
DannyB

Reputation: 14776

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:

enter image description here

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

Upvotes: 9

Related Questions