From bfbf97fd166ed052f082fc6d189ab8fa57fbc53f Mon Sep 17 00:00:00 2001 From: Johnson Sun Date: Fri, 13 Dec 2024 16:54:20 +0800 Subject: [PATCH] ci: Reuse docker cache --- .github/workflows/build-gazebo-world-ws.yaml | 58 ++++++++++++++++---- .github/workflows/build-template-ws.yaml | 45 +++++++++++++-- 2 files changed, 86 insertions(+), 17 deletions(-) diff --git a/.github/workflows/build-gazebo-world-ws.yaml b/.github/workflows/build-gazebo-world-ws.yaml index a83c344c..9f0dac07 100644 --- a/.github/workflows/build-gazebo-world-ws.yaml +++ b/.github/workflows/build-gazebo-world-ws.yaml @@ -1,20 +1,49 @@ name: Build Docker Image for gazebo-world-ws on: - push: - branches: - - "main" - tags: - - v* - paths: - - .github/workflows/build-gazebo-world-ws.yaml - - gazebo_world_ws/docker/Dockerfile - - gazebo_world_ws/docker/.dockerignore - - gazebo_world_ws/docker/.bashrc + workflow_run: + workflows: ["Build Docker Image for template-ws"] + types: [completed] jobs: + paths-filter: + if: ${{ github.event.workflow_run.conclusion == 'success' }} + runs-on: ubuntu-latest + outputs: + results: ${{ steps.filter.outputs.results }} + steps: + # Ref: https://github.com/dorny/paths-filter/issues/147#issuecomment-1287800590 + - name: Download a single artifact + uses: dawidd6/action-download-artifact@v7 + with: + workflow: build-template-ws.yaml + name: original-refs + workflow_conclusion: success + - name: set REF_BASE to env + run: | + echo "BASE=$(cat base.txt)" >> $GITHUB_ENV + echo "CURRENT_BRANCH=$(cat current-branch.txt)" >> $GITHUB_ENV + - uses: actions/checkout@v4 + - uses: dorny/paths-filter@v3 + id: filter + with: + base: ${{ env.BASE }} + ref: ${{ env.CURRENT_BRANCH }} + filters: | + results: + - .github/workflows/build-gazebo-world-ws.yaml + - gazebo_world_ws/docker/Dockerfile + - gazebo_world_ws/docker/.dockerignore + - gazebo_world_ws/docker/.bashrc + - name: Changes matched + if: steps.filter.outputs.results == 'true' + run: echo "Changes matched, will build image" + - name: Changes didn't match + if: steps.filter.outputs.results != 'true' + run: echo "Changes didn't match, will NOT build image" docker: - if: github.repository == 'j3soon/ros2-essentials' + needs: paths-filter + if: ${{ needs.paths-filter.outputs.results == 'true' }} runs-on: ubuntu-latest steps: - name: Maximize build space @@ -42,6 +71,8 @@ jobs: # Output the environment variable # Ref: https://stackoverflow.com/a/57989070 echo "VERSION=$VERSION" >> $GITHUB_ENV + # Print for debugging purpose + echo "VERSION=$VERSION" - name: Docker meta id: meta # Ref: https://github.com/docker/metadata-action @@ -57,9 +88,14 @@ jobs: with: username: ${{ secrets.DOCKERHUB_USERNAME }} password: ${{ secrets.DOCKERHUB_TOKEN }} + - name: Pull j3soon/ros2-template-ws (amd64) + run: docker pull --platform linux/amd64 j3soon/ros2-template-ws + - name: Pull j3soon/ros2-template-ws (arm64) + run: docker pull --platform linux/arm64 j3soon/ros2-template-ws - name: Build and push uses: docker/build-push-action@v4 with: context: gazebo_world_ws/docker + cache-from: j3soon/ros2-template-ws push: true tags: ${{ steps.meta.outputs.tags }} diff --git a/.github/workflows/build-template-ws.yaml b/.github/workflows/build-template-ws.yaml index bc6963a1..c4e94ec5 100644 --- a/.github/workflows/build-template-ws.yaml +++ b/.github/workflows/build-template-ws.yaml @@ -6,16 +6,49 @@ on: - "main" tags: - v* - paths: - - .github/workflows/build-template-ws.yaml - - template_ws/docker/Dockerfile - - template_ws/docker/.dockerignore - - template_ws/docker/.bashrc jobs: - docker: + paths-filter: if: github.repository == 'j3soon/ros2-essentials' runs-on: ubuntu-latest + outputs: + results: ${{ steps.filter.outputs.results }} + steps: + - uses: actions/checkout@v4 + - uses: dorny/paths-filter@v3 + id: filter + with: + filters: | + results: + - .github/workflows/build-template-ws.yaml + - template_ws/docker/Dockerfile + - template_ws/docker/.dockerignore + - template_ws/docker/.bashrc + - name: Changes matched + if: steps.filter.outputs.results == 'true' + run: echo "Changes matched, will build image" + - name: Changes didn't match + if: steps.filter.outputs.results != 'true' + run: echo "Changes didn't match, will NOT build image" + # Ref: https://github.com/dorny/paths-filter/issues/147#issuecomment-1287800590 + - name: Save base ref info + run: | + BASE=${{ github.event.before }} + CURRENT_BRANCH=${{ github.ref }} + echo $BASE > base.txt + echo $CURRENT_BRANCH > current-branch.txt + - name: Upload base ref info + uses: actions/upload-artifact@v4 + with: + name: original-refs + path: | + base.txt + current-branch.txt + retention-days: 1 + docker: + needs: paths-filter + if: ${{ needs.paths-filter.outputs.results == 'true' }} + runs-on: ubuntu-latest steps: - name: Maximize build space uses: easimon/maximize-build-space@master