Reuse portion of github action across jobs

£可爱£侵袭症+ 提交于 2021-02-08 14:18:13

问题


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/ssh-agent@v0.2.0
        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/ssh-agent@v0.2.0
        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.


回答1:


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/ssh-agent@v0.2.0
        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




回答2:


Use composite actions to reuse steps, as suggested in this answer.



来源:https://stackoverflow.com/questions/59757355/reuse-portion-of-github-action-across-jobs

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!