From 3509a29aef49a97e82b90067248d49484348fb8f Mon Sep 17 00:00:00 2001 From: Thanayut T Date: Tue, 4 Feb 2025 15:48:32 +0700 Subject: [PATCH] feat: auto build --- .github/workflows/build.yaml | 45 ++++++++++++++++++++++++----- .github/workflows/manual-build.yaml | 41 ++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 8 deletions(-) create mode 100644 .github/workflows/manual-build.yaml diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 52d3251..9ff2170 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -1,16 +1,45 @@ name: Build on: - workflow_dispatch: - inputs: - APP_DIR: - description: "Directory of the service to build" - required: true - type: string + push: + branches: + - main jobs: + detect-changes: + runs-on: ubuntu-latest + outputs: + matrix: ${{ steps.set-matrix.outputs.matrix }} + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Filter changed paths + id: filter + uses: dorny/paths-filter@v3 + with: + list-files: json + filters: | + account: "account/server/**" + auth: "auth/server/**" + + - name: Generate matrix dynamically + id: set-matrix + run: | + matrix=() + for service in $(jq -r 'keys[]' <<< '${{ toJson(steps.filter.outputs) }}'); do + if [[ "${{ steps.filter.outputs[service] }}" == "true" ]]; then + matrix+=("\"$service\"") + fi + done + echo "matrix={\"APP_DIR\":[$(IFS=,; echo "${matrix[*]}")]}" >> $GITHUB_ENV + build: + needs: detect-changes + if: ${{ needs.detect-changes.outputs.matrix != '{}' }} runs-on: ubuntu-latest + strategy: + matrix: ${{ fromJson(needs.detect-changes.outputs.matrix) }} permissions: contents: read packages: write @@ -34,8 +63,8 @@ jobs: context: . file: ./docker/Dockerfile push: true - tags: ghcr.io/noah-platform/noah/${{ github.event.inputs.APP_DIR }}:latest + tags: ghcr.io/noah-platform/noah/${{ matrix.APP_DIR }}:latest build-args: | - APP_DIR=${{ github.event.inputs.APP_DIR }} + APP_DIR=${{ matrix.APP_DIR }} cache-from: type=gha cache-to: type=gha,mode=max diff --git a/.github/workflows/manual-build.yaml b/.github/workflows/manual-build.yaml new file mode 100644 index 0000000..fef5d8f --- /dev/null +++ b/.github/workflows/manual-build.yaml @@ -0,0 +1,41 @@ +name: Manual Build + +on: + workflow_dispatch: + inputs: + APP_DIR: + description: "Directory of the service to build" + required: true + type: string + +jobs: + build: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + steps: + - name: Checkout + uses: actions/checkout@v4 + + - name: Setup Docker Buildx + uses: docker/setup-buildx-action@v3 + + - name: Login to the Container Registry + uses: docker/login-action@v3 + with: + registry: ghcr.io + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and Push Docker Image + uses: docker/build-push-action@v6 + with: + context: . + file: ./docker/Dockerfile + push: true + tags: ghcr.io/noah-platform/noah/${{ github.event.inputs.APP_DIR }}:latest + build-args: | + APP_DIR=${{ github.event.inputs.APP_DIR }} + cache-from: type=gha + cache-to: type=gha,mode=max