Determine comparison base #4
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: ci | |
on: | |
pull_request: | |
branches: | |
- main | |
types: | |
- opened | |
- synchronize | |
permissions: | |
contents: read | |
packages: write | |
jobs: | |
prepare: | |
runs-on: ubuntu-latest | |
outputs: | |
matrix: ${{ steps.prepare.outputs.matrix }} | |
length: ${{ steps.prepare.outputs.length }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
with: | |
fetch-depth: 0 | |
- name: Determine comparison base | |
id: determine-base | |
run: | | |
if [[ ${{ github.event.action }} == "opened" ]]; then | |
echo "::set-output name=base_ref::main" | |
else | |
echo "::set-output name=base_ref::${{ github.event.before }}" | |
fi | |
- name: Get changed files | |
id: changed-files | |
uses: tj-actions/changed-files@v45 | |
with: | |
base-ref: ${{ steps.determine-base.outputs.base_ref }} | |
sha: ${{ github.sha }} | |
files: | | |
**/Dockerfile | |
- name: Prepare | |
id: prepare | |
shell: python | |
run: | | |
import json | |
import os | |
from pathlib import Path | |
def discover() -> list[str]: | |
for changed in ${{ steps.changed-markdown-files.outputs.all_changed_files || '[]' }}: | |
path = Path(changed) | |
if Path(path.parts[0]).is_dir(): | |
yield path.parts[0] | |
def jsonify(item_lists: list[str]) -> str: | |
return json.dumps(item_lists, separators=(",", ":")) | |
def main(): | |
item_lists = list(set(discover())) | |
length = len(item_lists) | |
json_modules = jsonify({"service": item_lists}) | |
github_output = f"matrix={json_modules}\n" | |
with open(os.environ["GITHUB_OUTPUT"], "a") as f: | |
f.write(github_output) | |
f.write(f"length={length}\n") | |
if __name__ == "__main__": | |
main() | |
build: | |
needs: prepare | |
runs-on: ubuntu-latest | |
if: ${{ needs.prepare.outputs.length > 0 }} | |
strategy: | |
fail-fast: false | |
matrix: ${{ fromJson(needs.prepare.outputs.matrix) }} | |
steps: | |
- name: Checkout | |
uses: actions/checkout@v4 | |
- name: Set up QEMU | |
uses: docker/setup-qemu-action@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v3 | |
- name: Login to GitHub Container Registry | |
uses: docker/login-action@v3 | |
with: | |
password: ${{ secrets.GITHUB_TOKEN }} | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
- id: meta | |
name: Docker metadata | |
uses: docker/metadata-action@v5 | |
with: | |
images: ghcr.io/${{ github.repository }}/${{ matrix.service }} | |
- id: build-push | |
name: Build and push | |
uses: docker/build-push-action@v3 | |
with: | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
context: ${{ matrix.service }} | |
labels: ${{ steps.meta.outputs.labels }} | |
platforms: linux/amd64,linux/arm64 | |
push: true | |
tags: | | |
${{ steps.meta.outputs.tags }} |