From 1680a1786e7e81122c36743d580a4caafe19612e Mon Sep 17 00:00:00 2001 From: ItzCrazyKns <95534749+ItzCrazyKns@users.noreply.github.com> Date: Thu, 3 Oct 2024 10:41:05 +0530 Subject: [PATCH] feat(image-build): improve build time by caching --- .github/workflows/docker-build.yaml | 40 +++++++++++++++++++++++------ app.dockerfile | 2 +- backend.dockerfile | 2 +- 3 files changed, 34 insertions(+), 10 deletions(-) diff --git a/.github/workflows/docker-build.yaml b/.github/workflows/docker-build.yaml index 63210ffd..3cd9044f 100644 --- a/.github/workflows/docker-build.yaml +++ b/.github/workflows/docker-build.yaml @@ -1,4 +1,4 @@ -name: Build & Push Docker Image +name: Build & Push Docker Images on: push: @@ -10,7 +10,9 @@ on: jobs: build-and-push: runs-on: ubuntu-latest - + strategy: + matrix: + service: [backend, app] steps: - name: Checkout code uses: actions/checkout@v3 @@ -31,16 +33,38 @@ jobs: id: version run: echo "RELEASE_VERSION=${GITHUB_REF#refs/tags/}" >> $GITHUB_ENV - - name: Build and push Docker image + - name: Build and push Docker image for ${{ matrix.service }} if: github.ref == 'refs/heads/master' && github.event_name == 'push' run: | docker buildx create --use - docker buildx build --platform linux/amd64,linux/arm64 -f backend.dockerfile -t itzcrazykns1337/perplexica-backend:main --push . - docker buildx build --platform linux/amd64,linux/arm64 -f app.dockerfile -t itzcrazykns1337/perplexica-frontend:main --push . + if [[ "${{ matrix.service }}" == "backend" ]]; then \ + DOCKERFILE=backend.dockerfile; \ + IMAGE_NAME=perplexica-backend; \ + else \ + DOCKERFILE=app.dockerfile; \ + IMAGE_NAME=perplexica-frontend; \ + fi + docker buildx build --platform linux/amd64,linux/arm64 \ + --cache-from=type=registry,ref=itzcrazykns1337/${IMAGE_NAME}:main \ + --cache-to=type=inline \ + -f $DOCKERFILE \ + -t itzcrazykns1337/${IMAGE_NAME}:main \ + --push . - - name: Build and push release Docker image + - name: Build and push release Docker image for ${{ matrix.service }} if: github.event_name == 'release' run: | docker buildx create --use - docker buildx build --platform linux/amd64,linux/arm64 -f backend.dockerfile -t itzcrazykns1337/perplexica-backend:${{ env.RELEASE_VERSION }} --push . - docker buildx build --platform linux/amd64,linux/arm64 -f app.dockerfile -t itzcrazykns1337/perplexica-frontend:${{ env.RELEASE_VERSION }} --push . \ No newline at end of file + if [[ "${{ matrix.service }}" == "backend" ]]; then \ + DOCKERFILE=backend.dockerfile; \ + IMAGE_NAME=perplexica-backend; \ + else \ + DOCKERFILE=app.dockerfile; \ + IMAGE_NAME=perplexica-frontend; \ + fi + docker buildx build --platform linux/amd64,linux/arm64 \ + --cache-from=type=registry,ref=itzcrazykns1337/${IMAGE_NAME}:${{ env.RELEASE_VERSION }} \ + --cache-to=type=inline \ + -f $DOCKERFILE \ + -t itzcrazykns1337/${IMAGE_NAME}:${{ env.RELEASE_VERSION }} \ + --push . diff --git a/app.dockerfile b/app.dockerfile index 8337171b..ff1824db 100644 --- a/app.dockerfile +++ b/app.dockerfile @@ -9,7 +9,7 @@ WORKDIR /home/perplexica COPY ui /home/perplexica/ -RUN yarn install +RUN yarn install --frozen-lockfile RUN yarn build CMD ["yarn", "start"] \ No newline at end of file diff --git a/backend.dockerfile b/backend.dockerfile index 70c30e8d..b8d0155b 100644 --- a/backend.dockerfile +++ b/backend.dockerfile @@ -10,7 +10,7 @@ COPY yarn.lock /home/perplexica/ RUN mkdir /home/perplexica/data -RUN yarn install +RUN yarn install --frozen-lockfile RUN yarn build CMD ["yarn", "start"] \ No newline at end of file