diff --git a/.github/workflows/php-fpm.yml b/.github/workflows/php-fpm.yml index 393b132..120159c 100644 --- a/.github/workflows/php-fpm.yml +++ b/.github/workflows/php-fpm.yml @@ -16,13 +16,82 @@ on: workflow_dispatch: inputs: lane: - description: 'Lane to build (e.g. 7.4). Use "all" to build all lanes. Leave blank to build based on changes' + description: 'Lane to build (e.g. 7.4). Use "all" to build all lanes. Leave blank to build based on changes.' required: false default: "" jobs: + lanes: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout repository + uses: actions/checkout@v4 + with: + fetch-depth: 0 # Fetch all history for accurate change detection + + - name: Get all PHP lanes + id: get-lanes + run: | + lanes=$(find php-fpm -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | jq -R . | jq -s .) + echo "lanes=$lanes" >> $GITHUB_OUTPUT + shell: bash + + - name: Get changed files + id: changed-files + uses: tj-actions/changed-files@v44 + with: + files: | + php-fpm/** + shared/php-fpm/** + .github/workflows/php-fpm.yml + + - name: Determine lanes to build + id: set-matrix + shell: bash + run: | + all_lanes='${{ steps.get-lanes.outputs.lanes }}' + req_lane="${{ github.event.inputs.lane }}" + changed_files="${{ steps.changed-files.outputs.all_changed_files }}" + + lanes_to_build="[]" + build_all=false + + # 1. Handle workflow_dispatch input + if [[ -n "$req_lane" ]]; then + if [[ "$req_lane" == "all" ]]; then + build_all=true + else + lanes_to_build="[\"$req_lane\"]" + fi + else + # 2. Check for changes that trigger a full rebuild + for file in $changed_files; do + if [[ "$file" == ".github/workflows/php-fpm.yml" || "$file" == shared/php-fpm/* ]]; then + build_all=true + break + fi + done + fi + + # 3. Build matrix based on flags + if [[ "$build_all" == true ]]; then + lanes_to_build="$all_lanes" + elif [[ -n "$changed_files" && "$lanes_to_build" == "[]" ]]; then + # 4. If not a full build, determine changed lanes + lanes=$(echo "$changed_files" | tr ' ' '\n' | grep '^php-fpm/' | cut -d'/' -f2 | sort -u | jq -R . | jq -s .) + if [[ -n "$lanes" && "$lanes" != "[]" ]]; then + lanes_to_build="$lanes" + fi + fi + + matrix="{\"lane\":$lanes_to_build}" + echo "matrix=$matrix" >> $GITHUB_OUTPUT + build: needs: lanes + if: ${{ needs.lanes.outputs.matrix != '{"lane":[]}' }} runs-on: ubuntu-latest strategy: fail-fast: false @@ -30,9 +99,6 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@v4 - with: - # Fetch all history for accurate change detection - fetch-depth: 0 - name: Set up Docker Buildx uses: docker/setup-buildx-action@v3 @@ -62,73 +128,3 @@ jobs: tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} platforms: linux/amd64 - - lanes: - runs-on: ubuntu-latest - outputs: - matrix: ${{ steps.set-matrix.outputs.matrix }} - steps: - - name: Checkout repository - uses: actions/checkout@v4 - with: - fetch-depth: 0 - - - name: Get all PHP lanes - id: get-lanes - run: | - lanes=$(find php-fpm -mindepth 1 -maxdepth 1 -type d -exec basename {} \; | jq -R . | jq -s .) - echo "lanes=$lanes" >> $GITHUB_OUTPUT - shell: bash - - - name: Get changed files - id: changed-files - uses: tj-actions/changed-files@v44 - with: - files: | - php-fpm/** - shared/php-fpm/** - .github/workflows/php-fpm.yml - - - name: Determine lanes to build - id: set-matrix - run: | - all_lanes='${{ steps.get-lanes.outputs.lanes }}' - req_lane="${{ github.event.inputs.lane }}" - - build_all="false" - - # Handle workflow_dispatch input - if [[ -n "$req_lane" ]]; then - if [[ "$req_lane" == "all" ]]; then - build_all="true" - else - # Build only the requested lane - matrix="{\"lane\":[\"$req_lane\"]}" - echo "matrix=$matrix" >> $GITHUB_OUTPUT - exit 0 - fi - fi - - # Rebuild all if workflow or shared files changed - if [[ "${{ steps.changed-files.outputs.any_changed }}" == "true" ]]; then - for file in ${{ steps.changed-files.outputs.all_changed_files }}; do - if [[ "$file" == ".github/workflows/php-fpm.yml" || "$file" == shared/php-fpm/* ]]; then - build_all="true" - break - fi - done - fi - - if [[ "$build_all" == "true" ]]; then - matrix="{\"lane\":$all_lanes}" - else - # Determine changed lanes from php-fpm directory - changed_lanes=$(echo "${{ steps.changed-files.outputs.all_changed_files }}" | tr ' ' '\n' | grep '^php-fpm/' | cut -d'/' -f2 | sort -u | jq -R . | jq -s .) - if [[ -z "$changed_lanes" ]]; then - changed_lanes="[]" - fi - matrix="{\"lane\":$changed_lanes}" - fi - - echo "matrix=$matrix" >> $GITHUB_OUTPUT - shell: bash