From 65a01d156f55ab2ad40391be49961039199bcafe Mon Sep 17 00:00:00 2001 From: umignon Date: Tue, 20 May 2025 09:06:09 +0200 Subject: [PATCH 1/3] feat(java-build): add reusable workflow --- .github/workflows/java-build.yml | 119 ++++++++++++++++++++++++++++++ java-build/README.md | 123 +++++++++++++++++++++++++++++++ 2 files changed, 242 insertions(+) create mode 100644 .github/workflows/java-build.yml create mode 100644 java-build/README.md 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..ac1cdbf --- /dev/null +++ b/java-build/README.md @@ -0,0 +1,123 @@ +# Java Build and Test + +A reusable GitHub Actions workflow for building and testing Java applications with Gradle or Maven. + +## Inputs + +### `java-version` + +**Optional** The Java version to use. Default: `'17'`. + +### `java-distribution` + +**Optional** The Java distribution to use (temurin, zulu, adopt, etc.). Default: `'temurin'`. + +### `build-tool` + +**Optional** The build tool to use (gradle or maven). Default: `'gradle'`. + +### `gradle-version` + +**Optional** The Gradle version to use (only applicable if build-tool is gradle). Default: `'wrapper'` (uses the Gradle Wrapper). + +### `gradle-build-task` + +**Optional** The Gradle build task to run. Default: `'build'`. + +### `gradle-build-file` + +**Optional** The Gradle build file to use. Default: `''` (uses the default build.gradle file). + +### `maven-version` + +**Optional** The Maven version to use (only applicable if build-tool is maven). Default: `'3.9.5'`. + +### `maven-goals` + +**Optional** The Maven goals to run. Default: `'clean package'`. + +### `maven-args` + +**Optional** Additional Maven arguments. Default: `''`. + +### `working-directory` + +**Optional** The working directory where the build commands will be run. Default: `'.'`. + +### `upload-artifacts` + +**Optional** Whether to upload build artifacts. Default: `true`. + +### `artifacts-name` + +**Optional** The name of the artifacts to upload. Default: `'build-artifacts'`. + +### `artifacts-path` + +**Optional** The path to the artifacts to upload (relative to working-directory). Default: `''` (automatically determined based on build tool). + +## Example usage + +### 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' +``` \ No newline at end of file From cca3299e95420de1d7cd73e8807462472fdbcedb Mon Sep 17 00:00:00 2001 From: Ugo Date: Mon, 16 Jun 2025 16:04:34 +0200 Subject: [PATCH 2/3] feat: enhance README with detailed reusable workflow documentation --- java-build/README.md | 151 ++++++++++++++++++++++++++----------------- 1 file changed, 92 insertions(+), 59 deletions(-) diff --git a/java-build/README.md b/java-build/README.md index ac1cdbf..1dc59d9 100644 --- a/java-build/README.md +++ b/java-build/README.md @@ -1,62 +1,95 @@ -# Java Build and Test - -A reusable GitHub Actions workflow for building and testing Java applications with Gradle or Maven. - -## Inputs - -### `java-version` - -**Optional** The Java version to use. Default: `'17'`. - -### `java-distribution` - -**Optional** The Java distribution to use (temurin, zulu, adopt, etc.). Default: `'temurin'`. - -### `build-tool` - -**Optional** The build tool to use (gradle or maven). Default: `'gradle'`. - -### `gradle-version` - -**Optional** The Gradle version to use (only applicable if build-tool is gradle). Default: `'wrapper'` (uses the Gradle Wrapper). - -### `gradle-build-task` - -**Optional** The Gradle build task to run. Default: `'build'`. - -### `gradle-build-file` - -**Optional** The Gradle build file to use. Default: `''` (uses the default build.gradle file). - -### `maven-version` - -**Optional** The Maven version to use (only applicable if build-tool is maven). Default: `'3.9.5'`. - -### `maven-goals` - -**Optional** The Maven goals to run. Default: `'clean package'`. - -### `maven-args` - -**Optional** Additional Maven arguments. Default: `''`. - -### `working-directory` - -**Optional** The working directory where the build commands will be run. Default: `'.'`. - -### `upload-artifacts` - -**Optional** Whether to upload build artifacts. Default: `true`. - -### `artifacts-name` - -**Optional** The name of the artifacts to upload. Default: `'build-artifacts'`. - -### `artifacts-path` - -**Optional** The path to the artifacts to upload (relative to working-directory). Default: `''` (automatically determined based on build tool). - -## Example usage +# 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) From d31782be432cbf722e524565d7f40fb6e5e071ef Mon Sep 17 00:00:00 2001 From: Ugo Date: Mon, 16 Jun 2025 16:05:25 +0200 Subject: [PATCH 3/3] docs: fix formatting in README for code block --- java-build/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java-build/README.md b/java-build/README.md index 1dc59d9..9a54c07 100644 --- a/java-build/README.md +++ b/java-build/README.md @@ -153,4 +153,4 @@ jobs: upload-artifacts: true artifacts-name: 'my-java-app' artifacts-path: 'custom/path/to/artifacts' -``` \ No newline at end of file +```