Skip to content

Commit

Permalink
fix: Proper build target for local Docker Compose (#7834)
Browse files Browse the repository at this point in the history
  • Loading branch information
Wirone committed Feb 17, 2024
1 parent 4ab8fc4 commit 591fb20
Show file tree
Hide file tree
Showing 4 changed files with 108 additions and 59 deletions.
71 changes: 16 additions & 55 deletions .github/workflows/docker.yml
Original file line number Diff line number Diff line change
@@ -1,74 +1,35 @@
name: Create and publish a Docker images
name: Docker

on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}
branches:
- "master"
pull_request:
paths:
- "compose.yaml"
- "Dockerfile"
- "docker/**"
# Allow running on demand using Github UI
workflow_dispatch:
schedule:
- cron: "0 5 * * *"

jobs:
build-and-push-image:
name: PHP ${{ matrix.php-version }}, Fixer ${{ github.ref_name }}
if: github.repository == 'PHP-CS-Fixer/PHP-CS-Fixer'
docker-compose-build:
name: Compose build PHP ${{ matrix.php-version }}
runs-on: ubuntu-latest
strategy:
matrix:
include:
- php-version: '7.4'
alpine-version: '3.16'
- php-version: '8.0'
alpine-version: '3.16'
- php-version: '8.1'
alpine-version: '3.18'
- php-version: '8.2'
alpine-version: '3.18'
- php-version: '8.3'
alpine-version: '3.18'

permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Make image lowercase
run: |
echo IMAGE_NAME_LOWER=$(echo ${{ env.IMAGE_NAME }} | tr '[:upper:]' '[:lower:]') >> ${GITHUB_ENV}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
flavor: |
latest=false
suffix=-php${{ matrix.php-version }}
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
target: dist
build-args: |
PHP_VERSION=${{ matrix.php-version }}
ALPINE_VERSION=${{ matrix.alpine-version }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
- name: Docker Compose build
run: docker compose build --no-cache --pull php-${{ matrix.php-version }}
74 changes: 74 additions & 0 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
name: Release

on:
push:
tags:
- "v[0-9]+.[0-9]+.[0-9]+"

env:
REGISTRY: ghcr.io
IMAGE_NAME: ${{ github.repository }}

jobs:
docker-images:
name: Docker with PHP ${{ matrix.php-version }}, Fixer ${{ github.ref_name }}
if: github.repository == 'PHP-CS-Fixer/PHP-CS-Fixer'
runs-on: ubuntu-latest
strategy:
matrix:
include:
- php-version: '7.4'
alpine-version: '3.16'
- php-version: '8.0'
alpine-version: '3.16'
- php-version: '8.1'
alpine-version: '3.18'
- php-version: '8.2'
alpine-version: '3.18'
- php-version: '8.3'
alpine-version: '3.18'

permissions:
contents: read
packages: write

steps:
- name: Checkout repository
uses: actions/checkout@v3

- name: Log in to the Container registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- name: Make image lowercase
run: |
echo IMAGE_NAME_LOWER=$(echo ${{ env.IMAGE_NAME }} | tr '[:upper:]' '[:lower:]') >> ${GITHUB_ENV}
- name: Extract metadata (tags, labels) for Docker
id: meta
uses: docker/metadata-action@v5
with:
flavor: |
latest=false
suffix=-php${{ matrix.php-version }}
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME_LOWER }}
tags: |
type=semver,pattern={{version}}
type=semver,pattern={{major}}.{{minor}}
type=semver,pattern={{major}}
- name: Build and push Docker image
uses: docker/build-push-action@v5
with:
context: .
file: Dockerfile
target: dist
build-args: |
PHP_VERSION=${{ matrix.php-version }}
ALPINE_VERSION=${{ matrix.alpine-version }}
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
2 changes: 1 addition & 1 deletion compose.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ version: '3.8'
services:
php-7.4: &php
build:
target: php
target: dev
args:
ALPINE_VERSION: "3.16"
PHP_VERSION: "7.4"
Expand Down
20 changes: 17 additions & 3 deletions tests/AutoReview/CiConfigurationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,8 @@ public function testTestJobsRunOnEachPhp(): void

self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $ciVersions);
self::assertUpcomingPhpVersionIsCoveredByCiJob(end($supportedVersions), $ciVersions);
self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $this->getPhpVersionsUsedByGitHubDocker());
self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $this->getPhpVersionsUsedForBuildingOfficialImages());
self::assertSupportedPhpVersionsAreCoveredByCiJobs($supportedVersions, $this->getPhpVersionsUsedForBuildingLocalImages());
}

public function testDeploymentJobsRunOnLatestStablePhpThatIsSupportedByTool(): void
Expand Down Expand Up @@ -262,13 +263,26 @@ private function getPhpVersionsUsedByGitHub(): array
/**
* @return list<numeric-string>
*/
private function getPhpVersionsUsedByGitHubDocker(): array
private function getPhpVersionsUsedForBuildingOfficialImages(): array
{
$yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/release.yml'));

return array_map(
static fn ($item) => $item['php-version'],
$yaml['jobs']['docker-images']['strategy']['matrix']['include']
);
}

/**
* @return list<numeric-string>
*/
private function getPhpVersionsUsedForBuildingLocalImages(): array
{
$yaml = Yaml::parse(file_get_contents(__DIR__.'/../../.github/workflows/docker.yml'));

return array_map(
static fn ($item) => $item['php-version'],
$yaml['jobs']['build-and-push-image']['strategy']['matrix']['include']
$yaml['jobs']['docker-compose-build']['strategy']['matrix']['include']
);
}
}

0 comments on commit 591fb20

Please sign in to comment.