Make workflow for github action to build Java jdk for Windows x64. After completing the building, it should upload the artifact. I want to run workflow myself or when syncing with the main repo. Building should work with using cygwin.
It should have input for branch name or tag or commit SHA. Checkbox for adding --disable-warnings-as-errors as configure flag, choose of what build debug level i want (release, optimized, fast debug, slowdebug, etc.), input for boot jdk provider, input for boot jdk version, optional input for extra configure flags, and input for artifact name.
All should have a default:
input for branch and e.g. - master
Boot jdk version - 21
Boot jdk provider - Adoptium
build debug level - release
Optional configure flags should be empty
build artifact name should have default to (repo name)-(branch name)-windows-x64
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.
Okay, here is a GitHub Actions workflow that meets your requirements. It sets up Cygwin, checks out the specified source, uses a chosen Boot JDK, runs the configure and make steps within the Cygwin environment, and uploads the resulting JDK image as an artifact.
yaml
name: Build JDK (Windows x64 with Cygwin)
on:
push:
# Trigger on push to the main branch (or whatever your primary branch is)
branches:
- main # Or 'master', depending on your repository's default branch name
workflow_dispatch:
# Allows manual triggering with inputs
inputs:
ref:
description: 'Branch, Tag, or Commit SHA to build'
required: true
type: string
default: 'master' # Default branch name
disable_warnings:
description: 'Add --disable-warnings-as-errors configure flag?'
required: false
type: boolean
default: false
build_debug_level:
description: 'Build debug level'
required: true
type: choice
options:
- release
- optimized
- fastdebug
- slowdebug
default: 'release'
boot_jdk_provider:
description: 'Boot JDK provider to use (e.g., Adoptium, Zulu, Temurin, Oracle)'
required: true
type: string
default: 'Adoptium'
boot_jdk_version:
description: 'Boot JDK version to use (e.g., 17, 21, latest, lts)'
required: true
type: string
default: '21'
extra_configure_flags:
description: 'Additional configure flags (space separated)'
required: false
type: string
default: ''
artifact_name:
description: 'Base name for the build artifact file (will add .zip). The final artifact name in GitHub Actions will be derived automatically on push, or use this input on manual dispatch.'
required: true
type: string
# Note: Dynamic defaults like repo-branch are not directly supported in workflow_dispatch defaults.
# We handle dynamic naming in the job steps based on trigger type.
default: 'jdk-build-windows-x64'
permissions:
contents: read # Needed for checkout
actions: write # Needed for uploading artifacts
jobs:
build-jdk:
runs-on: windows-latest # Or windows-2019 / windows-2022 if needed
steps:
- name: Checkout source code
uses: actions/checkout@v4
with:
# Use the branch/tag/commit from workflow_dispatch input
# For push trigger, this will checkout the branch that triggered the push
ref: ${{ github.event_name == 'workflow_dispatch' && inputs.ref || github.ref }}
# Fetch depth 0 is often needed for full history if tags/commits are used
fetch-depth: 0
- name: Setup Boot JDK
uses: actions/setup-java@v4
with:
distribution: ${{ inputs.boot_jdk_provider }}
java-version: ${{ inputs.boot_jdk_version }}
architecture: x64 # Important for Windows build
check-latest: true # Get the latest available for the version/provider
- name: Setup Cygwin
# Install Cygwin and necessary packages using PowerShell
shell: pwsh
run: |
$cygwinInstaller = "setup-cygwin.exe"
$cygwinPath = "C:\cygwin64" # Default installation path
$cygwinDownloadUrl = "https://cygwin.com/setup-x86_64.exe"
# Essential packages for OpenJDK build on Cygwin
$packages = "autoconf,make,zip,unzip,file,tar,bash,gcc-core,gcc-g++,binutils,xpt"
$mirrorSite = "http://mirrors.kernel.org/sourceware/cygwin/" # Using a common mirror
Write-Host "Downloading Cygwin installer..."
Invoke-WebRequest -Uri $cygwinDownloadUrl -OutFile $cygwinInstaller
Write-Host "Installing Cygwin packages: $packages from $mirrorSite..."
.\$cygwinInstaller -q -O -s $mirrorSite -P $packages -R $cygwinPath
# Add Cygwin bin directory to the PATH for subsequent steps
Write-Host "Adding $cygwinPath\bin to PATH..."
echo "$cygwinPath\bin" | Out-File -Append $env:GITHUB_PATH -Encoding UTF8
- name: Set final artifact name
shell: bash # Use bash as it's available via Cygwin now
id: set_artifact_name # Give this step an ID to access its outputs
run: |
ARTIFACT_BASE_NAME="${{ inputs.artifact_name }}"
FINAL_NAME=""
if [ "${{ github.event_name }}" == "workflow_dispatch" ]; then
# Use the input provided by the user on manual trigger
FINAL_NAME="$ARTIFACT_BASE_NAME"
else # Handle push trigger to 'main' branch
# Construct a dynamic name based on repo and ref
REPO_SLUG=$(echo "${{ github.repository }}" | cut -d '/' -f 2)
# Sanitize ref name for use in file names
REF_NAME=$(echo "${{ github.ref_name }}" | sed 's/[^a-zA-Z0-9_-]/-/g')
FINAL_NAME="${REPO_SLUG}-${REF_NAME}-windows-x64"
fi
echo "Using artifact name: $FINAL_NAME"
echo "final_name=$FINAL_NAME" >> "$GITHUB_OUTPUT" # Set output variable
- name: Configure JDK build
# Run configure script within the Cygwin environment (using bash shell)
shell: bash
run: |
BOOT_JDK_HOME=$(cygpath -u "${{ env.JAVA_HOME }}") # Convert Windows path to Cygwin path
echo "Boot JDK Path (Cygwin): $BOOT_JDK_HOME"
# Base configure flags
CONFIGURE_FLAGS="--with-boot-jdk=${BOOT_JDK_HOME}"
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --with-debug-level=${{ inputs.build_debug_level }}"
# Add architecture/platform specifics if needed - configure is often smart enough
# CONFIGURE_FLAGS="$CONFIGURE_FLAGS --target=x86_64-w64-mingw32" # Example if cross-compiling (usually not needed for native Cygwin)
# Add conditional flag
if [ "${{ inputs.disable_warnings }}" == "true" ]; then
CONFIGURE_FLAGS="$CONFIGURE_FLAGS --disable-warnings-as-errors"
fi
# Add extra flags from input
if [ -n "${{ inputs.extra_configure_flags }}" ]; then
CONFIGURE_FLAGS="$CONFIGURE_FLAGS ${{ inputs.extra_configure_flags }}"
fi
echo "Running configure with flags: $CONFIGURE_FLAGS"
bash ./configure $CONFIGURE_FLAGS || { echo "Configure failed"; exit 1; }
- name: Build JDK (make images)
# Run make images target within the Cygwin environment (using bash shell)
shell: bash
run: |
echo "Running make images..."
# Use -j to speed up build by using multiple cores. $(nproc) gets number of processors.
make images -j$(nproc) || { echo "Make failed"; exit 1; }
echo "Make images completed."
- name: Find built JDK image directory
shell: bash
id: find_image
run: |
# Find the 'images' directory created by 'make images' under 'build'
BUILD_DIR=$(find build -maxdepth 2 -type d -name "images" | head -n 1)
if [ -z "$BUILD_DIR" ]; then
echo "Error: Could not find built JDK image directory."
exit 1
fi
echo "Found build directory: $BUILD_DIR"
# Set the found path as a step output variable
echo "image_path=$BUILD_DIR" >> "$GITHUB_OUTPUT"
- name: Prepare artifact (zip the JDK image)
shell: bash
id: zip_artifact
run: |
IMAGE_PATH="${{ steps.find_image.outputs.image_path }}"
if [ -z "$IMAGE_PATH" ]; then
echo "Error: Image path not found from previous step."
exit 1
fi
# The 'images' directory is typically inside a config-specific dir (e.g., build/windows-x86_64-server-release)
# We want to zip the parent directory of 'images' to keep the structure
BUILD_ROOT=$(dirname "$IMAGE_PATH") # e.g., build/windows-x86_64-server-release
# Get the name of the directory we want to zip (the config-specific name)
DIR_TO_ZIP=$(basename "$BUILD_ROOT") # e.g., windows-x86_64-server-release
# Determine the output zip file name using the calculated final artifact name
ARTIFACT_ZIP_NAME="${{ steps.set_artifact_name.outputs.final_name }}.zip"
echo "Zipping '$DIR_TO_ZIP' into '$ARTIFACT_ZIP_NAME'..."
# Change directory to the parent of $BUILD_ROOT to zip correctly
cd $(dirname "$BUILD_ROOT") # e.g., cd build
zip -r "../$ARTIFACT_ZIP_NAME" "$DIR_TO_ZIP" || { echo "Zipping failed"; exit 1; }
# Set the path to the created zip file for the upload step
echo "artifact_zip_path=$ARTIFACT_ZIP_NAME" >> "$GITHUB_OUTPUT"
- name: Upload build artifact
uses: actions/upload-artifact@v4
with:
# Use the final artifact name determined earlier
name: ${{ steps.set_artifact_name.outputs.final_name }}
# Use the path to the created zip file
path: ${{ steps.zip_artifact.outputs.artifact_zip_path }}
retention-days: 7 # Optional: How long to keep the artifact (adjust as needed)
Explanation:
name: Sets the name of the workflow displayed in the GitHub Actions tab.
on:
push: branches: [main]: Triggers the workflow automatically whenever code is pushed to the main branch. Change main if your default branch is named differently (e.g., master).
workflow_dispatch: Enables manual triggering of the workflow from the Actions tab in your repository.
inputs: Defines the parameters you can provide when triggering manually. Each input has a description, required status, type, and default value as requested.
ref: For the branch, tag, or commit SHA to build. Defaults to master.
disable_warnings: Boolean checkbox, defaults to false.
build_debug_level: Choice list with common OpenJDK debug levels, defaults to release.
boot_jdk_provider: String for the Boot JDK distribution, defaults to Adoptium.
boot_jdk_version: String for the Boot JDK version, defaults to 21.
extra_configure_flags: String for additional flags, defaults to empty.
artifact_name: String for the base artifact name. Defaults to jdk-build-windows-x64. The actual uploaded artifact name will be derived from this input on manual trigger, or dynamically generated on push.
permissions: Specifies the permissions needed by the GITHUB_TOKEN. contents: read is needed to clone the repository, and actions: write is typically needed by upload-artifact.
jobs: Defines the jobs to run.
build-jdk: The main job.
runs-on: windows-latest: Specifies that the job should run on the latest Windows runner provided by GitHub Actions.
steps: The sequence of commands and actions.
Checkout: Uses actions/checkout@v4 to get the source code. It checks out the ref specified in the input triggered manually, otherwise it checks out the branch that triggered the . is important to ensure all history (including tags if you build by tag) is available.
To use this workflow:
Save the code as a .yml file (e.g., build-jdk-windows.yml) inside the .github/workflows directory in your Java JDK source repository.
Push the file to your repository.
Go to the "Actions" tab in your GitHub repository.
You should see a workflow named "Build JDK (Windows x64 with Cygwin)".
If you trigger it manually via workflow_dispatch, you will see a "Run workflow" button and can provide inputs.
If you push to your main branch, it will trigger automatically using the default branch for the ref and a dynamically generated artifact name.
Remember to adjust the main branch name in the on: push: section if your default branch is named differently.
Setup Boot JDK: Uses actions/setup-java@v4 to download and set up the required Boot JDK based on your boot_jdk_provider and boot_jdk_version inputs. This action also conveniently sets the JAVA_HOME environment variable.
Setup Cygwin: This is a run step using pwsh (PowerShell) shell.
It downloads the setup-x86_64.exe installer.
It runs the installer in quiet mode (-q) to install essential packages (-P) needed for building OpenJDK within Cygwin (autoconf, make, gcc, etc.). It uses a reliable mirror site (-s).
It adds the Cygwin bin directory (C:\cygwin64\bin by default) to the GITHUB_PATH environment variable, making Cygwin commands like bash, make, zip, etc., available in subsequent steps even when using the bash shell.
Set final artifact name: This step determines the actual name that will be used for the uploaded artifact. It uses shell: bash.
If the workflow was triggered manually (github.event_name == 'workflow_dispatch'), it uses the name provided in the artifact_name input.
If the workflow was triggered by a push (github.event_name == 'push'), it constructs a dynamic name in the format (repo name)-(branch name)-windows-x64 by parsing github.repository and github.ref_name.
It sets this determined name as a step output variable (final_name) using echo "variable_name=value" >> "$GITHUB_OUTPUT".
Configure JDK build: This step runs the OpenJDK configure script. It uses shell: bash to execute within the Cygwin environment.
cygpath -u "${{ env.JAVA_HOME }}": Converts the Windows-style path provided by setup-java (C:\...) into a Unix-style path (/cygdrive/c/...) that the Cygwin configure script expects for --with-boot-jdk.
It constructs the CONFIGURE_FLAGS string based on the build_debug_level, the conditional disable_warnings flag, and any extra_configure_flags provided in the input.
It executes ./configure with the assembled flags.
Build JDK (make images): This step runs the make images command, also using shell: bash. The images target builds the complete JDK structure. -j$(nproc) tells make to use as many parallel jobs as there are processors, speeding up the build.
Find built JDK image directory: This step uses shell: bash and the find command to locate the images directory created by the build process, typically found under a path like build/windows-x86_64-server-release/images. It sets the found path as a step output variable (image_path).
Prepare artifact (zip the JDK image): This step uses shell: bash and the zip command (installed with Cygwin) to create a zip archive of the built JDK image directory.
It gets the image path from the previous step's output.
It determines the parent directory of the images directory (e.g., build/windows-x86_64-server-release).
It changes into this parent directory's parent (build) so that when it zips the config directory, the archive structure is correct (e.g., the zip contains windows-x86_64-server-release/...).
It names the zip file using the final artifact name determined earlier (${{ steps.set_artifact_name.outputs.final_name }}.zip).
It sets the path to the created zip file as a step output variable (artifact_zip_path).
Upload build artifact: Uses actions/upload-artifact@v4 to upload the created zip file.
name: Uses the final calculated artifact name from the set_artifact_name step.
path: Uses the path to the zip file created in the zip_artifact step.
retention-days: Optional setting to automatically delete the artifact after a certain number of days.