Skip to content
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
14 changes: 10 additions & 4 deletions .github/workflows/package-static.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ concurrency:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}-static
YIIPRESS_COMMIT: ${{ github.sha }}

jobs:
linux:
Expand Down Expand Up @@ -61,9 +62,11 @@ jobs:
cache-from: type=gha,scope=package-linux-amd64
cache-to: type=gha,scope=package-linux-amd64,mode=max
platforms: linux/amd64
build-args: |
YIIPRESS_COMMIT=${{ github.sha }}

- name: Smoke test Linux binary
run: ./dist/linux-amd64/yiipress --help
run: test "$(./dist/linux-amd64/yiipress --version)" = "YiiPress ${GITHUB_SHA}"

- name: Upload Linux artifacts
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
Expand Down Expand Up @@ -136,7 +139,10 @@ jobs:

- name: Smoke test Windows binary
shell: pwsh
run: ./dist/windows-amd64/yiipress.exe --help
run: |
if ((./dist/windows-amd64/yiipress.exe --version) -ne "YiiPress $env:GITHUB_SHA") {
throw "Packaged YiiPress version does not match $env:GITHUB_SHA."
}

- name: Upload Windows artifacts
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
Expand Down Expand Up @@ -188,10 +194,10 @@ jobs:
macos-package-${{ runner.os }}-${{ runner.arch }}-

- name: Build macOS artifacts
run: make package-macos PACKAGE_MACOS_ARCH=arm64 PACKAGE_MACOS_DIST=dist/macos-arm64
run: make package-macos PACKAGE_MACOS_ARCH=arm64 PACKAGE_MACOS_DIST=dist/macos-arm64 YIIPRESS_COMMIT=${GITHUB_SHA}

- name: Smoke test macOS binary
run: ./dist/macos-arm64/yiipress --help
run: ./dist/macos-arm64/yiipress --version | grep -Fqx "YiiPress ${GITHUB_SHA}"

- name: Pack macOS artifact
run: tar -C dist/macos-arm64 -czf dist/yiipress-macos-arm64.tar.gz yiipress
Expand Down
16 changes: 12 additions & 4 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ concurrency:
env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}-static
COMPOSER_ROOT_VERSION: ${{ github.ref_name }}
YIIPRESS_COMMIT: ${{ github.sha }}

jobs:
linux:
Expand Down Expand Up @@ -41,9 +43,12 @@ jobs:
cache-from: type=gha,scope=release-linux-amd64
cache-to: type=gha,scope=release-linux-amd64,mode=max
platforms: linux/amd64
build-args: |
COMPOSER_ROOT_VERSION=${{ github.ref_name }}
YIIPRESS_COMMIT=${{ github.sha }}

- name: Smoke test Linux binary
run: ./dist/linux-amd64/yiipress --help
run: test "$(./dist/linux-amd64/yiipress --version)" = "YiiPress ${GITHUB_REF_NAME}"

- name: Upload Linux artifact
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
Expand Down Expand Up @@ -105,7 +110,10 @@ jobs:

- name: Smoke test Windows binary
shell: pwsh
run: ./dist/windows-amd64/yiipress.exe --help
run: |
if ((./dist/windows-amd64/yiipress.exe --version) -ne "YiiPress $env:GITHUB_REF_NAME") {
throw "Packaged YiiPress version does not match $env:GITHUB_REF_NAME."
}

- name: Upload Windows artifact
uses: actions/upload-artifact@ea165f8d65b6e75b540449e92b4886f43607fa02
Expand Down Expand Up @@ -156,10 +164,10 @@ jobs:
release-macos-package-${{ runner.os }}-${{ runner.arch }}-

- name: Build macOS artifact
run: make package-macos PACKAGE_MACOS_ARCH=arm64 PACKAGE_MACOS_DIST=dist/macos-arm64
run: make package-macos PACKAGE_MACOS_ARCH=arm64 PACKAGE_MACOS_DIST=dist/macos-arm64 YIIPRESS_COMMIT=${GITHUB_SHA}

- name: Smoke test macOS binary
run: ./dist/macos-arm64/yiipress --help
run: ./dist/macos-arm64/yiipress --version | grep -Fqx "YiiPress ${GITHUB_REF_NAME}"

- name: Pack macOS artifact
run: tar -C dist/macos-arm64 -czf dist/yiipress-macos-arm64.tar.gz yiipress
Expand Down
8 changes: 5 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,10 @@ PACKAGE_MACOS_DIST ?= dist/macos-$(PACKAGE_MACOS_ARCH)
PACKAGE_WINDOWS_DIST ?= dist/windows-amd64
PACKAGE_PHAR_DIST ?= dist/phar
PACKAGE_IMAGE ?= ${IMAGE}-static
YIIPRESS_COMMIT ?= $(shell git rev-parse HEAD)
MACOS_PACKAGE_ARGS ?=
WINDOWS_PACKAGE_ARGS ?=
export YIIPRESS_COMMIT

.PHONY: build up open down stop clear shell yii composer rector cs-fix test test-coverage test-coverage-clover psalm composer-dependency-analyser bench-generate bench-generate-realistic bench bench-baseline bench-compare bench-profile profile-build php build-docs package package-phar package-linux package-macos package-windows package-distroless package-distroless-push prod-build prod-push prod-deploy help

Expand Down Expand Up @@ -207,19 +209,19 @@ ifeq ($(PRIMARY_GOAL),package)
package: ## Build Linux static binary into dist/linux-amd64/
@mkdir -p $(PACKAGE_LINUX_DIST)
@rm -f $(PACKAGE_LINUX_DIST)/yiipress.phar
docker buildx build --file docker/Dockerfile --target package-linux-artifacts --platform $(PACKAGE_PLATFORM) --output type=local,dest=$(PACKAGE_LINUX_DIST) .
docker buildx build --file docker/Dockerfile --target package-linux-artifacts --build-arg YIIPRESS_COMMIT=$(YIIPRESS_COMMIT) --platform $(PACKAGE_PLATFORM) --output type=local,dest=$(PACKAGE_LINUX_DIST) .
endif

ifeq ($(PRIMARY_GOAL),package-phar)
package-phar: ## Build YiiPress PHAR into dist/phar/
docker buildx build --file docker/Dockerfile --target package-phar-artifacts --platform $(PACKAGE_PLATFORM) --output type=local,dest=$(PACKAGE_PHAR_DIST) .
docker buildx build --file docker/Dockerfile --target package-phar-artifacts --build-arg YIIPRESS_COMMIT=$(YIIPRESS_COMMIT) --platform $(PACKAGE_PLATFORM) --output type=local,dest=$(PACKAGE_PHAR_DIST) .
endif

ifeq ($(PRIMARY_GOAL),package-linux)
package-linux: ## Build Linux static binary into dist/linux-amd64/
@mkdir -p $(PACKAGE_LINUX_DIST)
@rm -f $(PACKAGE_LINUX_DIST)/yiipress.phar
docker buildx build --file docker/Dockerfile --target package-linux-artifacts --platform $(PACKAGE_PLATFORM) --output type=local,dest=$(PACKAGE_LINUX_DIST) .
docker buildx build --file docker/Dockerfile --target package-linux-artifacts --build-arg YIIPRESS_COMMIT=$(YIIPRESS_COMMIT) --platform $(PACKAGE_PLATFORM) --output type=local,dest=$(PACKAGE_LINUX_DIST) .
endif

ifeq ($(PRIMARY_GOAL),package-macos)
Expand Down
26 changes: 26 additions & 0 deletions build/package-phar.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,12 @@

$target = $argv[1] ?? $root . '/dist/yiipress.phar';
$targetDirectory = dirname($target);
$commit = strtolower(getenv('YIIPRESS_COMMIT') ?: '');

if ($commit !== '' && preg_match('/^[0-9a-f]{40}$/', $commit) !== 1) {
fwrite(STDERR, "YIIPRESS_COMMIT must be a full 40-character commit SHA.\n");
exit(1);
}

if (!is_dir($targetDirectory) && !mkdir($targetDirectory, 0775, true) && !is_dir($targetDirectory)) {
fwrite(STDERR, "Failed to create target directory: {$targetDirectory}\n");
Expand Down Expand Up @@ -61,6 +67,26 @@
continue;
}

if ($localPath === 'src/ApplicationInfo.php' && $commit !== '') {
$contents = file_get_contents($fullPath);
if ($contents === false) {
fwrite(STDERR, "Failed to read file: {$localPath}\n");
exit(1);
}
$contents = str_replace(
"public const string COMMIT = '';",
"public const string COMMIT = '{$commit}';",
$contents,
$replacementCount,
);
if ($replacementCount !== 1) {
fwrite(STDERR, "Failed to embed the YiiPress commit in {$localPath}.\n");
exit(1);
}
$phar->addFromString($localPath, $contents);
continue;
}

if (PhpDocStripper::shouldStrip($localPath)) {
$contents = file_get_contents($fullPath);
if ($contents === false) {
Expand Down
6 changes: 4 additions & 2 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,8 @@ CMD ["./yii", "serve", "0.0.0.0", "--port=19777", "--workers=1"]
#

FROM app-base AS phar-builder
ARG COMPOSER_ROOT_VERSION=dev-master
ARG YIIPRESS_COMMIT
ENV APP_ENV=prod
COPY --from=composer /composer /usr/bin/composer
WORKDIR /app
Expand All @@ -129,8 +131,8 @@ COPY themes /app/themes
COPY build/package-phar.php build/PharArchiveFilter.php build/PhpDocStripper.php /app/build/
COPY yii composer.json composer.lock /app/
RUN --mount=type=cache,target=/tmp/composer-cache \
COMPOSER_CACHE_DIR=/tmp/composer-cache composer install --no-dev --no-progress --no-interaction --classmap-authoritative && \
php -d phar.readonly=0 build/package-phar.php dist/yiipress.phar
COMPOSER_CACHE_DIR=/tmp/composer-cache COMPOSER_ROOT_VERSION="${COMPOSER_ROOT_VERSION}" composer install --no-dev --no-progress --no-interaction --classmap-authoritative && \
YIIPRESS_COMMIT="${YIIPRESS_COMMIT}" php -d phar.readonly=0 build/package-phar.php dist/yiipress.phar

FROM app-base AS static-package
ARG PHP_VERSION=8.5
Expand Down
3 changes: 3 additions & 0 deletions docs/binaries-phar-docker.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@ irm https://raw.githubusercontent.com/yiipress/engine/master/install.ps1 | iex
```

The installers download the latest release archive and `SHA256SUMS`, verify the archive, and atomically install the executable.
The shell installer prints the exact resolved release version before downloading its archive.
The installed binary reports that same release tag with `yiipress --version`.
Development artifacts report the full commit SHA from which they were built instead of a placeholder version.
The shell installer uses `/usr/local/bin/yiipress`; the PowerShell installer uses the current user's local application directory
and adds it to the user `PATH`. Re-run the same command to update an existing installation. To use another directory or a fixed
release, set environment variables before invoking the installer. For example, with the shell installer:
Expand Down
9 changes: 8 additions & 1 deletion install.sh
Original file line number Diff line number Diff line change
Expand Up @@ -59,9 +59,16 @@ fi
work_dir="$(mktemp -d)"
trap 'rm -rf "$work_dir"' EXIT HUP INT TERM

if [ "$version" = "latest" ]; then
redirect_url="$(curl -fsS --retry 3 --retry-delay 2 -w '%{redirect_url}' "${release_url}/SHA256SUMS" -o /dev/null)"
redirect_url="${redirect_url%%\?*}"
release_url="${redirect_url%/SHA256SUMS}"
version="${release_url##*/}"
fi

Comment thread
coderabbitai[bot] marked this conversation as resolved.
echo "Downloading YiiPress ${version} for ${platform}/${architecture}..."
curl -fsSL --retry 3 --retry-delay 2 "${release_url}/${asset}" -o "${work_dir}/${asset}"
curl -fsSL --retry 3 --retry-delay 2 "${release_url}/SHA256SUMS" -o "${work_dir}/SHA256SUMS"
curl -fsSL --retry 3 --retry-delay 2 "${release_url}/${asset}" -o "${work_dir}/${asset}"

expected_checksum="$(awk -v asset="$asset" '$2 == asset || $2 == "*" asset || $2 == "assets/" asset { print $1; exit }' "${work_dir}/SHA256SUMS")"
if [ -z "$expected_checksum" ]; then
Expand Down
24 changes: 18 additions & 6 deletions src/ApplicationInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,32 @@
namespace YiiPress;

use Composer\InstalledVersions;

final class ApplicationInfo
{
public const string NAME = 'YiiPress';
public const string VERSION = '1.0.0';
public const string COMMIT = '';
public const string VERSION = 'unknown';

public static function version(): string
{
$version = InstalledVersions::getPrettyVersion('yiipress/engine');
$package = InstalledVersions::getRootPackage();
$version = $package['name'] === 'yiipress/engine' ? $package['pretty_version'] : null;
$reference = $package['name'] === 'yiipress/engine' ? $package['reference'] : null;

return self::resolveVersion($version, $reference);
}

if ($version === null || str_ends_with($version, '+no-version-set')) {
return self::VERSION;
private static function resolveVersion(?string $version, ?string $reference): string
{
if (
$version !== null
&& !str_ends_with($version, '+no-version-set')
&& !str_starts_with($version, 'dev-')
&& !str_ends_with($version, '-dev')
) {
return $version;
}

return $version;
return self::COMMIT !== '' ? self::COMMIT : ($reference ?? self::VERSION);
}
}
3 changes: 2 additions & 1 deletion tests/Console/ConsoleRunnerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace YiiPress\Tests\Console;

use PHPUnit\Framework\TestCase;
use YiiPress\ApplicationInfo;

use function escapeshellarg;
use function PHPUnit\Framework\assertSame;
Expand All @@ -20,7 +21,7 @@ public function testConsoleRunsWithApplicationNameAndVersion(): void
exec($yii . ' 2>&1', $output, $exitCode);

assertSame(0, $exitCode);
assertStringContainsString('YiiPress 1.0.0', implode("\n", $output));
assertStringContainsString('YiiPress ' . ApplicationInfo::version(), implode("\n", $output));
self::assertStringNotContainsString('Yii Console', implode("\n", $output));
assertStringNotContainsString('Runs an internal portable worker job', implode("\n", $output));
}
Expand Down
24 changes: 23 additions & 1 deletion tests/Unit/ApplicationInfoTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace YiiPress\Tests\Unit;

use ReflectionMethod;
use YiiPress\ApplicationInfo;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
Expand All @@ -14,7 +15,28 @@ final class ApplicationInfoTest extends TestCase
public function versionIsUserFacingReleaseVersion(): void
{
self::assertSame('YiiPress', ApplicationInfo::NAME);
self::assertMatchesRegularExpression('/^\d+\.\d+\.\d+/', ApplicationInfo::version());
self::assertMatchesRegularExpression('/^(?:\d+\.\d+\.\d+(?:[-+][0-9A-Za-z.-]+)?|[0-9a-f]{40}|unknown)$/', ApplicationInfo::version());
self::assertSame('', ApplicationInfo::COMMIT);
self::assertSame('unknown', ApplicationInfo::VERSION);
self::assertStringNotContainsString('no-version-set', ApplicationInfo::version());
}

#[Test]
public function developmentAliasFallsBackToExactReference(): void
{
$method = new ReflectionMethod(ApplicationInfo::class, 'resolveVersion');
$reference = 'aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa';

self::assertSame($reference, $method->invoke(null, '1.0.x-dev', $reference));
self::assertSame($reference, $method->invoke(null, 'dev-master', $reference));
self::assertSame('1.2.3', $method->invoke(null, '1.2.3', $reference));
}

#[Test]
public function missingComposerMetadataUsesSafeFallback(): void
{
$method = new ReflectionMethod(ApplicationInfo::class, 'resolveVersion');

self::assertSame(ApplicationInfo::VERSION, $method->invoke(null, null, null));
}
}
Loading
Loading