How to use GitHub Actions matrix to conditionally build Docker images based on file changes?

I am trying to configure a GitHub Actions workflow to conditionally build and push Docker images based on changes in specific directories. I only want to build the images associated with the changed paths and skip the others. Here’s a simplified version of my workflow. I want to build all images when there is a change in the library, and build only the relevant image based on the file changes. For example, build the collector image only if there is a change in the collector.

The matrix includes a job Build and push Docker Images (false) for instance when there is a change library (needs.changes.outputs.library == ‘true’)

How can I properly configure the matrix so that it only includes images for which their corresponding path has changed, based on the output from the changes job?

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
<code>jobs:
changes:
name: Detect changes
runs-on: ubuntu-latest
outputs:
library: ${{ steps.filter.outputs.library }}
workers: ${{ steps.filter.outputs.workers }}
collector: ${{ steps.filter.outputs.collector }}
api: ${{ steps.filter.outputs.api }}
webhook: ${{ steps.filter.outputs.webhook }}
steps:
- name: Checkout
uses: actions/checkout@v2
- name: Filter paths
uses: dorny/paths-filter@v2
id: filter
with:
token: ${{ secrets.GITHUB_TOKEN }}
filters: |
library:
- 'package.json'
- 'package-lock.json'
- 'shared/**'
workers:
- 'workers/**'
- 'prisma/**'
collector:
- 'collector/**'
api:
- 'api/**'
webhook:
- 'webhook/**'
docker:
name: Build and push Docker Images
runs-on: ubuntu-latest
needs: changes
strategy:
matrix:
image:
- collector
- workers
- api
- webhook
include:
- image: ${{ needs.changes.outputs.collector == 'true' || needs.changes.outputs.library == 'true' && 'collector' }}
- image: ${{ needs.changes.outputs.workers == 'true' || needs.changes.outputs.library == 'true' && 'workers' }}
- image: ${{ needs.changes.outputs.api == 'true' || needs.changes.outputs.library == 'true' && 'api' }}
- image: ${{ needs.changes.outputs.collector == 'true' || needs.changes.outputs.webhook == 'true' && 'webhook' }}
exclude:
- image: ${{ needs.changes.outputs.collector == 'false' || needs.changes.outputs.library == 'false' && 'collector' }}
- image: ${{ needs.changes.outputs.workers == 'false' || needs.changes.outputs.library == 'false' && 'workers' }}
- image: ${{ needs.changes.outputs.api == 'false' || needs.changes.outputs.library == 'false' && 'api' }}
- image: ${{ needs.changes.outputs.collector == 'false' || needs.changes.outputs.webhook == 'false' && 'webhook' }}
steps:
- name: Build and push
run: |
echo "Building and pushing Docker image ${{ matrix.image }}"
</code>
<code>jobs: changes: name: Detect changes runs-on: ubuntu-latest outputs: library: ${{ steps.filter.outputs.library }} workers: ${{ steps.filter.outputs.workers }} collector: ${{ steps.filter.outputs.collector }} api: ${{ steps.filter.outputs.api }} webhook: ${{ steps.filter.outputs.webhook }} steps: - name: Checkout uses: actions/checkout@v2 - name: Filter paths uses: dorny/paths-filter@v2 id: filter with: token: ${{ secrets.GITHUB_TOKEN }} filters: | library: - 'package.json' - 'package-lock.json' - 'shared/**' workers: - 'workers/**' - 'prisma/**' collector: - 'collector/**' api: - 'api/**' webhook: - 'webhook/**' docker: name: Build and push Docker Images runs-on: ubuntu-latest needs: changes strategy: matrix: image: - collector - workers - api - webhook include: - image: ${{ needs.changes.outputs.collector == 'true' || needs.changes.outputs.library == 'true' && 'collector' }} - image: ${{ needs.changes.outputs.workers == 'true' || needs.changes.outputs.library == 'true' && 'workers' }} - image: ${{ needs.changes.outputs.api == 'true' || needs.changes.outputs.library == 'true' && 'api' }} - image: ${{ needs.changes.outputs.collector == 'true' || needs.changes.outputs.webhook == 'true' && 'webhook' }} exclude: - image: ${{ needs.changes.outputs.collector == 'false' || needs.changes.outputs.library == 'false' && 'collector' }} - image: ${{ needs.changes.outputs.workers == 'false' || needs.changes.outputs.library == 'false' && 'workers' }} - image: ${{ needs.changes.outputs.api == 'false' || needs.changes.outputs.library == 'false' && 'api' }} - image: ${{ needs.changes.outputs.collector == 'false' || needs.changes.outputs.webhook == 'false' && 'webhook' }} steps: - name: Build and push run: | echo "Building and pushing Docker image ${{ matrix.image }}" </code>
jobs:
  changes:
    name: Detect changes
    runs-on: ubuntu-latest
    outputs:
      library: ${{ steps.filter.outputs.library }}
      workers: ${{ steps.filter.outputs.workers }}
      collector: ${{ steps.filter.outputs.collector }}
      api: ${{ steps.filter.outputs.api }}
      webhook: ${{ steps.filter.outputs.webhook }}
    steps:
      - name: Checkout
        uses: actions/checkout@v2

      - name: Filter paths
        uses: dorny/paths-filter@v2
        id: filter
        with:
          token: ${{ secrets.GITHUB_TOKEN }}
          filters: |
            library:
              - 'package.json'
              - 'package-lock.json'
              - 'shared/**'
            workers:
              - 'workers/**'
              - 'prisma/**'
            collector:
              - 'collector/**'
            api:
              - 'api/**'
            webhook:
              - 'webhook/**'

  docker:
    name: Build and push Docker Images
    runs-on: ubuntu-latest
    needs: changes
    strategy:
      matrix:
        image:
          - collector
          - workers
          - api
          - webhook
    include:
      - image: ${{ needs.changes.outputs.collector == 'true' || needs.changes.outputs.library == 'true' && 'collector' }}
      - image: ${{ needs.changes.outputs.workers == 'true' || needs.changes.outputs.library == 'true' && 'workers' }}
      - image: ${{ needs.changes.outputs.api == 'true' || needs.changes.outputs.library == 'true' && 'api' }}
      - image: ${{ needs.changes.outputs.collector == 'true' || needs.changes.outputs.webhook == 'true' && 'webhook' }}
    exclude:
      - image: ${{ needs.changes.outputs.collector == 'false' || needs.changes.outputs.library == 'false' && 'collector' }}
      - image: ${{ needs.changes.outputs.workers == 'false' || needs.changes.outputs.library == 'false' && 'workers' }}
      - image: ${{ needs.changes.outputs.api == 'false' || needs.changes.outputs.library == 'false' && 'api' }}
      - image: ${{ needs.changes.outputs.collector == 'false' || needs.changes.outputs.webhook == 'false' && 'webhook' }}
    steps:
      - name: Build and push
        run: |
          echo "Building and pushing Docker image ${{ matrix.image }}"

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật