Skip to content

Open source website builder and Webflow alternative. Webstudio is an advanced visual builder that connects to any headless CMS, supports all CSS properties, and can be hosted anywhere, including with us.

build: CLI container to use in webstudio builder publish process (#3163)

Ivan Starkov7fdfb50fb0b2
diff --git a/.dockerignore b/.dockerignore
new file mode 100644
index 0000000000..8c846bb5ad
--- /dev/null
+++ b/.dockerignore
@@ -0,0 +1,61 @@
+# See https://help.github.com/articles/ignoring-files/ for more about ignoring files.
+
+# dependencies
+node_modules
+.pnp
+.pnp.js
+yarn.lock
+
+# testing
+coverage
+
+# remix
+build
+_build
+.cache
+.vercel
+.output
+.netlify
+
+# misc
+.DS_Store
+*.pem
+/.idea
+
+# logs
+npm-debug.log*
+yarn-debug.log*
+yarn-error.log*
+.pnpm-debug.log*
+
+# local env files
+.env
+.env.local
+.env.development.local
+.env.test.local
+.env.production.local
+
+# turbo
+.turbo
+
+# data
+/data
+*.db
+*.db-journal
+
+# migrations
+**/prisma/migrations/lockfile
+**/prisma/migrations/*/client
+
+# builds
+packages/**/lib
+generated
+storybook-static
+tsconfig.tsbuildinfo
+
+# to save thunder https://marketplace.visualstudio.com/items?itemName=rangav.vscode-thunder-client files
+.thunder
+.env*.local
+
+# wrangler builds
+dist
diff --git a/.github/actions/vercel/action.yaml b/.github/actions/vercel/action.yaml
index 980c64b532..a07451a37f 100644
--- a/.github/actions/vercel/action.yaml
+++ b/.github/actions/vercel/action.yaml
@@ -37,14 +37,16 @@ runs:
steps:
- id: branch
run: |
- CLEAN_NAME="${GITHUB_REF_NAME/.staging/}"
+ CLEAN_NAME="${REF_NAME/.staging/}"
CLEAN_NAME=$( echo "${CLEAN_NAME}" | sed 's/[^a-zA-Z0-9_-]//g' | tr A-Z a-z | tr _ - | sed 's/-\{2,\}/-/g' )
echo "value=${CLEAN_NAME}" >> $GITHUB_OUTPUT
shell: bash
+ env:
+ REF_NAME: ${{ inputs.ref-name }}
- id: short_sha
run: |
- SHORT_SHA=$( echo "value=$(echo ${{ github.sha }} | cut -c1-7)" )
+ SHORT_SHA=$( echo "value=$(echo ${{ inputs.sha }} | cut -c1-7)" )
echo "value=${SHORT_SHA}" >> $GITHUB_OUTPUT
shell: bash
@@ -81,8 +83,8 @@ runs:
--prebuilt \
--token ${{ inputs.vercel-token }} \
--env SHORT_SHA=${{ steps.short_sha.outputs.value }} \
- --env BRANCH_NAME=${{ steps.branch.outputs.value }} \
- --meta GITHUB_SHA=${{ steps.short_sha.outputs.value }} \
+ --env BRANCH_NAME=${{ inputs.ref-name }} \
+ --meta GITHUB_SHA=${{ inputs.sha }} \
--meta GITHUB_REF_NAME=${{ steps.branch.outputs.value }} \
2> >(tee info.txt >&2) | tee domain.txt
diff --git a/.github/workflows/cli-docker.yaml b/.github/workflows/cli-docker.yaml
new file mode 100644
index 0000000000..dd9cde9ded
--- /dev/null
+++ b/.github/workflows/cli-docker.yaml
@@ -0,0 +1,62 @@
+name: CLI Docker
+
+on:
+ push:
+ branches:
+ - "*.staging"
+
+# cancel in-progress runs on new commits to same PR (gitub.event.number)
+concurrency:
+ group: vercel-deploy-${{ github.workflow }}-${{ github.event.number || github.sha }}
+ cancel-in-progress: true
+
+permissions:
+ contents: read # to fetch code (actions/checkout)
+ statuses: write # This is required for the GitHub Script createCommitStatus to work
+
+jobs:
+ build:
+ environment:
+ name: "staging"
+
+ timeout-minutes: 20
+
+ runs-on: ubuntu-latest
+
+ steps:
+ - uses: actions/checkout@v3
+ with:
+ ref: ${{ github.event.pull_request.head.sha || github.sha }} # HEAD commit instead of merge commit
+
+ - name: Set up Docker Buildx
+ uses: docker/setup-buildx-action@v3
+
+ - name: Login to Docker Hub
+ uses: docker/login-action@v3
+ with:
+ username: ${{ secrets.DOCKERHUB_USER }}
+ password: ${{ secrets.DOCKERHUB_TOKEN }}
+
+ # To be able to pull image by branh name, sha, short sha and tag
+ # i.e. olegisonen/webstudio:hello.staging
+ - name: Docker meta
+ id: meta
+ uses: docker/metadata-action@v5
+ with:
+ # TODO: create team someday on dockerhub
+ images: |
+ olegisonen/webstudio
+ tags: |
+ type=sha,format=long
+ type=sha,format=short
+ type=ref,event=branch
+ type=ref,event=tag
+
+ - name: Build and push
+ uses: docker/build-push-action@v5
+ with:
+ context: .
+ file: ./devops/cli/Dockerfile
+ push: true
+ tags: ${{ steps.meta.outputs.tags }}
+ labels: ${{ steps.meta.outputs.labels }}
diff --git a/.gitignore b/.gitignore
index fae29ad83c..391ae908a8 100644
--- a/.gitignore
+++ b/.gitignore
@@ -53,3 +53,6 @@ tsconfig.tsbuildinfo
# to save thunder https://marketplace.visualstudio.com/items?itemName=rangav.vscode-thunder-client files
.thunder
.env*.local
+
+# wrangler builds
+dist
diff --git a/apps/builder/app/shared/context.server.ts b/apps/builder/app/shared/context.server.ts
index ddf1e415f5..b450e4a610 100644
--- a/apps/builder/app/shared/context.server.ts
+++ b/apps/builder/app/shared/context.server.ts
@@ -45,13 +45,24 @@ const createDomainContext = (request: Request) => {
return context;
};
-const createDeploymentContext = (request: Request) => {
+const getRequestOrigin = (request: Request) => {
const url = new URL(request.url);
+ console.info("getRequestOrigin", [...request.headers.entries()]);
+
+ // vercel overwrites x-forwarded-host on edge level even if our header is set
+ // as workaround we use custom header x-forwarded-ws-host to get the original host
+ url.host =
+ request.headers.get("x-forwarded-ws-host") ??
+ request.headers.get("x-forwarded-host") ??
+ url.host;
+ return url.origin;
+};
+const createDeploymentContext = (request: Request) => {
const context: AppContext["deployment"] = {
deploymentTrpc: trpcSharedClient.deployment,
env: {
- BUILDER_ORIGIN: url.origin,
+ BUILDER_ORIGIN: `${getRequestOrigin(request)}`,
BRANCH_NAME: env.BRANCH_NAME ?? "main",
},
};
diff --git a/devops/cli/Dockerfile b/devops/cli/Dockerfile
new file mode 100644
index 0000000000..6efa261690
--- /dev/null
+++ b/devops/cli/Dockerfile
@@ -0,0 +1,47 @@
+# Container image that runs your code
+# Execute from the root of the repository:
+# docker build -t docker-cli -f ./devops/cli/Dockerfile . --progress=plain
+# docker run --rm -it docker-cli sh
+# docker run --rm -it --env-file <(infisical export --format=dotenv --path='/GITHUB_SAAS' --env=dev | while IFS= read -r line; do eval "echo $line"; done) docker-cli rclone copy -P -M --no-check-dest --header "Cache-Control: public,max-age=31536004,immutable" '/cli/package.json' 'r2:/build-artifacts'
+FROM node:18.20-alpine3.18
+
+ENV CI=true
+ENV SKIP_INSTALL_SIMPLE_GIT_HOOKS=true
+ENV PRISMA_SKIP_POSTINSTALL_GENERATE=true
+
+RUN apk update && apk add curl rclone postgresql14-client --no-cache
+
+COPY ./ /cli
+WORKDIR /cli
+
+ENV PNPM_HOME="/.pnpm/store"
+ENV PATH="$PNPM_HOME:$PATH"
+
+RUN corepack enable pnpm && mkdir -p $PNPM_HOME
+RUN pnpm i -g zx
+
+# Install deps, build cli
+RUN pnpm install --ignore-scripts
+RUN pnpm -r --filter './packages/*' run build
+
+# Test build is working
+
+ENV COMPATIBILITY_DATE=2024-04-10
+WORKDIR /cli/fixtures/webstudio-cloudlfare-template
+
+RUN \
+ pnpm fixtures:build && \
+ pnpm build && \
+ NODE_ENV=production pnpm wrangler deploy \
+ --name build \
+ --compatibility-date ${COMPATIBILITY_DATE} \
+ --minify true \
+ --logpush true \
+ --dry-run \
+ --outdir dist \
+ './functions/[[path]].ts'
+
+# Clean up
+RUN rm -rf dist && rm -rf build && rm -rf .wrangler
+
+# TODO: Layered build, production deps only
\ No newline at end of file
diff --git a/fixtures/webstudio-cloudlfare-template/vite.config.ts b/fixtures/webstudio-cloudlfare-template/vite.config.ts
index 37b93b6d03..8a9388f44a 100644
--- a/fixtures/webstudio-cloudlfare-template/vite.config.ts
+++ b/fixtures/webstudio-cloudlfare-template/vite.config.ts
@@ -5,6 +5,11 @@ import {
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
-export default defineConfig({
- plugins: [remixCloudflareDevProxy(), remix(), tsconfigPaths()],
-});
+export default defineConfig(({ mode }) => ({
+ plugins: [
+ // without this, remixCloudflareDevProxy trying to load workerd even for production (it's not needed for production)
+ mode === "production" ? undefined : remixCloudflareDevProxy(),
+ remix(),
+ tsconfigPaths(),
+ ].filter(Boolean),
+}));
diff --git a/package.json b/package.json
index bfa97bd238..539df0cc25 100644
--- a/package.json
+++ b/package.json
@@ -14,7 +14,7 @@
"storybook:build": "storybook build",
"migrations": "cd apps/builder && pnpm migrations --dev",
"build-figma-tokens": "cd packages/design-system && pnpm build-figma-tokens",
- "prepare": "git config core.hooksPath .git/hooks/ && simple-git-hooks",
+ "prepare": "which git && git config core.hooksPath .git/hooks/ && simple-git-hooks || echo git not installed",
"local:version-snapshot": "pnpm -r exec pnpm version prepatch --preid $(cat /dev/urandom | LC_ALL=C tr -dc 'a-z' | fold -w 8 | head -n 1)",
"local:publish-snapshot": "pnpm -r publish --access public --no-git-checks --registry http://localhost:4873",
"local:dangerously-undo-version-snapshot": "git restore --source=$(git branch --show-current) '**/*/package.json'",
diff --git a/packages/cli/templates/cloudflare/vite.config.ts b/packages/cli/templates/cloudflare/vite.config.ts
index 37b93b6d03..8a9388f44a 100644
--- a/packages/cli/templates/cloudflare/vite.config.ts
+++ b/packages/cli/templates/cloudflare/vite.config.ts
@@ -5,6 +5,11 @@ import {
import { defineConfig } from "vite";
import tsconfigPaths from "vite-tsconfig-paths";
-export default defineConfig({
- plugins: [remixCloudflareDevProxy(), remix(), tsconfigPaths()],
-});
+export default defineConfig(({ mode }) => ({
+ plugins: [
+ // without this, remixCloudflareDevProxy trying to load workerd even for production (it's not needed for production)
+ mode === "production" ? undefined : remixCloudflareDevProxy(),
+ remix(),
+ tsconfigPaths(),
+ ].filter(Boolean),
+}));