Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docker build and publishing #30

Merged
merged 7 commits into from
Sep 29, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
.dockerignore
.git
.next
.turbo
dist
docker
Dockerfile
node_modules
npm-debug.log
README.md
58 changes: 58 additions & 0 deletions .github/workflows/docker.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
name: Docker
on:
push:
tags:
- "v*"
pull_request:
concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true
permissions:
contents: read
packages: write
id-token: write
jobs:
build:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v4

- name: Docker meta
id: docker_meta
uses: docker/metadata-action@v4
with:
images: |
name=ghcr.io/cartesi/rollups-explorer
name=docker.io/cartesi/rollups-explorer,enable=${{ startsWith(github.ref, 'refs/tags/v') }}
tags: |
type=semver,pattern={{version}}
type=ref,event=branch
type=ref,event=pr

- name: Login to Docker Hub
uses: docker/login-action@v2
if: ${{ startsWith(github.ref, 'refs/tags/v') }}
with:
username: ${{ secrets.DOCKER_USERNAME }}
password: ${{ secrets.DOCKER_PASSWORD }}

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}

- uses: depot/setup-action@v1

- name: Build and push docker image
id: docker_build
uses: depot/bake-action@v1
with:
files: |
./docker-bake.hcl
${{ steps.docker_meta.outputs.bake-file }}
./docker-bake.platforms.hcl
push: true
project: ${{ vars.DEPOT_PROJECT }}
5 changes: 4 additions & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,8 @@
"editor.codeActionsOnSave": {
"source.fixAll": true,
"source.organizeImports": true
},
"[terraform]": {
"editor.defaultFormatter": "hashicorp.terraform"
}
}
}
2 changes: 1 addition & 1 deletion apps/web/.env.localhost
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
NEXT_PUBLIC_CHAIN_ID=31337
NEXT_PUBLIC_EXPLORER_API_URL="http://127.0.0.1:8545"
NEXT_PUBLIC_EXPLORER_API_URL="http://127.0.0.1:4350/graphql"
3 changes: 3 additions & 0 deletions apps/web/.env.sunodo
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
NEXT_PUBLIC_CHAIN_ID=31337
NEXT_PUBLIC_EXPLORER_API_URL="http://squid_api:4350/graphql"
BASE_PATH="/explorer"
9 changes: 3 additions & 6 deletions apps/web/codegen.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
import type { CodegenConfig } from "@graphql-codegen/cli";

const schema = process.env.NEXT_PUBLIC_EXPLORER_API_URL;

if (!schema)
throw new Error(
"NEXT_PUBLIC_EXPLORER_API_URL environment variable is required for GraphQL code generation.",
);
const schema = "https://squid.subsquid.io/cartesi-rollups-mainnet/graphql";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@tuler, Is it not better to get from the environment variable instead of hardcoded?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the problem of using the environment is that often we want to build for localhost but the localhost environment is not necessarily running during the build

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

But then you can set up that build to whatever URL you want to work with. Because the environments (sepolia/mainnet) may be in parity now, but that is not guaranteed, and the testnet is more likely to have new graphQL updates that would cause build problems on Vercel, for example. I think we don't have this defined here on GitHub action variables, but I would setup to use Sepolia endpoints.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So maybe we should separate the NEXT_PUBLIC_EXPLORER_API_URL variable from the variable used by the codegen?

Copy link
Collaborator

@brunomenezes brunomenezes Sep 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah. For now, it shouldn't be a problem and should not be a blocker to proceed with Sunodo's developments. But we will need to improve that part for this code generator to support multiple situations, e.g. Our own Vercel deployments, docker image generation for external use, etc.

// const schema = "https://squid.subsquid.io/cartesi-rollups-sepolia/graphql";
// const schema = "http://127.0.0.1:4350/graphql";

const plugins = [
{
Expand Down
2 changes: 2 additions & 0 deletions apps/web/next.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const nextConfig = {
reactStrictMode: true,
swcMinify: true,
output: "standalone",
basePath: process.env.BASE_PATH || "",
};

module.exports = nextConfig;
11 changes: 11 additions & 0 deletions docker-bake.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
target "docker-metadata-action" {}
target "docker-platforms" {}

target "default" {
inherits = ["docker-metadata-action", "docker-platforms"]
dockerfile = "docker/Dockerfile"
context = "."
args = {
"ENVIRONMENT" = "sunodo"
brunomenezes marked this conversation as resolved.
Show resolved Hide resolved
}
}
3 changes: 3 additions & 0 deletions docker-bake.override.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
target "default" {
tags = ["cartesi/rollups-explorer:devel"]
}
6 changes: 6 additions & 0 deletions docker-bake.platforms.hcl
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
target "docker-platforms" {
platforms = [
"linux/amd64",
"linux/arm64"
]
}
53 changes: 53 additions & 0 deletions docker/Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
FROM node:20-alpine as base

FROM base as builder
RUN apk add --no-cache libc6-compat
RUN apk update

WORKDIR /app
RUN yarn global add turbo
COPY . .
RUN turbo prune --scope=web --docker

# Add lockfile and package.json's of isolated subworkspace
FROM base AS installer
RUN apk add --no-cache libc6-compat
RUN apk update
WORKDIR /app

# First install the dependencies (as they change less often)
COPY .gitignore .gitignore
COPY --from=builder /app/out/json/ .
COPY --from=builder /app/out/yarn.lock ./yarn.lock
RUN yarn install

# Build the project
COPY --from=builder /app/out/full/ .
ARG ENVIRONMENT=localhost
COPY --from=builder /app/out/full/apps/web/.env.${ENVIRONMENT} ./apps/web/.env
RUN yarn build --filter=web

FROM base AS runner
WORKDIR /app

ENV NODE_ENV=production

# Don't run production as root
RUN addgroup --system --gid 1001 nodejs
RUN adduser --system --uid 1001 nextjs

#COPY --from=installer /app/apps/web/public ./public
COPY --from=installer /app/apps/web/next.config.js .
COPY --from=installer /app/apps/web/package.json .

# Automatically leverage output traces to reduce image size
# https://nextjs.org/docs/advanced-features/output-file-tracing
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/standalone ./
COPY --from=installer --chown=nextjs:nodejs /app/apps/web/.next/static ./apps/web/.next/static

USER nextjs
EXPOSE 3000
ENV PORT 3000
ENV HOSTNAME 0.0.0.0

CMD ["node", "apps/web/server.js"]
14 changes: 14 additions & 0 deletions docker/docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
version: "3.9"

services:
proxy:
image: traefik:v2.10
ports:
- 8080:8080
- 8088:8088
volumes:
- ./traefik:/etc/traefik
explorer:
image: cartesi/rollups-explorer:devel
expose:
- 3000
10 changes: 10 additions & 0 deletions docker/traefik/conf.d/explorer.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
http:
routers:
explorer:
rule: "PathPrefix(`/explorer`)"
service: explorer
services:
explorer:
loadBalancer:
servers:
- url: "http://explorer:3000"
11 changes: 11 additions & 0 deletions docker/traefik/traefik.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
api:
dashboard: true
insecure: true
entryPoints:
web:
address: ":8080"
traefik:
address: ":8088"
providers:
file:
directory: /etc/traefik/conf.d
6 changes: 5 additions & 1 deletion turbo.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
{
"$schema": "https://turbo.build/schema.json",
"globalEnv": ["NEXT_PUBLIC_CHAIN_ID", "NEXT_PUBLIC_EXPLORER_API_URL"],
"globalEnv": [
"BASE_PATH",
"NEXT_PUBLIC_CHAIN_ID",
"NEXT_PUBLIC_EXPLORER_API_URL"
],
"pipeline": {
"build": {
"dependsOn": ["^build", "codegen"],
Expand Down