diff --git a/.github/workflows/java-build.yml b/.github/workflows/java-build.yml new file mode 100644 index 0000000..81eebfd --- /dev/null +++ b/.github/workflows/java-build.yml @@ -0,0 +1,119 @@ +name: Build and Test Java + +on: + workflow_call: + inputs: + java-version: + description: 'Java version to use' + default: '17' + type: string + java-distribution: + description: 'Java distribution to use (temurin, zulu, adopt, etc.)' + default: 'temurin' + type: string + build-tool: + description: 'Build tool to use (gradle or maven)' + default: 'gradle' + type: string + gradle-version: + description: 'Gradle version to use (only applicable if build-tool is gradle)' + default: 'wrapper' + type: string + gradle-build-task: + description: 'Gradle build task to run' + default: 'build' + type: string + gradle-build-file: + description: 'Gradle build file to use' + default: '' + type: string + maven-version: + description: 'Maven version to use (only applicable if build-tool is maven)' + default: '3.9.5' + type: string + maven-goals: + description: 'Maven goals to run' + default: 'clean package' + type: string + maven-args: + description: 'Additional Maven arguments' + default: '' + type: string + working-directory: + description: 'Working directory where the build commands will be run' + default: '.' + type: string + upload-artifacts: + description: 'Whether to upload build artifacts' + default: true + type: boolean + artifacts-name: + description: 'Name of the artifacts to upload' + default: 'build-artifacts' + type: string + artifacts-path: + description: 'Path to the artifacts to upload (relative to working-directory)' + default: '' + type: string + +jobs: + build: + runs-on: ubuntu-latest + steps: + - name: Checkout Repository + uses: actions/checkout@v4 + + - name: Set up JDK + uses: actions/setup-java@v4 + with: + java-version: ${{ inputs.java-version }} + distribution: ${{ inputs.java-distribution }} + architecture: x64 + + # Gradle build + - name: Setup Gradle + if: ${{ inputs.build-tool == 'gradle' }} + uses: gradle/actions/setup-gradle@af1da67850ed9a4cedd57bfd976089dd991e2582 # v4.0.0 + with: + gradle-version: ${{ inputs.gradle-version }} + + - name: Build with Gradle + if: ${{ inputs.build-tool == 'gradle' }} + working-directory: ${{ inputs.working-directory }} + run: | + if [ -n "${{ inputs.gradle-build-file }}" ]; then + ./gradlew -b ${{ inputs.gradle-build-file }} ${{ inputs.gradle-build-task }} + else + ./gradlew ${{ inputs.gradle-build-task }} + fi + + # Maven build + - name: Setup Maven + if: ${{ inputs.build-tool == 'maven' }} + uses: stCarolas/setup-maven@v5 + with: + maven-version: ${{ inputs.maven-version }} + + - name: Build with Maven + if: ${{ inputs.build-tool == 'maven' }} + working-directory: ${{ inputs.working-directory }} + run: mvn ${{ inputs.maven-goals }} ${{ inputs.maven-args }} + + # Determine artifacts path based on build tool if not explicitly provided + - name: Set default artifacts path + if: ${{ inputs.upload-artifacts && inputs.artifacts-path == '' }} + id: set-artifacts-path + run: | + if [ "${{ inputs.build-tool }}" = "gradle" ]; then + echo "path=build/libs" >> $GITHUB_OUTPUT + else + echo "path=target" >> $GITHUB_OUTPUT + fi + + # Upload artifacts + - name: Upload build artifacts + if: ${{ inputs.upload-artifacts }} + uses: actions/upload-artifact@v4 + with: + name: ${{ inputs.artifacts-name }} + path: ${{ inputs.working-directory }}/${{ inputs.artifacts-path != '' && inputs.artifacts-path || steps.set-artifacts-path.outputs.path }} \ No newline at end of file diff --git a/java-build/README.md b/java-build/README.md new file mode 100644 index 0000000..9a54c07 --- /dev/null +++ b/java-build/README.md @@ -0,0 +1,156 @@ +# Java Build and Test - Reusable Workflow Documentation 🚀 + +## Overview 🌟 + +This reusable GitHub Actions workflow automates the process of building and testing Java applications. It supports both Gradle and Maven build tools and is highly configurable via inputs. The workflow performs the following actions: + +- **Checks Out Your Repository**: Retrieves your code. 📥 +- **Sets Up Java JDK**: Installs the specified Java version and distribution. ☕ +- **Sets Up Gradle/Maven**: Configures the specified build tool with the desired version. 🔧 +- **Builds Your Application**: Compiles and packages your Java application. 🔨 +- **Uploads Build Artifacts**: Optionally uploads the build artifacts for later use. 📦 + +## Workflow Inputs 🛠️ + +| **Input** | **Description** | **Required** | **Default** | +|-----------------------|--------------------------------------------------------------------|--------------|-------------------| +| **java-version** | Java version to use. | No | `17` | +| **java-distribution** | Java distribution to use (temurin, zulu, adopt, etc.). | No | `temurin` | +| **build-tool** | Build tool to use (gradle or maven). | No | `gradle` | +| **gradle-version** | Gradle version to use (only applicable if build-tool is gradle). | No | `wrapper` | +| **gradle-build-task** | Gradle build task to run. | No | `build` | +| **gradle-build-file** | Gradle build file to use. | No | `''` (empty string) | +| **maven-version** | Maven version to use (only applicable if build-tool is maven). | No | `3.9.5` | +| **maven-goals** | Maven goals to run. | No | `clean package` | +| **maven-args** | Additional Maven arguments. | No | `''` (empty string) | +| **working-directory** | Working directory where the build commands will be run. | No | `.` | +| **upload-artifacts** | Whether to upload build artifacts. | No | `true` | +| **artifacts-name** | Name of the artifacts to upload. | No | `build-artifacts` | +| **artifacts-path** | Path to the artifacts to upload (relative to working-directory). | No | `''` (auto-determined) | + +## Job and Steps ⚙️ + +### Job Name: `build` + +- **Runs On**: `ubuntu-latest`. +- **Steps**: + - **Checkout Repository**: Uses `actions/checkout@v4` to fetch your code. 📥 + - **Set up JDK**: Configures Java with `actions/setup-java@v4`. ☕ + - **Setup Gradle**: If using Gradle, sets up the specified Gradle version. 🔧 + - **Build with Gradle**: If using Gradle, runs the specified build task. 🔨 + - **Setup Maven**: If using Maven, sets up the specified Maven version. 🔧 + - **Build with Maven**: If using Maven, runs the specified goals with arguments. 🔨 + - **Set default artifacts path**: Determines the default artifacts path based on the build tool. 📁 + - **Upload build artifacts**: If enabled, uploads the build artifacts. 📦 + +## How to Use This Reusable Workflow 🔄 + +1. **Save the Workflow File** + This workflow is already saved as `.github/workflows/java-build.yml` in the repository. 💾 + +2. **Call the Reusable Workflow** + In another workflow file, invoke this reusable workflow like so: + + ```yaml + name: Build My Java Application + on: + push: + branches: [main] + + jobs: + build: + uses: iExecBlockchainComputing/github-actions-workflows/.github/workflows/java-build.yml@java-build-v1.0.0 + with: + java-version: '17' + build-tool: 'gradle' + gradle-build-task: 'build' + ``` + +## Workflow Steps in Detail 🔍 + +1. **Checkout Repository**: + - Uses `actions/checkout@v4` to fetch your code. + +2. **Set up JDK**: + - Uses `actions/setup-java@v4` to install and configure the specified Java version and distribution. + - Sets the architecture to x64. + +3. **Gradle Build** (if build-tool is 'gradle'): + - Sets up Gradle using `gradle/actions/setup-gradle@v4.0.0`. + - Runs the specified Gradle build task, using a custom build file if provided. + +4. **Maven Build** (if build-tool is 'maven'): + - Sets up Maven using `stCarolas/setup-maven@v5`. + - Runs the specified Maven goals with any additional arguments. + +5. **Artifact Handling**: + - If no explicit artifacts path is provided, determines the default path based on the build tool: + - For Gradle: `build/libs` + - For Maven: `target` + - If artifact uploading is enabled, uploads the build artifacts using `actions/upload-artifact@v4`. + +## Example Usage Scenarios 📋 + +### Basic usage with Gradle (default) + +```yaml +jobs: + build: + uses: iExecBlockchainComputing/github-actions-workflows/java-build@java-build-v1.0.0 +``` + +### Using with Maven + +```yaml +jobs: + build: + uses: iExecBlockchainComputing/github-actions-workflows/java-build@java-build-v1.0.0 + with: + build-tool: 'maven' +``` + +### Custom Java version and distribution + +```yaml +jobs: + build: + uses: iExecBlockchainComputing/github-actions-workflows/java-build@java-build-v1.0.0 + with: + java-version: '11' + java-distribution: 'zulu' +``` + +### Custom Gradle configuration + +```yaml +jobs: + build: + uses: iExecBlockchainComputing/github-actions-workflows/java-build@java-build-v1.0.0 + with: + gradle-build-task: 'test' + gradle-build-file: 'custom.gradle' +``` + +### Custom Maven configuration + +```yaml +jobs: + build: + uses: iExecBlockchainComputing/github-actions-workflows/java-build@java-build-v1.0.0 + with: + build-tool: 'maven' + maven-goals: 'clean test' + maven-args: '-DskipTests=false' +``` + +### Custom artifacts configuration + +```yaml +jobs: + build: + uses: iExecBlockchainComputing/github-actions-workflows/java-build@java-build-v1.0.0 + with: + upload-artifacts: true + artifacts-name: 'my-java-app' + artifacts-path: 'custom/path/to/artifacts' +```