Skip to content

Commit 55583e3

Browse files
authored
Build for windows (#109)
* This commit adds support for building the context generator (ctx) executable for multiple platforms: - Linux (x86_64, aarch64) - Windows (x64)
1 parent dbd8976 commit 55583e3

File tree

4 files changed

+326
-85
lines changed

4 files changed

+326
-85
lines changed

.github/workflows/build-phar-release.yml

-5
Original file line numberDiff line numberDiff line change
@@ -51,15 +51,10 @@ jobs:
5151
- name: 🤔 Run help command
5252
run: ${{ env.BIN_PHAR }} --help
5353

54-
- name: Backward compatibility
55-
run: |
56-
cp ${{ env.BIN_PHAR }} .build/phar/context-generator.phar
57-
5854
- name: 📤 Upload release assets
5955
uses: softprops/[email protected]
6056
if: startsWith(github.ref, 'refs/tags/')
6157
with:
6258
token: "${{ secrets.RELEASE_TOKEN }}"
6359
files: |
6460
${{ env.BIN_PHAR }}
65-
.build/phar/context-generator.phar
+116-14
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,150 @@
1-
name: Build Context Generator Executable
1+
---
2+
3+
name: 📦 Build binary
24

35
on: # yamllint disable-line rule:truthy
46
release:
57
types:
68
- published
79

810
jobs:
9-
build:
11+
build-unix:
1012
runs-on: ubuntu-latest
11-
name: 📦 Build CTX Executable
13+
name: 📦 Build Unix Executables
14+
strategy:
15+
fail-fast: false
16+
matrix:
17+
platform:
18+
- os: linux
19+
arch: amd64
20+
- os: linux
21+
arch: arm64
1222

1323
steps:
1424
- name: Checkout code
1525
uses: actions/checkout@v3
1626

27+
- name: Set up QEMU
28+
uses: docker/setup-qemu-action@v2
29+
with:
30+
platforms: arm64,amd64
31+
1732
- name: Set up Docker Buildx
1833
uses: docker/setup-buildx-action@v2
1934

20-
- name: Set current version from ${{ github.ref_name }}
35+
- name: Extract version from tag
36+
id: get_version
2137
run: |
22-
echo "{\"version\": \"${{ github.ref_name }}\", \"type\":\"bin\"}" > version.json
38+
if [[ "$GITHUB_REF_NAME" == refs/pull/* ]]; then
39+
# For pull requests, use "dev" as version
40+
VERSION="dev"
41+
else
42+
# For releases, extract version from tag (remove 'v' prefix if present)
43+
VERSION=${GITHUB_REF_NAME#v}
44+
fi
45+
echo "VERSION=$VERSION" >> $GITHUB_ENV
46+
echo "{\"version\": \"$VERSION\", \"type\":\"bin\"}" > version.json
2347
24-
- name: Build Docker image
48+
- name: Build Docker image for ${{ matrix.platform.os }}-${{ matrix.platform.arch }}
2549
uses: docker/build-push-action@v4
2650
with:
2751
context: .
2852
push: false
2953
load: true
30-
tags: ctx-builder:latest
31-
cache-from: type=gha
32-
cache-to: type=gha,mode=max
54+
tags: ctx-builder-${{ matrix.platform.os }}-${{ matrix.platform.arch }}:latest
55+
platforms: linux/${{ matrix.platform.arch }}
56+
build-args: |
57+
TARGET_OS=${{ matrix.platform.os }}
58+
TARGET_ARCH=${{ matrix.platform.arch }}
59+
VERSION=${{ env.VERSION }}
60+
cache-from: type=gha,scope=${{ matrix.platform.os }}-${{ matrix.platform.arch }}
61+
cache-to: type=gha,mode=max,scope=${{ matrix.platform.os }}-${{ matrix.platform.arch }}
3362

34-
- name: Extract executable from Docker container
63+
- name: Extract executable
3564
run: |
3665
mkdir -p dist
37-
container_id=$(docker create ctx-builder:latest)
38-
docker cp $container_id:/app/.build/bin/ctx ./dist/ctx
66+
container_id=$(docker create ctx-builder-${{ matrix.platform.os }}-${{ matrix.platform.arch }}:latest)
67+
docker cp $container_id:/.output/ctx ./dist/ctx-${{ env.VERSION }}-${{ matrix.platform.os }}-${{ matrix.platform.arch }}
3968
docker rm $container_id
40-
chmod +x ./dist/ctx
4169
4270
- name: 📤 Upload release assets
4371
uses: softprops/[email protected]
4472
if: startsWith(github.ref, 'refs/tags/')
4573
with:
4674
token: "${{ secrets.RELEASE_TOKEN }}"
4775
files: |
48-
./dist/ctx
76+
./dist/ctx-${{ env.VERSION }}-${{ matrix.platform.os }}-${{ matrix.platform.arch }}
77+
78+
build-windows:
79+
runs-on: windows-latest
80+
name: 📦 Build Windows (x64)
81+
steps:
82+
- name: Checkout code
83+
uses: actions/checkout@v3
84+
85+
- name: Set up PHP
86+
uses: shivammathur/setup-php@v2
87+
with:
88+
php-version: '8.3'
89+
extensions: mbstring, xml, curl, sockets
90+
coverage: none
91+
92+
- name: Extract version from tag or set dev version
93+
id: get_version
94+
shell: bash
95+
run: |
96+
if [[ "$GITHUB_REF" == refs/pull/* ]]; then
97+
# For pull requests, use "dev" as version
98+
VERSION="dev"
99+
else
100+
# For releases, extract version from tag (remove 'v' prefix if present)
101+
VERSION=${GITHUB_REF_NAME#v}
102+
fi
103+
echo "VERSION=$VERSION" >> $GITHUB_ENV
104+
echo "{\"version\": \"$VERSION\", \"type\":\"bin\"}" > version.json
105+
106+
- name: Install Composer dependencies
107+
run: composer install --no-dev --prefer-dist --ignore-platform-reqs
108+
109+
- name: Create build directories
110+
run: New-Item -Path ".build\phar", ".build\bin" -ItemType Directory -Force
111+
112+
- name: Download box tool
113+
run: |
114+
Invoke-WebRequest -Uri "https://github.com/box-project/box/releases/download/4.6.6/box.phar" -OutFile ".build/bin/box.phar"
115+
116+
- name: Download SPC for Windows
117+
run: |
118+
Invoke-WebRequest -Uri "https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-windows-x64.exe" -OutFile ".build/bin/spc.exe"
119+
120+
- name: Download required PHP extensions
121+
run: .build/bin/spc.exe download micro --for-extensions=ctype,dom,filter,libxml,mbstring,phar,simplexml,sockets,tokenizer,xml,xmlwriter,curl --with-php=8.3 --prefer-pre-built
122+
123+
# todo: fix this
124+
# - name: Install UPX for compression
125+
# run: |
126+
# .build/bin/spc.exe install-pkg upx
127+
128+
- name: Verify environment is ready
129+
run: |
130+
.build/bin/spc.exe doctor --auto-fix
131+
132+
- name: Build the self-executable binary
133+
run: .build/bin/spc.exe build "ctype,dom,filter,libxml,mbstring,phar,simplexml,sockets,tokenizer,xml,xmlwriter,curl" --build-micro --with-upx-pack
134+
135+
- name: Build PHAR file
136+
run: |
137+
php .build/bin/box.phar compile -v
138+
139+
- name: Combine micro.sfx with the PHAR
140+
run: |
141+
New-Item -Path "dist" -ItemType Directory -Force
142+
.build\bin\spc.exe micro:combine .build\phar\ctx.phar --output=dist\ctx-${{ env.VERSION }}-windows-amd64.exe
143+
144+
- name: 📤 Upload release assets
145+
uses: softprops/[email protected]
146+
if: startsWith(github.ref, 'refs/tags/')
147+
with:
148+
token: "${{ secrets.RELEASE_TOKEN }}"
149+
files: |
150+
./dist/ctx-${{ env.VERSION }}-windows-amd64.exe

Dockerfile

+13-41
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,34 @@
11
ARG COMPOSER_VERSION="2.8.4"
22

3-
FROM composer:${COMPOSER_VERSION} AS composer
4-
FROM php:8.3-cli-alpine AS builder
3+
FROM ghcr.io/context-hub/docker-ctx-binary/bin-builder:latest AS builder
54

6-
ENV COMPOSER_ALLOW_SUPERUSER=1
5+
# Define build arguments for target platform
6+
ARG TARGET_OS="linux"
7+
ARG TARGET_ARCH="x86_64"
8+
ARG VERSION="latest"
79

8-
# Install required packages
9-
RUN apk add --no-cache \
10-
wget \
11-
git \
12-
unzip \
13-
upx \
14-
bash
15-
16-
# Set working directory
1710
WORKDIR /app
1811

1912
# Copy source code
2013
COPY . .
21-
COPY --from=composer /usr/bin/composer /usr/bin/composer
2214

2315
RUN composer install --no-dev --prefer-dist --ignore-platform-reqs
2416

2517
# Create build directories
2618
RUN mkdir -p .build/phar .build/bin
2719

28-
# Download box tool for PHAR creation
29-
RUN wget -O .build/bin/box "https://github.com/box-project/box/releases/download/4.6.6/box.phar"
30-
RUN chmod +x .build/bin/box
31-
32-
# Download static-php-cli tool
33-
RUN wget -O .build/bin/spc "https://dl.static-php.dev/static-php-cli/spc-bin/nightly/spc-linux-x86_64"
34-
RUN chmod +x .build/bin/spc
35-
36-
# Download required PHP extensions
37-
RUN .build/bin/spc download micro \
38-
--for-extensions=ctype,dom,filter,libxml,mbstring,phar,simplexml,sockets,tokenizer,xml,xmlwriter,curl \
39-
--with-php=8.3 \
40-
--prefer-pre-built
41-
42-
# Install UPX for compression
43-
RUN .build/bin/spc install-pkg upx
44-
45-
# Verify environment is ready
46-
RUN .build/bin/spc doctor --auto-fix
47-
48-
# Build the self-executable binary with required extensions
49-
RUN .build/bin/spc build ctype,dom,filter,libxml,mbstring,phar,simplexml,sockets,tokenizer,xml,xmlwriter,curl \
50-
--build-micro \
51-
--with-upx-pack
52-
5320
# Build PHAR file
54-
RUN .build/bin/box compile -v
21+
RUN /usr/local/bin/box compile -v
5522

23+
RUN mkdir -p ./buildroot/bin
24+
RUN cp /build-tools/build/bin/micro.sfx ./buildroot/bin
5625
# Combine micro.sfx with the PHAR to create the final binary
57-
RUN .build/bin/spc micro:combine .build/phar/ctx.phar --output=.build/bin/ctx
26+
RUN /build-tools/static-php-cli/bin/spc micro:combine .build/phar/ctx.phar --output=.build/bin/ctx
5827
RUN chmod +x .build/bin/ctx
5928

60-
RUN cp .build/bin/ctx /.output
29+
# Copy to output with appropriate naming including version
30+
RUN mkdir -p /.output
31+
RUN cp .build/bin/ctx /.output/ctx
6132

33+
# Set default entrypoint (without version in name)
6234
ENTRYPOINT ["/.output/ctx"]

0 commit comments

Comments
 (0)