Теперь просто в этом же build-pr.yml добавим джобу build и выглядеть она будет так.
# .github/workflows/build-pr.yml - часть 2
#jobs:
#changed-services:
#....
build:
name: Build ${{ matrix.service }}
runs-on: ubuntu-latest
needs: [changed-services]
if: ${{ needs.changed-services.outputs.any_changed == 'true' }}
strategy:
fail-fast: false
matrix: ${{ fromJSON(needs.changed-services.outputs.matrix) }}
steps:
- name: Checkout
uses: actions/checkout@v4
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
- name: Log in to GitHub Container Registry
uses: docker/login-action@v3
with:
registry: ${{ env.REGISTRY }}
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
- name: Extract metadata
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/${{ matrix.service }}
tags: |
type=ref,event=pr
type=sha,prefix=pr-${{ github.event.pull_request.number }}-
type=raw,value=pr-${{ github.event.pull_request.number }}
- name: Build and push
uses: docker/build-push-action@v6
with:
context: .
file: ${{ matrix.service }}/Dockerfile
platforms: linux/amd64,linux/arm64
push: true
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
cache-from: type=gha,scope=${{ matrix.service }}
cache-to: type=gha,mode=max,scope=${{ matrix.service }}
- name: Add PR comment
uses: mshick/add-pr-comment@v2
with:
message: |
**${{ matrix.service }}** successfully built!
**Images:**
`
${{ steps.meta.outputs.tags }}
`
**Pull command:**
`bash
docker pull ${{ env.REGISTRY }}/${{ env.IMAGE_PREFIX }}/${{ matrix.service }}:pr-${{ github.event.pull_request.number }}
`
message-id: build-${{ matrix.service }}
Самое главное что нам не нужно заботиться о том, чтобы поменять CI и о чем то думать - достаточно положить в корень репозитория свой новый сервис и все автоматом заведется.
Как это будет выглядеть в github смотрите на скринах. В качестве домашнего задания можете форкнуть реп https://github.com/itcaat/url-shortener-demo (все примеры workflow вы найдете там же) и сделать так, чтобы собирались не все сервисы при изменении в pkg, а только те что реально зависят от измененного пакета.
Теперь вы просто мастер Йода в мире github actions, а остальные пусть дальше собирают всё подряд.
habr [2025.10.17]: https://habr.com/ru/articles/957636/
#github_actions


