This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| # Example: Detecting Git Changes and Calling Reusable Workflow | ||
| # This workflow detects changed packages and passes them to the reusable workflow | ||
| name: Example - Build with Git Change Detection | ||
| on: | ||
| pull_request: | ||
| branches: | ||
| - Release_Source | ||
| - main | ||
| workflow_dispatch: | ||
| inputs: | ||
| target_branch: | ||
| description: 'Base branch for comparison' | ||
| required: false | ||
| type: string | ||
| default: 'Release_Source' | ||
| env: | ||
| PACKAGES_FOLDER: 'packages' | ||
| jobs: | ||
| # Detect which packages have changed | ||
| detect_changed_packages: | ||
| name: Detect Changed Packages | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| matrix: ${{ steps.set-matrix.outputs.matrix }} | ||
| has-packages: ${{ steps.set-matrix.outputs.has-packages }} | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| fetch-depth: 0 | ||
| - name: Detect Changed Folders in packages/ | ||
| id: set-matrix | ||
| shell: bash | ||
| run: | | ||
| # Determine the base ref | ||
| if [ "${{ github.event_name }}" = "pull_request" ]; then | ||
| BASE_REF="${{ github.event.pull_request.base.ref }}" | ||
| else | ||
| BASE_REF="${{ inputs.target_branch }}" | ||
| fi | ||
| echo "Comparing against: origin/$BASE_REF" | ||
| # Get the list of changed files | ||
| changed_files=$(git diff --name-only origin/$BASE_REF HEAD) | ||
| # Extract unique package folders (first directory under packages/) | ||
| packages=() | ||
| while IFS= read -r file; do | ||
| if [[ $file =~ ^${{ env.PACKAGES_FOLDER }}/([^/]+) ]]; then | ||
| package_name="${BASH_REMATCH[1]}" | ||
| if [[ ! " ${packages[@]} " =~ " ${package_name} " ]]; then | ||
| packages+=("$package_name") | ||
| fi | ||
| fi | ||
| done <<< "$changed_files" | ||
| if [ ${#packages[@]} -eq 0 ]; then | ||
| echo "No changes in ${{ env.PACKAGES_FOLDER }}/ folder detected" | ||
| matrix='[{"package":""}]' | ||
| echo "has-packages=false" >> $GITHUB_OUTPUT | ||
| else | ||
| echo "Detected changed packages: ${packages[*]}" | ||
| # Sort packages and convert to JSON array | ||
| IFS=$'\n' sorted=($(sort <<<"${packages[*]}")) | ||
| unset IFS | ||
| matrix="[" | ||
| for i in "${!sorted[@]}"; do | ||
| if [ $i -gt 0 ]; then | ||
| matrix="$matrix," | ||
| fi | ||
| matrix="$matrix{\"package\":\"${sorted[$i]}\"}" | ||
| done | ||
| matrix="$matrix]" | ||
| echo "has-packages=true" >> $GITHUB_OUTPUT | ||
| fi | ||
| echo "matrix=$matrix" | ||
| echo "matrix=$matrix" >> $GITHUB_OUTPUT | ||
| # Call the reusable workflow with the detected packages | ||
| build_and_validate: | ||
| name: Build and Validate Packages | ||
| needs: detect_changed_packages | ||
| if: needs.detect_changed_packages.outputs.has-packages == 'true' | ||
| uses: ./.github/workflows/linux/reusable-build-validate.yml | ||
|
Check failure on line 91 in .github/workflows/example-build-git-changes.yml
|
||
| with: | ||
| project-name: 'Demo-PSQL' | ||
| packages-matrix: ${{ needs.detect_changed_packages.outputs.matrix }} | ||
| packages-folder: 'packages' | ||
| agent-jar-path: '/home/runner/DBmaestroAgent.jar' | ||
| use-ssl: 'True' | ||
| auth-type: 'DBmaestroAccount' | ||
| package-type: 'Regular' | ||
| runner: 'dbmaestro-linux' | ||
| secrets: | ||
| dbmaestro-server: ${{ vars.DBMAESTRO_SERVER }} | ||
| dbmaestro-user: ${{ secrets.DBMAESTRO_USER }} | ||
| dbmaestro-password: ${{ secrets.DBMAESTRO_PASSWORD }} | ||
| # Optional: Post-processing job for PR comments | ||
| post_process: | ||
| name: Post-Process Results | ||
| runs-on: ubuntu-latest | ||
| needs: [detect_changed_packages, build_and_validate] | ||
| if: always() && github.event_name == 'pull_request' | ||
| steps: | ||
| - name: Check Results | ||
| run: | | ||
| echo "Packages detected: ${{ needs.detect_changed_packages.outputs.has-packages }}" | ||
| echo "Package list: ${{ needs.detect_changed_packages.outputs.matrix }}" | ||
| - name: Comment on PR (No Changes) | ||
| if: needs.detect_changed_packages.outputs.has-packages == 'false' | ||
| run: | | ||
| curl -X POST \ | ||
| -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | ||
| -H "Accept: application/vnd.github.v3+json" \ | ||
| -H "Content-Type: application/json" \ | ||
| "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \ | ||
| -d "{\"body\":\":information_source: **No Package Changes Detected**\n\nNo changes found in the packages/ folder.\"}" | ||
| - name: Comment on PR (Success) | ||
| if: needs.detect_changed_packages.outputs.has-packages == 'true' && needs.build_and_validate.result == 'success' | ||
| run: | | ||
| curl -X POST \ | ||
| -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | ||
| -H "Accept: application/vnd.github.v3+json" \ | ||
| -H "Content-Type: application/json" \ | ||
| "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \ | ||
| -d "{\"body\":\":white_check_mark: **All Packages Built and Validated Successfully**\n\nPackages: \`${{ needs.detect_changed_packages.outputs.matrix }}\`\n\nReady to merge!\"}" | ||
| - name: Comment on PR (Failure) | ||
| if: needs.detect_changed_packages.outputs.has-packages == 'true' && needs.build_and_validate.result == 'failure' | ||
| run: | | ||
| curl -X POST \ | ||
| -H "Authorization: Bearer ${{ secrets.GITHUB_TOKEN }}" \ | ||
| -H "Accept: application/vnd.github.v3+json" \ | ||
| -H "Content-Type: application/json" \ | ||
| "https://api.github.com/repos/${{ github.repository }}/issues/${{ github.event.pull_request.number }}/comments" \ | ||
| -d "{\"body\":\":x: **Package Build or Validation Failed**\n\nPackages: \`${{ needs.detect_changed_packages.outputs.matrix }}\`\n\nPlease check the workflow logs for details.\"}" | ||