|
| 1 | +name: Build EC2 Compute Images |
| 2 | + |
| 3 | +on: |
| 4 | + pull_request: |
| 5 | + paths: |
| 6 | + - 'ubuntu/standard/*/Dockerfile' |
| 7 | + - 'ubuntu/standard/*/**' |
| 8 | + - 'al/x86_64/standard/*/Dockerfile' |
| 9 | + - 'al/x86_64/standard/*/**' |
| 10 | + - 'al/aarch64/standard/*/Dockerfile' |
| 11 | + - 'al/aarch64/standard/*/**' |
| 12 | + workflow_dispatch: |
| 13 | + inputs: |
| 14 | + images: |
| 15 | + description: 'Comma-separated image paths to build (e.g. ubuntu/standard/8.0,al/x86_64/standard/6.0). Leave empty to build all.' |
| 16 | + required: false |
| 17 | + |
| 18 | +jobs: |
| 19 | + detect-changes: |
| 20 | + runs-on: ubuntu-latest |
| 21 | + outputs: |
| 22 | + images: ${{ steps.changed.outputs.images }} |
| 23 | + steps: |
| 24 | + - uses: actions/checkout@v4 |
| 25 | + with: |
| 26 | + fetch-depth: 0 |
| 27 | + |
| 28 | + - name: Detect changed images |
| 29 | + id: changed |
| 30 | + run: | |
| 31 | + # Manual override via workflow_dispatch |
| 32 | + if [ -n "${{ inputs.images }}" ]; then |
| 33 | + JSON=$(echo "${{ inputs.images }}" | tr ',' '\n' | jq -R . | jq -sc .) |
| 34 | + echo "images=$JSON" >> "$GITHUB_OUTPUT" |
| 35 | + exit 0 |
| 36 | + fi |
| 37 | +
|
| 38 | + # Build all when triggered manually without specifying images |
| 39 | + if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then |
| 40 | + JSON=$(find ubuntu/standard al/x86_64/standard al/aarch64/standard -name Dockerfile -printf '%h\n' 2>/dev/null | sort | jq -R . | jq -sc .) |
| 41 | + echo "images=$JSON" >> "$GITHUB_OUTPUT" |
| 42 | + exit 0 |
| 43 | + fi |
| 44 | +
|
| 45 | + # Auto-detect from PR diff |
| 46 | + IMAGES=() |
| 47 | + CHANGED_FILES=$(git diff --name-only ${{ github.event.pull_request.base.sha }} ${{ github.sha }}) |
| 48 | +
|
| 49 | + for dir in $(find ubuntu/standard al/x86_64/standard al/aarch64/standard -name Dockerfile -printf '%h\n' 2>/dev/null | sort); do |
| 50 | + if echo "$CHANGED_FILES" | grep -q "^${dir}/"; then |
| 51 | + IMAGES+=("$dir") |
| 52 | + fi |
| 53 | + done |
| 54 | +
|
| 55 | + if [ ${#IMAGES[@]} -eq 0 ]; then |
| 56 | + echo "images=[]" >> "$GITHUB_OUTPUT" |
| 57 | + else |
| 58 | + JSON=$(printf '%s\n' "${IMAGES[@]}" | jq -R . | jq -sc .) |
| 59 | + echo "images=$JSON" >> "$GITHUB_OUTPUT" |
| 60 | + fi |
| 61 | +
|
| 62 | + - name: Summary |
| 63 | + run: echo "Changed images - ${{ steps.changed.outputs.images }}" |
| 64 | + |
| 65 | + build: |
| 66 | + needs: detect-changes |
| 67 | + if: needs.detect-changes.outputs.images != '[]' |
| 68 | + runs-on: ubuntu-latest |
| 69 | + strategy: |
| 70 | + fail-fast: false |
| 71 | + matrix: |
| 72 | + image: ${{ fromJson(needs.detect-changes.outputs.images) }} |
| 73 | + steps: |
| 74 | + - uses: actions/checkout@v4 |
| 75 | + |
| 76 | + - name: Build ${{ matrix.image }} |
| 77 | + run: docker build -t codebuild-test/${{ matrix.image }} ${{ matrix.image }} |
0 commit comments