adding custom build action support #3
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
| name: Spring Cloud Deploy (Reusable) | ||
| on: | ||
| workflow_call: | ||
| inputs: | ||
| branch: | ||
| description: 'Which branch should be built (for workflow_dispatch)' | ||
| required: false | ||
| type: string | ||
| custom_build_command: | ||
| description: 'Custom run command for the Build and Deploy step (overrides default Maven command)' | ||
| required: false | ||
| type: string | ||
| custom_build_action: | ||
| description: 'Custom action reference for build/test (e.g., ./.github/actions/build-and-test or org/repo/.github/actions/name@ref)' | ||
| required: false | ||
| type: string | ||
| custom_build_action_inputs: | ||
| description: 'JSON string of inputs to pass to custom build action' | ||
| required: false | ||
| type: string | ||
| custom_deploy_command: | ||
| description: 'Custom deploy command to run after custom build action when SHOULD_DEPLOY is true' | ||
| required: false | ||
| type: string | ||
| runs_on: | ||
| description: 'Runner to use for the build job (defaults to ubuntu-latest)' | ||
| required: false | ||
| type: string | ||
| secrets: | ||
| ARTIFACTORY_USERNAME: | ||
| required: true | ||
| ARTIFACTORY_PASSWORD: | ||
| required: true | ||
| COMMERCIAL_ARTIFACTORY_USERNAME: | ||
| required: false | ||
| COMMERCIAL_ARTIFACTORY_PASSWORD: | ||
| required: false | ||
| DOCKERHUB_USERNAME: | ||
| required: true | ||
| DOCKERHUB_TOKEN: | ||
| required: true | ||
| jobs: | ||
| setup: | ||
| runs-on: ubuntu-latest | ||
| outputs: | ||
| matrix: ${{ steps.set-matrix.outputs.matrix }} | ||
| steps: | ||
| - name: Determine branches and JDK versions | ||
| id: set-matrix | ||
| uses: spring-cloud/spring-cloud-github-actions/.github/actions/determine-matrix@main | ||
| with: | ||
| repository: ${{ github.repository }} | ||
| event-name: ${{ github.event_name }} | ||
| ref-name: ${{ github.ref_name }} | ||
| branch: ${{ inputs.branch }} | ||
| build: | ||
| name: Build ${{ matrix.branch }} (JDK ${{ matrix.java-version }}) | ||
| needs: setup | ||
| runs-on: ${{ inputs.runs_on || 'ubuntu-latest' }} | ||
| timeout-minutes: 60 | ||
| strategy: | ||
| fail-fast: false | ||
| matrix: | ||
| include: ${{ fromJson(needs.setup.outputs.matrix) }} | ||
| steps: | ||
| - name: Checkout code | ||
| uses: actions/checkout@v4 | ||
| with: | ||
| ref: ${{ matrix.branch }} | ||
| clean: true | ||
| - name: Set up JDK ${{ matrix.java-version }} | ||
| uses: actions/setup-java@v4 | ||
| with: | ||
| java-version: ${{ matrix.java-version }} | ||
| distribution: 'temurin' | ||
| cache: 'maven' | ||
| - name: Configure Maven settings | ||
| uses: s4u/maven-settings-action@v3.0.0 | ||
| with: | ||
| servers: | | ||
| [{ | ||
| "id": "repo.spring.io", | ||
| "username": "${{ secrets.ARTIFACTORY_USERNAME }}", | ||
| "password": "${{ secrets.ARTIFACTORY_PASSWORD }}" | ||
| }, | ||
| { | ||
| "id": "spring-commercial-snapshot", | ||
| "username": "${{ secrets.COMMERCIAL_ARTIFACTORY_USERNAME }}", | ||
| "password": "${{ secrets.COMMERCIAL_ARTIFACTORY_PASSWORD }}" | ||
| }, | ||
| { | ||
| "id": "spring-commercial-release", | ||
| "username": "${{ secrets.COMMERCIAL_ARTIFACTORY_USERNAME }}", | ||
| "password": "${{ secrets.COMMERCIAL_ARTIFACTORY_PASSWORD }}" | ||
| }] | ||
| - name: Login to Docker Hub | ||
| uses: docker/login-action@v3 | ||
| with: | ||
| username: ${{ secrets.DOCKERHUB_USERNAME }} | ||
| password: ${{ secrets.DOCKERHUB_TOKEN }} | ||
| - name: Stop running Docker containers | ||
| continue-on-error: true | ||
| run: | | ||
| #!/bin/bash | ||
| if command -v timeout &> /dev/null; then | ||
| timeout 10s docker ps -a -q | xargs -n 1 -P 8 -I {} docker stop {} || echo "Failed to stop docker... Hopefully you know what you're doing" | ||
| fi | ||
| - name: Verify Maven installation | ||
| run: ./mvnw --version | ||
| - name: Set build configuration | ||
| run: | | ||
| # Determine if docs profile should be included and if artifacts should be deployed | ||
| # Logic: Include docs and deploy if (has-jdk8=true AND java-version=8) OR (has-jdk8=false AND java-version=17) | ||
| BUILD_WITH_DOCS=false | ||
| SHOULD_DEPLOY=false | ||
| if [[ ("${{ matrix.has-jdk8 }}" == "true" && "${{ matrix.java-version }}" == "8") ]] || [[ ("${{ matrix.has-jdk8 }}" == "false" && "${{ matrix.java-version }}" == "17") ]]; then | ||
| BUILD_WITH_DOCS=true | ||
| SHOULD_DEPLOY=true | ||
| echo "Docs profile will be included in this build" | ||
| echo "This build will deploy artifacts" | ||
| else | ||
| echo "Docs profile will NOT be included in this build" | ||
| echo "This build will NOT deploy artifacts" | ||
| fi | ||
| echo "BUILD_WITH_DOCS=$BUILD_WITH_DOCS" >> $GITHUB_ENV | ||
| echo "SHOULD_DEPLOY=$SHOULD_DEPLOY" >> $GITHUB_ENV | ||
| echo "Build configuration: BUILD_WITH_DOCS=$BUILD_WITH_DOCS, SHOULD_DEPLOY=$SHOULD_DEPLOY" | ||
| - name: Custom build action | ||
| if: ${{ inputs.custom_build_action != '' }} | ||
| uses: ${{ inputs.custom_build_action }} | ||
| with: | ||
| java-version: ${{ matrix.java-version }} | ||
| inputs: ${{ inputs.custom_build_action_inputs }} | ||
| - name: Custom deploy command | ||
| if: ${{ inputs.custom_build_action != '' && inputs.custom_deploy_command != '' && env.SHOULD_DEPLOY == 'true' }} | ||
| env: | ||
| CUSTOM_DEPLOY_COMMAND: ${{ inputs.custom_deploy_command }} | ||
| run: | | ||
| # Execute custom deploy command after custom build action | ||
| echo "Running custom deploy command" | ||
| printf '%s\n' "$CUSTOM_DEPLOY_COMMAND" > /tmp/custom-deploy.sh | ||
| chmod +x /tmp/custom-deploy.sh | ||
| bash /tmp/custom-deploy.sh | ||
| - name: Build | ||
| if: ${{ inputs.custom_build_action == '' && inputs.custom_build_command == '' && env.SHOULD_DEPLOY == 'false' }} | ||
| run: | | ||
| # Run Maven install for non-deploying JDK versions (no docs profile needed) | ||
| echo "Building without deployment" | ||
| ./mvnw clean install -Pspring -B -U | ||
| - name: Build and deploy | ||
| if: ${{ inputs.custom_build_action == '' && inputs.custom_build_command == '' && env.SHOULD_DEPLOY == 'true' }} | ||
| run: | | ||
| # Run Maven deploy for the designated JDK version (always includes docs profile) | ||
| echo "Building with docs profile and deploying artifacts" | ||
| ./mvnw clean deploy -Pdocs,deploy,spring -B -U | ||
| - name: Custom build command | ||
| if: ${{ inputs.custom_build_action == '' && inputs.custom_build_command != '' }} | ||
| env: | ||
| CUSTOM_BUILD_COMMAND: ${{ inputs.custom_build_command }} | ||
| run: | | ||
| # Execute custom build command provided by caller | ||
| printf '%s\n' "$CUSTOM_BUILD_COMMAND" > /tmp/build-deploy.sh | ||
| chmod +x /tmp/build-deploy.sh | ||
| bash /tmp/build-deploy.sh | ||