Skip to content

Build and Deploy

Build and Deploy #330

Workflow file for this run

name: Build and Deploy
on:
push:
branches:
- main
- alpha
- beta
- dev
- stg
workflow_dispatch:
inputs:
force:
description: 'Force exec'
type: boolean
required: true
jobs:
Detect:
runs-on: ubuntu-latest
outputs:
app-matrix: ${{ steps.detect.outputs.app-matrix }}
pkg-matrix: ${{ steps.detect.outputs.pkg-matrix }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Detect Change
id: detect
run: |
APP_CORE="Netcorext.Auth.Core"
# 應用程式清單
APPS=(
"Netcorext.Auth.API"
"Netcorext.Auth.Authentication"
"Netcorext.Auth.Authorization"
"Netcorext.Auth.Gateway"
)
# 套件清單
PKGS=(
"Netcorext.Auth.Abstractions"
"Netcorext.Auth.Extensions.AspNetCore"
"Netcorext.Auth.Protobufs"
)
# 對 push event,直接用 diff-tree 看當前 commit 的變更
if [ "${{ github.event_name }}" = "push" ]; then
echo "Using git diff-tree for push event"
CHANGE_FILES=$(git diff-tree --no-commit-id --name-only -r HEAD)
else
# workflow_dispatch 或其他 event 用 diff
echo "Using git diff for non-push event"
CHANGE_FILES=$(git diff --name-only HEAD^ HEAD)
fi
echo "Changed files:"
echo "$CHANGE_FILES"
# 建構應用程式的 matrix
APP_MATRIX=()
for app in "${APPS[@]}"; do
CHANGED=$(echo "$CHANGE_FILES" | grep -Em 1 "${app}|${APP_CORE}" || true)
if [ -n "$CHANGED" ] || [ "${{ inputs.force }}" = true ]; then
echo "Changes detected in ${app}"
APP_MATRIX+=("{\"app\":\"$app\"}")
else
echo "No changes in ${app}"
fi
done
# 建構套件的 matrix
PKG_MATRIX=()
for app in "${PKGS[@]}"; do
CHANGED=$(echo "$CHANGE_FILES" | grep -Em 1 "${app}" || true)
if [ -n "$CHANGED" ] || [ "${{ inputs.force }}" = true ]; then
echo "Changes detected in ${app}"
PKG_MATRIX+=("{\"app\":\"$app\"}")
else
echo "No changes in ${app}"
fi
done
# 輸出 matrix JSON
if [ ${#APP_MATRIX[@]} -eq 0 ]; then
echo "app-matrix=[]" >> "$GITHUB_OUTPUT"
else
APP_JSON=$(printf '%s\n' "${APP_MATRIX[@]}" | jq -sc '.')
echo "app-matrix=$APP_JSON" >> "$GITHUB_OUTPUT"
fi
if [ ${#PKG_MATRIX[@]} -eq 0 ]; then
echo "pkg-matrix=[]" >> "$GITHUB_OUTPUT"
else
PKG_JSON=$(printf '%s\n' "${PKG_MATRIX[@]}" | jq -sc '.')
echo "pkg-matrix=$PKG_JSON" >> "$GITHUB_OUTPUT"
fi
Build-App:
name: "Build ${{ matrix.app }}"
needs: Detect
if: needs.Detect.outputs.app-matrix != '[]'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.Detect.outputs.app-matrix) }}
outputs:
tag: ${{ steps.envs.outputs.tag }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- id: envs
uses: netcorext/dotnet-version-action@dev
with:
app: ${{ matrix.app }}
image-prefix: ${{ vars.IMAGE_PREFIX }}
- uses: netcorext/dotnet-dockerfile-action@dev
with:
expand-command: apt-get update && apt-get install -y curl
- name: Login to DockerHub
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}
- name: Build Image & Push
uses: netcorext/buildx-image-action@dev
with:
image: ${{ steps.envs.outputs.image-fullName }}
tag: ${{ steps.envs.outputs.tag }}
tag-hash: ${{ steps.envs.outputs.tag-hash }}
tag-latest: ${{ steps.envs.outputs.tag-latest }}
build-arg: |-
--build-arg APP="${{ steps.envs.outputs.app }}" \
--build-arg ADDITIONAL_ARGUMENTS="${{ steps.envs.outputs.build-options }}"
Build-Pkg:
name: "Build ${{ matrix.app }}"
needs: Detect
if: needs.Detect.outputs.pkg-matrix != '[]'
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.Detect.outputs.pkg-matrix) }}
outputs:
artifact: ${{ steps.build.outputs.artifact }}
steps:
- id: build
name: Build Package
uses: netcorext/dotnet-build-package-action@dev
with:
app: ${{ matrix.app }}
Deploy-Pkg:
name: "Deploy ${{ matrix.app }}"
needs:
- Detect
- Build-Pkg
runs-on: ubuntu-latest
strategy:
fail-fast: false
matrix:
include: ${{ fromJson(needs.Detect.outputs.pkg-matrix) }}
steps:
- uses: netcorext/push-nuget-package-action@dev
name: Push Package
with:
artifact: ${{ needs.Build-Pkg.outputs.artifact }}
nuget-api-url: ${{ vars.NUGET_API_URL }}
nuget-api-key: ${{ secrets.NUGET_API_KEY }}
Tag:
name: "Tag Version"
needs: Build-App
if: needs.Build-App.result == 'success' && needs.Build-App.outputs.tag != ''
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Create Tag
run: |
VERSION="${{ needs.Build-App.outputs.tag }}"
if [ -z "$VERSION" ]; then
echo "No version tag found, skipping"
exit 0
fi
git tag "$VERSION"
git push origin "$VERSION"