Search code examples
githubcontinuous-integrationyamlgithub-actions

Reuse portion of github action across jobs


I have a workflow for CI in a monorepo, for this workflow two projects end up being built. The jobs run fine, however, I'm wondering if there is a way to remove the duplication in this workflow.yml file with the setting up of the runner for the job. I have them split so they run in parallel as they do not rely on one another and to be faster to complete. It's a big time difference in 5 minutes vs. 10+ when waiting for the CI to finish.

jobs:
  job1:
    name: PT.W Build
    runs-on: macos-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v1

      - name: Setup SSH-Agent
        uses: webfactory/[email protected]
        with:
          ssh-private-key: |
            ${{ secrets.SSH_PRIVATE_KEY }}

      - name: Setup JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      - name: Setup Permobil-Client
        run: |
          echo no | npm i -g nativescript
          tns usage-reporting disable
          tns error-reporting disable
          npm run setup.all

      - name: Build PT.W Android
        run: |
          cd apps/wear/pushtracker
          tns build android --env.uglify

  job2:
    name: SD.W Build
    runs-on: macos-latest
    steps:
      - name: Checkout Repo
        uses: actions/checkout@v1

      - name: Setup SSH-Agent
        uses: webfactory/[email protected]
        with:
          ssh-private-key: |
            ${{ secrets.SSH_PRIVATE_KEY }}

      - name: Setup JDK 1.8
        uses: actions/setup-java@v1
        with:
          java-version: 1.8

      - name: Setup Permobil-Client
        run: |
          echo no | npm i -g nativescript
          tns usage-reporting disable
          tns error-reporting disable
          npm run setup.all

      - name: Build SD.W Android
        run: |
          cd apps/wear/smartdrive
          tns build android --env.uglify

You can see here the jobs have almost an identical process, it's just the building of the different apps themselves. I'm wondering if there is a way to take the duplicate blocks in the jobs and create a way to only write that once and reuse it in both jobs.


Solution

  • As I know currently there is no way to reuse steps
    but in this case, you can use strategy for parallel build and different variation:

    jobs:
      build:
        name: Build
        runs-on: macos-latest
        strategy:
          matrix:
            build-dir: ['apps/wear/pushtracker', 'apps/wear/smartdrive']
        steps:
          - name: Checkout Repo
            uses: actions/checkout@v1
    
          - name: Setup SSH-Agent
            uses: webfactory/[email protected]
            with:
              ssh-private-key: |
                ${{ secrets.SSH_PRIVATE_KEY }}
    
          - name: Setup JDK 1.8
            uses: actions/setup-java@v1
            with:
              java-version: 1.8
    
          - name: Setup Permobil-Client
            run: |
              echo no | npm i -g nativescript
              tns usage-reporting disable
              tns error-reporting disable
              npm run setup.all
    
          - name: Build Android
            run: |
              cd ${{ matrix.build-dir }}
              tns build android --env.uglify
    

    For more information please visit https://help.github.com/en/actions/reference/workflow-syntax-for-github-actions#jobsjob_idstrategy