From 32d06b2fc1323d39cc236a362ee233b6e1ad5f58 Mon Sep 17 00:00:00 2001 From: nguu0123 Date: Sun, 10 Aug 2025 12:20:22 +0300 Subject: [PATCH] Add publish image gha --- .github/workflows/build-and-publish-image.yml | 85 +++++++++++++++++++ 1 file changed, 85 insertions(+) create mode 100644 .github/workflows/build-and-publish-image.yml diff --git a/.github/workflows/build-and-publish-image.yml b/.github/workflows/build-and-publish-image.yml new file mode 100644 index 00000000..01698897 --- /dev/null +++ b/.github/workflows/build-and-publish-image.yml @@ -0,0 +1,85 @@ +name: Build and Publish Docker Image + +on: + push: + branches: + - main + tags: + - 'v*' + pull_request: + branches: + - main + workflow_dispatch: + inputs: + publish: + description: 'Publish to GHCR (only works on main branch)' + type: boolean + default: false + +env: + REGISTRY: ghcr.io + IMAGE_NAME: ${{ github.repository }} + +jobs: + build-and-push: + runs-on: ubuntu-latest + permissions: + contents: read + packages: write + + steps: + - name: Checkout repository + uses: actions/checkout@11bd71901bbe5b1630ceea73d27597364c9af683 # v4 + + - name: Set up Node.js + uses: actions/setup-node@49933ea5288caeca8642d1e84afbd3f7d6820020 # v4 + with: + node-version-file: '.nvmrc' + cache: 'npm' + + - name: Install dependencies + run: npm ci + + - name: Build packages + run: npm run build:packages + + - name: Prepare package artifacts + run: npm run prepare:package + + - name: Set up QEMU + uses: docker/setup-qemu-action@53851d14592bedcffcf25ea515a931808326393b # v3 + + - name: Set up Docker Buildx + uses: docker/setup-buildx-action@6524bf65af31da8452434dd49aa1939b02787313 # v3 + + - name: Extract metadata (tags, labels) for Docker + id: meta + uses: docker/metadata-action@8e5442c4ef9f78752691e2d8f8d19755c6f78e81 # v5 + with: + images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} + tags: | + type=ref,event=branch + type=ref,event=pr + type=semver,pattern={{version}} + type=semver,pattern={{major}}.{{minor}} + type=sha,prefix=sha-,format=short + + - name: Log in to the Container registry + if: github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')) + uses: docker/login-action@7ca345011cfb79dc351929d8d35c81db0542375e # v3 + with: + registry: ${{ env.REGISTRY }} + username: ${{ github.actor }} + password: ${{ secrets.GITHUB_TOKEN }} + + - name: Build and push Docker image + id: build-and-push + uses: docker/build-push-action@ca877d9245402d1537745e0e356eab47c3520991 # v6 + with: + context: . + platforms: linux/amd64,linux/arm64 + push: ${{ github.event_name != 'pull_request' && (github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v') || github.event.inputs.publish == 'true') }} + tags: ${{ steps.meta.outputs.tags }} + labels: ${{ steps.meta.outputs.labels }} + build-args: | + CLI_VERSION_ARG=${{ github.sha }} \ No newline at end of file