Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add windows docker image #292

Merged
merged 4 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
123 changes: 120 additions & 3 deletions .github/workflows/Release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -178,13 +178,11 @@ jobs:
- name: Setup Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Login to DockerHub
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- name: Login to GitHub Container Registry
if: github.event_name != 'pull_request'
uses: docker/login-action@v3
with:
registry: ghcr.io
Expand All @@ -201,13 +199,132 @@ jobs:
cache-from: type=gha
cache-to: type=gha,mode=max
- name: Docker Hub Description
if: github.event_name != 'pull_request'
uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
repository: ${{ secrets.DOCKER_USERNAME }}/cargo-zigbuild

docker-win:
name: Build Docker Image (Windows)
if: github.event_name != 'pull_request'
runs-on: windows-latest
environment: Docker Hub
defaults:
run:
shell: pwsh
steps:
- uses: actions/checkout@v4
- id: meta
uses: docker/metadata-action@v5
with:
images: |
${{ secrets.DOCKER_USERNAME }}/cargo-zigbuild.windows
ghcr.io/${{ github.repository_owner }}/cargo-zigbuild.windows
tags: |
type=schedule
type=ref,event=branch
type=ref,event=pr
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
type=sha
- name: Install containerd
run: |
# source: # https://github.com/containerd/containerd/blob/main/docs/getting-started.md#installing-containerd-on-windows
$url = "https://api.github.com/repos/containerd/containerd/releases/latest"
$Version = (Invoke-RestMethod -Uri $url -UseBasicParsing).tag_name.TrimStart('v')
$Arch = "amd64" # arm64 also available
curl.exe -LO https://github.com/containerd/containerd/releases/download/v$Version/containerd-$Version-windows-$Arch.tar.gz
tar.exe xvf .\containerd-$Version-windows-amd64.tar.gz
Copy-Item -Path .\bin -Destination $Env:ProgramFiles\containerd -Recurse -Force
$Path = [Environment]::GetEnvironmentVariable("PATH", "Machine") + [IO.Path]::PathSeparator + "$Env:ProgramFiles\containerd"
[Environment]::SetEnvironmentVariable( "Path", $Path, "Machine")
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
containerd.exe config default | Out-File $Env:ProgramFiles\containerd\config.toml -Encoding ascii
containerd.exe --register-service
Start-Service containerd
- name: Setup windows-container-networking-cni
run: |
# source: https://github.com/moby/buildkit/blob/master/docs/windows.md
$networkName = 'nat'
$natInfo = Get-HnsNetwork -ErrorAction Ignore | Where-Object { $_.Name -eq $networkName }
$gateway = $natInfo.Subnets[0].GatewayAddress
$subnet = $natInfo.Subnets[0].AddressPrefix
$cniConfPath = "$env:ProgramFiles\containerd\cni\conf\0-containerd-nat.conf"
$cniBinDir = "$env:ProgramFiles\containerd\cni\bin"
$url = "https://api.github.com/repos/microsoft/windows-container-networking/releases/latest"
# fix $cniVersion to 0.3.0 as buildkit does not support windows-cni 0.3.1+ (at the time of writing this workflow)
$cniVersion = '0.3.0' # (Invoke-RestMethod -Uri $url -UseBasicParsing).tag_name.TrimStart('v')
mkdir $cniBinDir -Force
curl.exe -LO https://github.com/microsoft/windows-container-networking/releases/download/v$cniVersion/windows-container-networking-cni-amd64-v$cniVersion.zip
tar xvf windows-container-networking-cni-amd64-v$cniVersion.zip -C $cniBinDir
$natConfig = @"
{
"cniVersion": "$cniVersion",
"name": "$networkName",
"type": "nat",
"master": "Ethernet",
"ipam": {
"subnet": "$subnet",
"routes": [
{
"gateway": "$gateway"
}
]
},
"capabilities": {
"portMappings": true,
"dns": true
}
}
"@
Set-Content -Path $cniConfPath -Value $natConfig
- name: Setup Buildkit
run: |
# source: https://github.com/moby/buildkit/blob/master/docs/windows.md
$url = "https://api.github.com/repos/moby/buildkit/releases/latest"
$version = (Invoke-RestMethod -Uri $url -UseBasicParsing).tag_name
$arch = "amd64" # arm64 binary available too
curl.exe -LO https://github.com/moby/buildkit/releases/download/$version/buildkit-$version.windows-$arch.tar.gz
mv bin bin2
tar.exe xvf .\buildkit-$version.windows-$arch.tar.gz
Copy-Item -Path ".\bin" -Destination "$Env:ProgramFiles\buildkit" -Recurse -Force
$Path = [Environment]::GetEnvironmentVariable("PATH", "Machine") + [IO.Path]::PathSeparator + "$Env:ProgramFiles\buildkit"
[Environment]::SetEnvironmentVariable( "Path", $Path, "Machine")
$Env:Path = [System.Environment]::GetEnvironmentVariable("Path","Machine") + ";" + [System.Environment]::GetEnvironmentVariable("Path","User")
buildkitd --register-service --service-name buildkitd --containerd-cni-config-path="C:\Program Files\containerd\cni\conf\0-containerd-nat.conf" --containerd-cni-binary-dir="C:\Program Files\containerd\cni\bin"
Start-Service buildkitd
- uses: docker/setup-buildx-action@v3
with:
# https://github.com/microsoft/Windows-Containers/blob/Main/helpful_tools/GitHubActions/install_buildkit_workflow.yaml
driver: remote
endpoint: npipe:////./pipe/buildkitd
- uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
- uses: docker/login-action@v3
with:
registry: ghcr.io
username: ${{ github.repository_owner }}
password: ${{ secrets.GITHUB_TOKEN }}
- uses: docker/build-push-action@v6
with:
context: .
file: Dockerfile.windows
platforms: windows/amd64
push: ${{ github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/')) }}
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha
cache-to: type=gha,mode=max
- uses: peter-evans/dockerhub-description@v4
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_TOKEN }}
repository: ${{ secrets.DOCKER_USERNAME }}/cargo-zigbuild

release:
name: Release
runs-on: ubuntu-latest
Expand Down
23 changes: 23 additions & 0 deletions Dockerfile.windows
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
FROM mcr.microsoft.com/windows/servercore:ltsc2022
ARG RUST_VERSION=1.82.0
ARG ZIG_VERSION=0.10.1
USER ContainerAdministrator
ADD https://github.com/phracker/MacOSX-SDKs/releases/latest/download/MacOSX11.3.sdk.tar.xz /sdk.tar.xz
ADD https://ziglang.org/download/${ZIG_VERSION}/zig-windows-x86_64-${ZIG_VERSION}.zip /zig.zip
ADD https://static.rust-lang.org/rustup/dist/x86_64-pc-windows-gnu/rustup-init.exe /rustup-init.exe
ADD https://www.7-zip.org/a/7zr.exe /7zr.exe
ADD . /cargo-zigbuild
SHELL [ "c:\\windows\\system32\\cmd.exe", "/s", "/c" ]
ENV PATH=C:\\Users\\ContainerAdministrator\\.cargo\\bin;C:\\zig-windows-x86_64-${ZIG_VERSION};C:\Windows\\system32;C:\\Windows;C:\\Windows\\System32\\Wbem;C:\\Windows\\System32\\WindowsPowerShell\\v1.0\\;C:\\Windows\\System32\\OpenSSH\\;C:\\Users\\ContainerAdministrator\\AppData\\Local\\Microsoft\\WindowsApps;
ENV SDKROOT=C:\\MacOSX11.3.sdk
# Default host is set to x86_64-pc-windows-gnu to avoid MSVC which requires VS Build Tools
RUN rustup-init.exe -y --no-modify-path --default-toolchain %RUST_VERSION% --default-host x86_64-pc-windows-gnu --target \
aarch64-pc-windows-gnullvm \
x86_64-unknown-linux-gnu x86_64-unknown-linux-musl \
aarch64-unknown-linux-gnu aarch64-unknown-linux-musl \
arm-unknown-linux-gnueabihf arm-unknown-linux-musleabihf \
x86_64-apple-darwin aarch64-apple-darwin \
&& tar xf zig.zip && 7zr e sdk.tar.xz && tar xf sdk.tar \
&& cargo build --manifest-path cargo-zigbuild\Cargo.toml --release \
&& move cargo-zigbuild\target\release\cargo-zigbuild.exe %USERPROFILE%\.cargo\bin\cargo-zigbuild.exe \
&& rmdir /q /s cargo-zigbuild && del /q rustup-init.exe 7zr.exe zig.zip sdk.tar.xz sdk.tar
13 changes: 11 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ You can also install it using pip which will also install [`ziglang`](https://py
pip install cargo-zigbuild
```

We also provide a [Docker image](https://hub.docker.com/r/messense/cargo-zigbuild) which has macOS SDK pre-installed in addition to cargo-zigbuild and Rust,
for example to build for x86_64 macOS:
We also provide Docker images which has macOS SDK pre-installed in addition to cargo-zigbuild and Rust, for example to build for x86_64 macOS:

- Linux docker image ([ghcr.io](https://github.com/rust-cross/cargo-zigbuild/pkgs/container/cargo-zigbuild), [Docker Hub](https://hub.docker.com/r/messense/cargo-zigbuild)):
```bash
docker run --rm -it -v $(pwd):/io -w /io messense/cargo-zigbuild \
cargo zigbuild --release --target x86_64-apple-darwin
```

- Windows docker image ([ghcr.io](https://github.com/rust-cross/cargo-zigbuild/pkgs/container/cargo-zigbuild.windows), [Docker Hub](https://hub.docker.com/r/messense/cargo-zigbuild.windows)):
```powershell
docker run --rm -it -v ${pwd}:c:\io -w c:\io messense/cargo-zigbuild.windows `
cargo zigbuild --target x86_64-apple-darwin
```
> [!NOTE]
> Windows docker image can compile debug builds, but does NOT support `cargo build --release` for *-apple-darwin targets.
> You will get ```error: unable to run `strip`: program not found```. If you know a solution to this, please open an issue and/or PR.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This might be caused by rust-lang/rust#131206 which is fixed in nightly and expected to be released in 1.83.0.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@messense I tested with nightly; it still outputs the same error.

On a side note, I don't think the Windows image will be used much, except in Windows runners. Once Windows runners support WSL2, then we can use the existing Linux-based docker image, and Windows-based image will be pretty much useless. Also, I could have simply used Linux runners to run Linux-based docker image, but a project I'm contributing to, are little hesitant to make a significant change to their CI.


[![Packaging status](https://repology.org/badge/vertical-allrepos/cargo-zigbuild.svg?columns=4)](https://repology.org/project/cargo-zigbuild/versions)

## Usage
Expand Down
Loading