From 15a6d949532834e12425c6f11e216c4c5e8c9d58 Mon Sep 17 00:00:00 2001 From: Eric Wang Date: Sun, 17 Mar 2024 14:20:34 +0800 Subject: [PATCH] Refactor: Streamline the build-push and deploy-dev workflow (#2852) --- .../{build-api-image.yml => build-push.yml} | 44 +++++++------- .github/workflows/build-web-image.yml | 60 ------------------- .github/workflows/deploy-dev.yml | 24 ++++++++ Makefile | 43 +++++++++++++ 4 files changed, 91 insertions(+), 80 deletions(-) rename .github/workflows/{build-api-image.yml => build-push.yml} (56%) delete mode 100644 .github/workflows/build-web-image.yml create mode 100644 .github/workflows/deploy-dev.yml create mode 100644 Makefile diff --git a/.github/workflows/build-api-image.yml b/.github/workflows/build-push.yml similarity index 56% rename from .github/workflows/build-api-image.yml rename to .github/workflows/build-push.yml index 0eb9e95b61..048f4cd942 100644 --- a/.github/workflows/build-api-image.yml +++ b/.github/workflows/build-push.yml @@ -1,17 +1,32 @@ -name: Build and Push API Image +name: Build and Push API & Web on: push: branches: - - 'main' - - 'deploy/dev' + - "main" + - "deploy/dev" release: - types: [ published ] + types: [published] + +env: + DOCKERHUB_USER: ${{ secrets.DOCKERHUB_USER }} + DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }} + DIFY_WEB_IMAGE_NAME: ${{ vars.DIFY_WEB_IMAGE_NAME || 'langgenius/dify-web' }} + DIFY_API_IMAGE_NAME: ${{ vars.DIFY_API_IMAGE_NAME || 'langgenius/dify-api' }} jobs: build-and-push: runs-on: ubuntu-latest if: github.event.pull_request.draft == false + strategy: + matrix: + include: + - service_name: "web" + image_name_env: "DIFY_WEB_IMAGE_NAME" + context: "web" + - service_name: "api" + image_name_env: "DIFY_API_IMAGE_NAME" + context: "api" steps: - name: Set up QEMU uses: docker/setup-qemu-action@v3 @@ -22,14 +37,14 @@ jobs: - name: Login to Docker Hub uses: docker/login-action@v2 with: - username: ${{ secrets.DOCKERHUB_USER }} - password: ${{ secrets.DOCKERHUB_TOKEN }} + username: ${{ env.DOCKERHUB_USER }} + password: ${{ env.DOCKERHUB_TOKEN }} - name: Extract metadata (tags, labels) for Docker id: meta uses: docker/metadata-action@v5 with: - images: langgenius/dify-api + images: ${{ env[matrix.image_name_env] }} tags: | type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/') }} type=ref,event=branch @@ -39,22 +54,11 @@ jobs: - name: Build and push uses: docker/build-push-action@v5 with: - context: "{{defaultContext}}:api" + context: "{{defaultContext}}:${{ matrix.context }}" platforms: ${{ startsWith(github.ref, 'refs/tags/') && 'linux/amd64,linux/arm64' || 'linux/amd64' }} - build-args: | - COMMIT_SHA=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }} + build-args: COMMIT_SHA=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }} push: true tags: ${{ steps.meta.outputs.tags }} labels: ${{ steps.meta.outputs.labels }} cache-from: type=gha cache-to: type=gha,mode=max - - - name: Deploy to server - if: github.ref == 'refs/heads/deploy/dev' - uses: appleboy/ssh-action@v0.1.8 - with: - host: ${{ secrets.SSH_HOST }} - username: ${{ secrets.SSH_USER }} - key: ${{ secrets.SSH_PRIVATE_KEY }} - script: | - ${{ secrets.SSH_SCRIPT }} diff --git a/.github/workflows/build-web-image.yml b/.github/workflows/build-web-image.yml deleted file mode 100644 index b77167c2a3..0000000000 --- a/.github/workflows/build-web-image.yml +++ /dev/null @@ -1,60 +0,0 @@ -name: Build and Push WEB Image - -on: - push: - branches: - - 'main' - - 'deploy/dev' - release: - types: [ published ] - -jobs: - build-and-push: - runs-on: ubuntu-latest - if: github.event.pull_request.draft == false - steps: - - name: Set up QEMU - uses: docker/setup-qemu-action@v3 - - - name: Set up Docker Buildx - uses: docker/setup-buildx-action@v3 - - - name: Login to Docker Hub - uses: docker/login-action@v2 - with: - username: ${{ secrets.DOCKERHUB_USER }} - password: ${{ secrets.DOCKERHUB_TOKEN }} - - - name: Extract metadata (tags, labels) for Docker - id: meta - uses: docker/metadata-action@v5 - with: - images: langgenius/dify-web - tags: | - type=raw,value=latest,enable=${{ startsWith(github.ref, 'refs/tags/') }} - type=ref,event=branch - type=sha,enable=true,priority=100,prefix=,suffix=,format=long - type=raw,value=${{ github.ref_name }},enable=${{ startsWith(github.ref, 'refs/tags/') }} - - - name: Build and push - uses: docker/build-push-action@v5 - with: - context: "{{defaultContext}}:web" - platforms: ${{ startsWith(github.ref, 'refs/tags/') && 'linux/amd64,linux/arm64' || 'linux/amd64' }} - build-args: | - COMMIT_SHA=${{ fromJSON(steps.meta.outputs.json).labels['org.opencontainers.image.revision'] }} - push: true - tags: ${{ steps.meta.outputs.tags }} - labels: ${{ steps.meta.outputs.labels }} - cache-from: type=gha - cache-to: type=gha,mode=max - - - name: Deploy to server - if: github.ref == 'refs/heads/deploy/dev' - uses: appleboy/ssh-action@v0.1.8 - with: - host: ${{ secrets.SSH_HOST }} - username: ${{ secrets.SSH_USER }} - key: ${{ secrets.SSH_PRIVATE_KEY }} - script: | - ${{ secrets.SSH_SCRIPT }} diff --git a/.github/workflows/deploy-dev.yml b/.github/workflows/deploy-dev.yml new file mode 100644 index 0000000000..47ca03c2eb --- /dev/null +++ b/.github/workflows/deploy-dev.yml @@ -0,0 +1,24 @@ +name: Deploy Dev + +on: + workflow_run: + workflows: ["Build and Push API & Web"] + branches: + - "deploy/dev" + types: + - completed + +jobs: + deploy: + runs-on: ubuntu-latest + if: | + github.event.workflow_run.conclusion == 'success' + steps: + - name: Deploy to server + uses: appleboy/ssh-action@v0.1.8 + with: + host: ${{ secrets.SSH_HOST }} + username: ${{ secrets.SSH_USER }} + key: ${{ secrets.SSH_PRIVATE_KEY }} + script: | + ${{ vars.SSH_SCRIPT || secrets.SSH_SCRIPT }} diff --git a/Makefile b/Makefile new file mode 100644 index 0000000000..ff61a00313 --- /dev/null +++ b/Makefile @@ -0,0 +1,43 @@ +# Variables +DOCKER_REGISTRY=langgenius +WEB_IMAGE=$(DOCKER_REGISTRY)/dify-web +API_IMAGE=$(DOCKER_REGISTRY)/dify-api +VERSION=latest + +# Build Docker images +build-web: + @echo "Building web Docker image: $(WEB_IMAGE):$(VERSION)..." + docker build -t $(WEB_IMAGE):$(VERSION) ./web + @echo "Web Docker image built successfully: $(WEB_IMAGE):$(VERSION)" + +build-api: + @echo "Building API Docker image: $(API_IMAGE):$(VERSION)..." + docker build -t $(API_IMAGE):$(VERSION) ./api + @echo "API Docker image built successfully: $(API_IMAGE):$(VERSION)" + +# Push Docker images +push-web: + @echo "Pushing web Docker image: $(WEB_IMAGE):$(VERSION)..." + docker push $(WEB_IMAGE):$(VERSION) + @echo "Web Docker image pushed successfully: $(WEB_IMAGE):$(VERSION)" + +push-api: + @echo "Pushing API Docker image: $(API_IMAGE):$(VERSION)..." + docker push $(API_IMAGE):$(VERSION) + @echo "API Docker image pushed successfully: $(API_IMAGE):$(VERSION)" + +# Build all images +build-all: build-web build-api + +# Push all images +push-all: push-web push-api + +build-push-api: build-api push-api +build-push-web: build-web push-web + +# Build and push all images +build-push-all: build-all push-all + @echo "All Docker images have been built and pushed." + +# Phony targets +.PHONY: build-web build-api push-web push-api build-all push-all build-push-all