Skip to content

Commit

Permalink
ci: matrix to handle multiple directory changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jaypatel1210 committed Nov 18, 2024
1 parent 28dd0f6 commit 9748e03
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions .github/workflows/docker-publish.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,46 @@ on:
- 'executor/**'

jobs:
check-changes:
runs-on: ubuntu-latest
timeout-minutes: 5
outputs:
matrix: ${{ steps.set-matrix.outputs.matrix }}
steps:
- name: Check out code
uses: actions/checkout@v4
with:
fetch-depth: 2

- name: Check modified directories and set matrix
id: set-matrix
run: |
matrix="{\"include\":["
if git diff --name-only HEAD^ HEAD | grep -q "^firefetch/"; then
matrix="$matrix{\"directory\":\"firefetch\",\"image\":\"firefetch\"}"
fi
if git diff --name-only HEAD^ HEAD | grep -q "^executor/"; then
if [[ "$matrix" != "{\"include\":[" ]]; then
matrix="$matrix,"
fi
matrix="$matrix{\"directory\":\"executor\",\"image\":\"cloud-ide-executor\"}"
fi
matrix="$matrix]}"
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
build:
needs: check-changes
if: fromJson(needs.check-changes.outputs.matrix).include[0]
strategy:
matrix: ${{ fromJson(needs.check-changes.outputs.matrix) }}
runs-on: ubuntu-latest
timeout-minutes: 10

steps:
- name: Checkout repository
- name: Check out code
uses: actions/checkout@v4

- name: Log in to Docker Hub
Expand All @@ -21,17 +56,6 @@ jobs:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Determine changed directory and image name
id: changed_dir
run: |
if [ -d firefetch ]; then
echo "dir=firefetch" >> "$GITHUB_ENV"
echo "image_name=firefetch" >> "$GITHUB_ENV"
elif [ -d executor ]; then
echo "dir=executor" >> "$GITHUB_ENV"
echo "image_name=cloud-ide-executor" >> "$GITHUB_ENV"
fi
- name: Get short SHA
id: short_sha
run: echo "short_sha=$(git rev-parse --short HEAD)" >> "$GITHUB_OUTPUT"
Expand All @@ -40,19 +64,19 @@ jobs:
id: meta
uses: docker/metadata-action@v5
with:
images: jaypatel1210/${{ env.image_name }}
images: jaypatel1210/${{ matrix.image }}
tags: |
type=raw,value=latest
type=raw,value=${{ steps.short_sha.outputs.short_sha }}
type=raw,value=latest
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
context: ${{ env.dir }}
file: ${{ env.dir }}/Dockerfile
context: ${{ matrix.directory }}
file: ${{ matrix.directory }}/Dockerfile
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Expand Down

0 comments on commit 9748e03

Please sign in to comment.