|
| 1 | +name: Build & Publish Images |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main] |
| 6 | + paths-ignore: |
| 7 | + - 'README.md' |
| 8 | + - 'LICENSE' |
| 9 | + - '.gitignore' |
| 10 | + workflow_dispatch: |
| 11 | + inputs: |
| 12 | + image: |
| 13 | + description: 'Specific image directory to build (leave empty to build all)' |
| 14 | + required: false |
| 15 | + type: string |
| 16 | + |
| 17 | +jobs: |
| 18 | + detect: |
| 19 | + name: Detect changed images |
| 20 | + runs-on: ubuntu-latest |
| 21 | + outputs: |
| 22 | + matrix: ${{ steps.changes.outputs.matrix }} |
| 23 | + has_images: ${{ steps.changes.outputs.has_images }} |
| 24 | + steps: |
| 25 | + - uses: actions/checkout@v4 |
| 26 | + with: |
| 27 | + fetch-depth: 0 |
| 28 | + |
| 29 | + - name: Determine which images to build |
| 30 | + id: changes |
| 31 | + run: | |
| 32 | + if [[ -n "${{ inputs.image }}" ]]; then |
| 33 | + # Manual trigger: specific image |
| 34 | + if [[ ! -f "${{ inputs.image }}/Dockerfile" ]]; then |
| 35 | + echo "::error::No Dockerfile found in ${{ inputs.image }}/" |
| 36 | + exit 1 |
| 37 | + fi |
| 38 | + dirs='["${{ inputs.image }}"]' |
| 39 | +
|
| 40 | + elif [[ "${{ github.event_name }}" == "workflow_dispatch" ]]; then |
| 41 | + # Manual trigger without input: build all |
| 42 | + dirs=$(find . -maxdepth 2 -name Dockerfile -exec dirname {} \; \ |
| 43 | + | sed 's|^./||' | sort | jq -R . | jq -s -c .) |
| 44 | +
|
| 45 | + else |
| 46 | + # Push event: check what changed |
| 47 | + prev_commit="${{ github.event.before }}" |
| 48 | +
|
| 49 | + # Handle first push (no previous commit) |
| 50 | + if [[ "$prev_commit" == "0000000000000000000000000000000000000000" ]]; then |
| 51 | + dirs=$(find . -maxdepth 2 -name Dockerfile -exec dirname {} \; \ |
| 52 | + | sed 's|^./||' | sort | jq -R . | jq -s -c .) |
| 53 | + else |
| 54 | + # If VERSION changed, rebuild everything |
| 55 | + if git diff --name-only "$prev_commit" HEAD | grep -q '^VERSION$'; then |
| 56 | + dirs=$(find . -maxdepth 2 -name Dockerfile -exec dirname {} \; \ |
| 57 | + | sed 's|^./||' | sort | jq -R . | jq -s -c .) |
| 58 | + else |
| 59 | + # Only build directories that changed |
| 60 | + dirs=$(git diff --name-only "$prev_commit" HEAD \ |
| 61 | + | grep '/' | cut -d'/' -f1 | sort -u \ |
| 62 | + | while read -r dir; do |
| 63 | + [[ -f "$dir/Dockerfile" ]] && echo "$dir" |
| 64 | + done | jq -R . | jq -s -c .) |
| 65 | + fi |
| 66 | + fi |
| 67 | + fi |
| 68 | +
|
| 69 | + if [[ "$dirs" == "[]" || "$dirs" == "null" || -z "$dirs" ]]; then |
| 70 | + echo "has_images=false" >> "$GITHUB_OUTPUT" |
| 71 | + echo "matrix=[]" >> "$GITHUB_OUTPUT" |
| 72 | + else |
| 73 | + echo "has_images=true" >> "$GITHUB_OUTPUT" |
| 74 | + echo "matrix=$dirs" >> "$GITHUB_OUTPUT" |
| 75 | + fi |
| 76 | +
|
| 77 | + echo "Images to build: $dirs" |
| 78 | +
|
| 79 | + build: |
| 80 | + name: Build ${{ matrix.image }} |
| 81 | + needs: detect |
| 82 | + if: needs.detect.outputs.has_images == 'true' |
| 83 | + runs-on: ubuntu-latest |
| 84 | + permissions: |
| 85 | + contents: read |
| 86 | + packages: write |
| 87 | + strategy: |
| 88 | + fail-fast: false |
| 89 | + matrix: |
| 90 | + image: ${{ fromJson(needs.detect.outputs.matrix) }} |
| 91 | + steps: |
| 92 | + - uses: actions/checkout@v4 |
| 93 | + |
| 94 | + - name: Read version |
| 95 | + id: version |
| 96 | + run: echo "version=$(cat VERSION)" >> "$GITHUB_OUTPUT" |
| 97 | + |
| 98 | + - name: Set up QEMU |
| 99 | + uses: docker/setup-qemu-action@v3 |
| 100 | + |
| 101 | + - name: Set up Docker Buildx |
| 102 | + uses: docker/setup-buildx-action@v3 |
| 103 | + |
| 104 | + - name: Login to GHCR |
| 105 | + uses: docker/login-action@v3 |
| 106 | + with: |
| 107 | + registry: ghcr.io |
| 108 | + username: ${{ github.actor }} |
| 109 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 110 | + |
| 111 | + - name: Build and push |
| 112 | + uses: docker/build-push-action@v6 |
| 113 | + with: |
| 114 | + context: ${{ matrix.image }} |
| 115 | + platforms: linux/amd64,linux/arm64 |
| 116 | + push: true |
| 117 | + tags: | |
| 118 | + ghcr.io/helmcode/agentcrew-${{ matrix.image }}:${{ steps.version.outputs.version }} |
| 119 | + ghcr.io/helmcode/agentcrew-${{ matrix.image }}:latest |
| 120 | + labels: | |
| 121 | + org.opencontainers.image.source=https://github.com/helmcode/agent_crew_marketplace |
| 122 | + org.opencontainers.image.description=AgentCrew marketplace image - ${{ matrix.image }} |
0 commit comments