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 support for static binary in GHA #291

Merged
merged 2 commits into from
Feb 22, 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
38 changes: 29 additions & 9 deletions .github/actions/cache/action.yaml
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
name: 'Compute cache keys'
description: 'Export cache keys for the build process to env vars'

inputs:
os:
description: 'The OS to compute the cache key for'
required: true
default: 'linux'

runs:
using: "composite"
steps:
Expand All @@ -9,27 +15,41 @@ runs:
run: |
set -e

# Should be the same command as the one in .github/workflows/ci.yml
cache_dirname_main=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --php-extensions=mbstring,phar,posix,tokenizer,pcntl)
cache_key_main=$(basename $cache_dirname_main)
echo cache_dirname_main=$cache_dirname_main >> $GITHUB_ENV
echo cache_key_main=$cache_key_main >> $GITHUB_ENV
# Should be the same command as the one in tools/static/castor.php
cache_dirname_linux_amd64=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --os=linux --arch=x86_64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl)
cache_key_linux_amd64=$(basename $cache_dirname_linux_amd64)
echo cache_dirname_linux_amd64=$cache_dirname_linux_amd64 >> $GITHUB_ENV
echo cache_key_linux_amd64=$cache_key_linux_amd64 >> $GITHUB_ENV

# Should be the same command as the one in tools/static/castor.php
cache_dirname_darwin_amd64=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --os=macos --arch=x86_64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl)
cache_key_darwin_amd64=$(basename $cache_dirname_darwin_amd64)
echo cache_dirname_darwin_amd64=$cache_dirname_darwin_amd64 >> $GITHUB_ENV
echo cache_key_darwin_amd64=$cache_key_darwin_amd64 >> $GITHUB_ENV

# And should be the same command as the one in CompileCommandTest
cache_dirname_test=$(tests/bin/compile-get-cache-key phar-location-is-not-used-in-cache-key --os linux --php-extensions mbstring,phar,posix,tokenizer)
cache_key_test=$(basename $cache_dirname_test)
echo cache_dirname_test=$cache_dirname_test >> $GITHUB_ENV
echo cache_key_test=$cache_key_test >> $GITHUB_ENV

- name: Restore PHP static building artifacts cache for main job
- name: Restore PHP static building artifacts cache for linux-amd64
uses: actions/cache@v4
with:
path: ${{ env.cache_dirname_linux_amd64 }}
key: php-static-${{ env.cache_key_linux_amd64 }}
if: ${{ inputs.os == 'linux' }}

- name: Restore PHP static building artifacts cache for darwin-amd64
uses: actions/cache@v4
with:
path: ${{ env.cache_dirname_main }}
key: php-static-${{ env.cache_key_main }}
if: matrix.castor.method == 'static'
path: ${{ env.cache_dirname_darwin_amd64 }}
key: php-static-${{ env.cache_key_darwin_amd64 }}
if: ${{ inputs.os == 'darwin' }}

- name: Restore PHP static building artifacts cache for test job
uses: actions/cache@v4
with:
path: ${{ env.cache_dirname_test }}
key: php-static-${{ env.cache_key_test }}
if: ${{ inputs.os == 'linux' }}
32 changes: 30 additions & 2 deletions .github/actions/static/action.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,41 @@
name: 'Build static binaries'
description: 'Builds static binaries for all systems'

inputs:
os:
description: 'The operating system to build for'
required: true
default: 'linux'

runs:
using: "composite"
steps:
- name: Build Castor static binary for Linux
run: bin/castor castor:static:linux
shell: bash
if: ${{ inputs.os == 'linux' }}

- name: Ensure Linux binary is OK
run: ./castor.linux-amd64 --version
shell: bash
if: ${{ inputs.os == 'linux' }}

- name: Build Castor static binary for MacOS - amd64
run: bin/castor castor:static:darwin-amd64
shell: bash
if: ${{ inputs.os == 'darwin-amd64' }}

- name: Ensure MacOs - amd64 binary is OK
run: ./castor.darwin-amd64 --version
shell: bash
if: ${{ inputs.os == 'darwin-amd64' }}

- name: Build Castor static binary for MacOS - arm64
run: bin/castor castor:static:darwin-arm64
shell: bash
if: ${{ inputs.os == 'darwin-arm64' }}

- name: Ensure binary is OK
run: ./castor.linux.x86_64 --version
- name: Ensure MacOs - arm64 binary is OK
run: ./castor.darwin-arm64 --version
shell: bash
if: ${{ inputs.os == 'darwin-arm64' }}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build phars
name: Artifacts

on:
push:
Expand All @@ -10,8 +10,8 @@ permissions:
contents: write

jobs:
phar:
name: Create phar and upload them as an artifact
phars:
name: Create phars and upload
runs-on: ubuntu-latest

steps:
Expand Down Expand Up @@ -52,10 +52,72 @@ jobs:
path: tools/phar/build/castor.windows-amd64.phar
if-no-files-found: error

static-linux:
needs: phars
name: Create Linux static binary and upload
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: ./.github/actions/install

- uses: ./.github/actions/cache
with:
os: 'linux'

- name: retrieve phar artifacts
uses: actions/download-artifact@v4
with:
path: tools/phar/build
merge-multiple: true

- uses: ./.github/actions/static
with:
os: 'linux'

- name: Upload the Linux static binary
uses: actions/upload-artifact@v4
with:
name: 'castor.linux-amd64'
path: ./castor.linux-amd64
if-no-files-found: error

static-darwin-amd64:
needs: phars
name: Create MacOs amd64 static binary and upload
runs-on: macos-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- uses: ./.github/actions/install

- uses: ./.github/actions/cache
with:
os: 'darwin'

- name: retrieve phar artifacts
uses: actions/download-artifact@v4
with:
path: tools/phar/build
merge-multiple: true

- uses: ./.github/actions/static
with:
os: 'darwin-amd64'

- name: Upload the MacOs amd64 static binary
uses: actions/upload-artifact@v4
with:
name: 'castor.darwin-amd64'
path: ./castor.darwin-amd64
if-no-files-found: error

release:
name: Upload phars to the release
name: Upload artifacts to the release
if: github.event_name == 'release'
needs: [phar]
needs: [phars, static-linux, static-darwin-amd64]
runs-on: ubuntu-latest
steps:
- name: Checkout
Expand All @@ -69,8 +131,10 @@ jobs:

- name: Upload files
run: |
gh release upload ${{ github.ref_name }} ./build/castor.darwin-amd64
gh release upload ${{ github.ref_name }} ./build/castor.darwin-amd64.phar
gh release upload ${{ github.ref_name }} ./build/castor.darwin-arm64.phar
gh release upload ${{ github.ref_name }} ./build/castor.linux-amd64
gh release upload ${{ github.ref_name }} ./build/castor.linux-amd64.phar
gh release upload ${{ github.ref_name }} ./build/castor.windows-amd64.phar
env:
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ jobs:
if: matrix.castor.method == 'phar' || matrix.castor.method == 'static'

- name: Compile Custom Built PHP along Castor phar for Linux
# Should be the same command as the one in .github/actions/cache/action.yaml
# Should be the same command as the one in tools/static/castor.php
run: bin/castor compile tools/phar/build/castor.linux-amd64.phar --php-extensions=mbstring,phar,posix,tokenizer,pcntl --binary-path=${{ github.workspace }}/${{ matrix.castor.bin }}
if: matrix.castor.method == 'static'

Expand Down
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
* Allow to listen to the symfony console events
* Add a `compile` command that puts together a customizable PHP binary with a
repacked castor app into one executable file
* Distribute `castor.linux.amd64` automatically with the release
lyrixx marked this conversation as resolved.
Show resolved Hide resolved
* Set the process title according to the current application name and task name
* Compile watcher and phar for `ARM64` on macOS
* Deprecates `add_context()` function, use `AsContextGenerator` attribute
Expand Down
3 changes: 2 additions & 1 deletion bin/generate-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
'castor:qa:cs',
'castor:qa:phpstan',
'castor:repack',
'castor:static:build',
'castor:static:darwin-amd64',
'castor:static:darwin-arm64',
'castor:static:linux',
'castor:watcher:build',
'castor:watcher:darwin',
Expand Down
5 changes: 3 additions & 2 deletions tests/Examples/Generated/ListTest.php.output.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,9 @@ castor:phar:linux Build phar for
castor:phar:windows Build phar for Windows system
castor:qa:cs Fix CS
castor:qa:phpstan Run PHPStan
castor:static:build Build phar for all systems
castor:static:linux Build phar for Linux system
castor:static:darwin-amd64 Build static binary for MacOS (amd64) system
castor:static:darwin-arm64 Build static binary for MacOS (arm64) system
castor:static:linux Build static binary for Linux system
castor:watcher:build Build watcher for all systems
castor:watcher:darwin Build watcher for MacOS system
castor:watcher:linux Build watcher for Linux system
Expand Down
17 changes: 11 additions & 6 deletions tools/static/castor.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,22 @@

use Castor\Attribute\AsTask;

use function Castor\parallel;
use function Castor\run;

#[AsTask(description: 'Build phar for Linux system')]
#[AsTask(description: 'Build static binary for Linux system')]
function linux()
{
run('bin/castor compile ./tools/phar/build/castor.linux-amd64.phar --os=linux --arch=x86_64', timeout: 0);
run('bin/castor compile tools/phar/build/castor.linux-amd64.phar --os=linux --arch=x86_64 --binary-path=castor.linux-amd64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl', timeout: 0);
}

#[AsTask(description: 'Build phar for all systems')]
function build()
#[AsTask(description: 'Build static binary for MacOS (amd64) system')]
function darwinAmd64()
{
parallel(linux(...));
run('bin/castor compile tools/phar/build/castor.darwin-amd64.phar --os=macos --arch=x86_64 --binary-path=castor.darwin-amd64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl', timeout: 0);
}

#[AsTask(description: 'Build static binary for MacOS (arm64) system')]
function darwinArm64()
{
run('bin/castor compile tools/phar/build/castor.darwin-arm64.phar --os=macos --arch=aarch64 --binary-path=castor.darwin-arm64 --php-extensions=mbstring,phar,posix,tokenizer,pcntl', timeout: 0);
}