123 lines
3.4 KiB
YAML
123 lines
3.4 KiB
YAML
name: CI
|
|
|
|
on:
|
|
push:
|
|
branches:
|
|
- develop
|
|
paths:
|
|
- "bff/**"
|
|
- "services/**"
|
|
- "rpc/**"
|
|
- "pkg/**"
|
|
- ".gitea/workflows/ci.yml"
|
|
workflow_dispatch:
|
|
|
|
jobs:
|
|
changes:
|
|
runs-on: runner
|
|
outputs:
|
|
matrix: ${{ steps.filter.outputs.matrix }}
|
|
steps:
|
|
- name: Checkout
|
|
uses: https://git.ailuowan.com/deploy/checkout@v4
|
|
with:
|
|
fetch-depth: 0
|
|
|
|
- name: Detect changes
|
|
id: filter
|
|
run: |
|
|
SERVICES="bff product admin user ad chore express sale wecom"
|
|
|
|
dockerfile_for() {
|
|
case "$1" in
|
|
bff) echo "bff/Dockerfile" ;;
|
|
*) echo "services/$1/Dockerfile" ;;
|
|
esac
|
|
}
|
|
|
|
pattern_for() {
|
|
case "$1" in
|
|
bff) echo '^(bff|pkg|rpc)/|^\.gitea/workflows/ci\.yml$' ;;
|
|
*) echo "^(services/$1|rpc/$1|pkg)/|^\\.gitea/workflows/ci\\.yml$" ;;
|
|
esac
|
|
}
|
|
|
|
build_all=false
|
|
if [ "${{ github.event_name }}" = "workflow_dispatch" ]; then
|
|
build_all=true
|
|
else
|
|
before="${{ github.event.before }}"
|
|
if [ -z "$before" ] || [ "$before" = "0000000000000000000000000000000000000000" ]; then
|
|
changed=$(git ls-files)
|
|
else
|
|
changed=$(git diff --name-only "$before" "${{ github.sha }}")
|
|
fi
|
|
echo "changed files:"
|
|
echo "$changed"
|
|
fi
|
|
|
|
matrix="["
|
|
first=true
|
|
for svc in $SERVICES; do
|
|
build=false
|
|
if [ "$build_all" = true ]; then
|
|
build=true
|
|
elif echo "$changed" | grep -qE "$(pattern_for "$svc")"; then
|
|
build=true
|
|
fi
|
|
|
|
df=$(dockerfile_for "$svc")
|
|
if [ "$first" = true ]; then
|
|
first=false
|
|
else
|
|
matrix="$matrix,"
|
|
fi
|
|
# use yes/no to avoid true/false being coerced to boolean in matrix if
|
|
if [ "$build" = true ]; then
|
|
flag=yes
|
|
else
|
|
flag=no
|
|
fi
|
|
matrix="$matrix{\"name\":\"$svc\",\"dockerfile\":\"$df\",\"build\":\"$flag\"}"
|
|
echo "$svc=$flag"
|
|
done
|
|
matrix="$matrix]"
|
|
|
|
echo "matrix=$matrix" >> "$GITHUB_OUTPUT"
|
|
echo "matrix=$matrix"
|
|
|
|
docker:
|
|
needs: changes
|
|
runs-on: runner
|
|
name: ${{ matrix.name }}
|
|
strategy:
|
|
fail-fast: false
|
|
matrix:
|
|
include: ${{ fromJSON(needs.changes.outputs.matrix) }}
|
|
env:
|
|
NAME: lone/${{ matrix.name }}
|
|
steps:
|
|
- name: Skip
|
|
if: matrix.build != 'yes'
|
|
run: echo "skip ${{ matrix.name }} (no changes)"
|
|
|
|
- name: Checkout
|
|
if: matrix.build == 'yes'
|
|
uses: https://git.ailuowan.com/deploy/checkout@v4
|
|
|
|
- name: Login Registry
|
|
if: matrix.build == 'yes'
|
|
run: |
|
|
echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login ${{ vars.REGISTRY }} -u "${{ vars.REGISTRY_USERNAME }}" --password-stdin
|
|
|
|
- name: Docker build
|
|
if: matrix.build == 'yes'
|
|
run: |
|
|
docker buildx use default
|
|
docker buildx build --builder default --load -f "${{ matrix.dockerfile }}" -t "${{ vars.REGISTRY }}/$NAME:latest" .
|
|
|
|
- name: Docker push
|
|
if: matrix.build == 'yes'
|
|
run: |
|
|
docker push ${{ vars.REGISTRY }}/$NAME:latest
|