Okay, here is a GitHub Actions workflow definition in YAML that meets all your requirements for building a Java JDK on Windows x64 using Cygwin, with the specified inputs, defaults, and artifact upload.
name: Build OpenJDK (Windows x64, Cygwin)
on:
push:
branches:
- main # Trigger on pushes to the main branch
workflow_dispatch:
inputs:
jdk_source:
description: 'Branch, tag, or commit SHA of the OpenJDK source to build'
required: false
default: 'master' # Common default, adjust if your repo uses 'main' or another default
type: string
disable_warnings_as_errors:
description: 'Add --disable-warnings-as-errors configure flag'
required: false
default: false
type: boolean
build_debug_level:
description: 'Build debug level'
required: false
default: 'release'
type: choice
options:
- release
- optimized
- fastdebug
- slowdebug
boot_jdk_provider:
description: 'Provider for the Boot JDK'
required: false
default: 'Adoptium' # Use 'adopt' for setup-java, but allow descriptive name in input
type: string
boot_jdk_version:
description: 'Version of the Boot JDK'
required: false
default: '21'
type: string
extra_configure_flags:
description: 'Optional extra configure flags'
required: false
default: ''
type: string
artifact_name:
description: 'Name for the uploaded artifact (default: repo-name-source-ref-windows-x64)'
required: false
default: '' # Compute default in job logic if empty
type: string
jobs:
build_jdk:
runs-on: windows-latest # Specify Windows runner
env:
# Define standard Cygwin install path
CYGWIN_ROOT: C:\cygwin64
# Source will be checked out into the default workspace
JDK_SOURCE_DIR: ${{ github.workspace }}
steps:
- name: Checkout JDK Source
uses: actions/checkout@v4
with:
ref: ${{ github.event.inputs.jdk_source }}
fetch-depth: 0 # Fetch history depth (0 for all, or a specific number)
- name: Setup MSBuild and Visual Studio
# Required for compiling the native parts of OpenJDK on Windows
uses: microsoft/setup-msbuild@v1
- name: Install Cygwin and dependencies
# Use the Cygwin setup program in quiet mode to install necessary packages
# Based on OpenJDK build requirements for Cygwin
run: |
$CygwinSetupPath = Join-Path $env:TEMP "setup-x86_64.exe"
Write-Host "Downloading Cygwin setup from https://cygwin.com/setup-x86_64.exe"
Invoke-WebRequest -Uri "https://cygwin.com/setup-x86_64.exe" -OutFile $CygwinSetupPath -TimeoutSec 300
# Essential packages for OpenJDK build on Windows using Cygwin
# Consult specific JDK version build instructions for precise list
$CygwinPackages = @(
"make", "gcc-core", "g++", "zip", "unzip", "file", "m4", "autoconf", "python3",
"rsync" # Often used during the build process
)
$PackageList = $CygwinPackages -join ","
Write-Host "Installing Cygwin packages: $PackageList"
# Use a reliable mirror, e.g., https://mirrors.kernel.org/cygwin/ or https://cygwin.mirror.constant.com/
# Add '--local-package-dir' if you want to cache packages for faster reruns, specify a path
& $CygwinSetupPath --quiet-mode --no-setup --root "${{ env.CYGWIN_ROOT }}" --site "https://cygwin.mirror.constant.com/" --packages "$PackageList" --verbose | Out-Default
# Add Cygwin bin to PATH so subsequent steps can use bash, cygpath, etc.
echo "${{ env.CYGWIN_ROOT }}\bin" | Out-File -Path $env:GITHUB_PATH -Encoding utf8 -Append
shell: pwsh # Use PowerShell for Windows-specific commands
- name: Setup Boot JDK (${{ github.event.inputs.boot_jdk_provider }} ${{ github.event.inputs.boot_jdk_version }})
uses: actions/setup-java@v4
with:
# setup-java mapping: Adoptium maps to 'adopt' or 'temurin' distribution
distribution: ${{ github.event.inputs.boot_jdk_provider == 'Adoptium' && 'temurin' || github.event.inputs.boot_jdk_provider }}
java-version: ${{ github.event.inputs.boot_jdk_version }}
# This sets JAVA_HOME in the Windows environment. We'll convert it for Cygwin bash.
- name: Compute Artifact Name
id: compute_artifact_name # Use ID to reference outputs if needed, but here we use env
run: |
$repo_name = "${{ github.event.repo.name }}"
# For push, github.event.inputs.jdk_source might be empty, use github.ref_name
# For workflow_dispatch, github.event.inputs.jdk_source is guaranteed
$source_ref = "${{ github.event.inputs.jdk_source }}"
if ([string]::IsNullOrEmpty($source_ref)) {
# Fallback for push trigger if input is not used
$source_ref = "${{ github.ref_name }}"
}
$source_ref = $source_ref -replace "[^a-zA-Z0-9._-]", "_" # Sanitize for filename safety
$input_artifact_name = "${{ github.event.inputs.artifact_name }}"
if ([string]::IsNullOrEmpty($input_artifact_name)) {
$default_name = "$repo_name-$source_ref-windows-x64"
Write-Host "Using default artifact name: $default_name"
echo "ARTIFACT_NAME=$default_name" | Out-File -FilePath $env:GITHUB_ENV -Append
} else {
Write-Host "Using provided artifact name: $input_artifact_name"
echo "ARTIFACT_NAME=$input_artifact_name" | Out-File -FilePath $env:GITHUB_ENV -Append
}
shell: pwsh
- name: Create and Run Build Script (using Cygwin Bash)
# This step prepares and executes the configure and make commands within Cygwin's bash environment
# It first creates a script file and then executes it via bash --login -c
run: |
$jdk_source_dir = "${{ env.JDK_SOURCE_DIR }}"
$cygwin_source_dir = "$(cygpath -u "$jdk_source_dir")" # Convert Windows path to Cygwin path
$cygwin_boot_jdk_home = "$(cygpath -u "$env:JAVA_HOME")" # Convert Windows JAVA_HOME to Cygwin path
$configure_flags = @(
"--prefix=$cygwin_source_dir/build/install" # Install prefix within the build dir
"--enable-debug-level=${{ github.event.inputs.build_debug_level }}"
"--with-boot-jdk=$cygwin_boot_jdk_home"
# OpenJDK often needs the Visual Studio path explicitly on Windows
# setup-msbuild should handle this, but sometimes --with-msvcdir or similar might be needed
# Check the OpenJDK build instructions for the specific version if configure fails here.
# Often it can auto-detect via the VS environment variables setup by microsoft/setup-msbuild.
)
if ("${{ github.event.inputs.disable_warnings_as_errors}" -eq "true") {
$configure_flags += "--disable-warnings-as-errors"
}
# Add extra flags from input
if (![string]::IsNullOrEmpty("${{ github.event.inputs.extra_configure_flags }}")) {
$configure_flags += "${{ github.event.inputs.extra_configure_flags }}"
}
$configure_flags_string = $configure_flags -join " "
$build_script_content = @"
#!/bin/bash
# Ensure we are in the source directory within Cygwin
cd "$cygwin_source_dir" || { echo "Error: Failed to change directory to $cygwin_source_dir"; exit 1; }
echo "Current directory: \$(pwd)"
echo "Windows JAVA_HOME: ${{ env.JAVA_HOME }}"
echo "Cygwin Boot JDK path: \$cygwin_boot_jdk_home"
# Check if autogen.sh exists (some repositories like jdk require it)
if [ -f "autogen.sh" ]; then
echo "Running autogen.sh..."
bash autogen.sh || { echo "Error: autogen.sh failed"; exit 1; }
fi
echo "Configuring JDK with flags: $configure_flags_string"
bash configure $configure_flags_string || { echo "Error: configure failed"; exit 1; }
echo "Building JDK (make images)..."
# Use '-j' flag for parallel build based on runner cores
# The number of cores can be obtained via ${{ runner.os_architecture }} or hardcoded reasonable value
# Let's start with 4 for reasonable performance on typical GitHub runners
make images -j 4 || { echo "Error: make images failed"; exit 1; }
# Find the build output directory (usually build/<config>/images/jdk)
# Export the Windows path of the jdk image directory for the packaging step
BUILD_IMAGE_DIR=\$(find build/ -mindepth 3 -maxdepth 3 -type d -name "jdk" | head -n 1)
if [ -z "\$BUILD_IMAGE_DIR" ]; then
echo "Error: Could not find the built JDK image directory (expected in build/*/images/jdk)."
exit 1
fi
echo "Found JDK image directory (Cygwin path): \$BUILD_IMAGE_DIR"
# Convert Cygwin path to Windows path and export to GITHUB_ENV
BUILD_OUTPUT_WIN_PATH=\$(cygpath -w "\$BUILD_IMAGE_DIR")
echo "BUILD_OUTPUT_WIN_PATH=\$BUILD_OUTPUT_WIN_PATH" >> \$GITHUB_ENV
echo "Exported BUILD_OUTPUT_WIN_PATH: \$BUILD_OUTPUT_WIN_PATH"
echo "Build script finished successfully."
"@
# Write the script to a file in the workspace root
$script_path = Join-Path $jdk_source_dir "run_build.sh"
Write-Host "Writing build script to: $script_path"
$build_script_content | Out-File -Path $script_path -Encoding utf8
# Execute the script using bash --login to ensure full Cygwin environment
Write-Host "Executing build script via bash --login..."
# Need to pass GITHUB_ENV path so the bash script can write to it
bash --login -c "cd $(cygpath -u "$jdk_source_dir") && GITHUB_ENV='$GITHUB_ENV' bash run_build.sh"
shell: pwsh # Use PowerShell to write the script and execute bash
- name: Package Build Artifact
# Use 7z to create a zip file of the built JDK image
# 7z is available on windows-latest runners
run: |
# The build script should have exported BUILD_OUTPUT_WIN_PATH
$artifact_dir = "${{ env.BUILD_OUTPUT_WIN_PATH }}"
if ([string]::IsNullOrEmpty($artifact_dir) -or !(Test-Path $artifact_dir -PathType Container)) {
Write-Error "Build output directory not found or not exported correctly."
exit 1
}
Write-Host "Packaging directory: $artifact_dir"
$zip_file_name = "${{ env.ARTIFACT_NAME }}.zip"
$zip_output_path = Join-Path "${{ github.workspace }}" $zip_file_name
Write-Host "Output zip file: $zip_output_path"
# Create the zip archive
# Use 'a' (add) command, '$artifact_dir\*' adds the contents of the directory
# 'a "$zip_output_path" "$artifact_dir\*" '
7z a "$zip_output_path" "$artifact_dir\*" | Out-Default # Pipe output to see progress/details
if (!(Test-Path $zip_output_path -PathType Leaf)) {
Write-Error "Failed to create zip file: $zip_output_path"
exit 1
}
Write-Host "Artifact created successfully."
shell: pwsh
- name: Upload Build Artifact
uses: actions/upload-artifact@v4
with:
name: ${{ env.ARTIFACT_NAME }}
path: |
${{ github.workspace }}/${{ env.ARTIFACT_NAME }}.zip
# Optional: Configure artifact retention days
# retention-days: 7
Explanation:
name: Sets the name of the workflow.
on: Defines the triggers:
push on the main branch: Runs automatically when changes are pushed to main.
workflow_dispatch: Allows manual triggering from the "Actions" tab in GitHub, where you can provide inputs.
workflow_dispatch.inputs: Defines the custom inputs available when running manually, with descriptions, types, and default values as requested.
jdk_source: Branch, tag, or commit to checkout. Defaults to master.
disable_warnings_as_errors: Boolean checkbox. Defaults to false.
build_debug_level: Choice dropdown. Defaults to release.
boot_jdk_provider, boot_jdk_version: Strings for the boot JDK. Defaults to Adoptium, 21. (Note: setup-java uses specific distribution names like temurin, which is the project name under Adoptium. The workflow maps 'Adoptium' input to 'temurin' distribution).
extra_configure_flags: String for extra flags. Defaults to empty string.
artifact_name: String for the output artifact name. Defaults to empty string, which triggers the default name computation.
jobs.build_jdk: Defines the main job.
runs-on: windows-latest: Specifies the operating system for the runner.
env: Defines environment variables available to all steps in the job. CYGWIN_ROOT and JDK_SOURCE_DIR are set here.
steps: A sequence of tasks to execute.
- Checkout JDK Source: Uses
actions/checkout to get the specified JDK source code into the runner's workspace. ref uses the jdk_source input. fetch-depth: 0 ensures all history is fetched if needed by the build (e.g., for version stamping).
- Setup MSBuild and Visual Studio: Uses
microsoft/setup-msbuild to configure the environment for native compilation on Windows, which is necessary for OpenJDK.
- Install Cygwin and dependencies:
To use this workflow:
- Create a file named something like
.github/workflows/build-jdk-windows.yml in your repository.
- Copy and paste the YAML content into this file.
- Commit and push the file to your
main (or other push-triggered) branch.
- Go to the "Actions" tab in your GitHub repository.
- Find the workflow named "Build OpenJDK (Windows x64, Cygwin)".
- You should see a "Run workflow" button. Click it to trigger the workflow manually and use the defined inputs. Pushes to
main will also trigger it automatically.