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

www: remove corporate site, revamp static build #2106

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
117 changes: 6 additions & 111 deletions .github/workflows/build.yaml
Expand Up @@ -16,7 +16,7 @@ concurrency:
jobs:
build:
name: Build binary packages
runs-on: macos-11
runs-on: ubuntu-latest
steps:
- name: Check out code
uses: actions/checkout@v3
Expand All @@ -42,124 +42,20 @@ jobs:
path: "**/node_modules"
key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

- name: restore pkg cache
id: cache-pkg
uses: actions/cache@v3
with:
path: "~/.pkg-cache"
key: ${{ runner.os }}-${{ hashFiles('**/yarn.lock') }}

- name: yarn install
run: yarn install --frozen-lockfile --silent

- name: build pkgs
- name: build api server
# I want to do --parallel here, but the binary downloads conflict with each other
run: |
echo RELEASE_PATH="${GITHUB_WORKSPACE}/releases" >> "${GITHUB_ENV}"
echo BIN_PATH="${GITHUB_WORKSPACE}/bin" >> "${GITHUB_ENV}"
yarn run lerna-run pkg
yarn run esbuild

- name: Rename and move built binaries
run: |
mkdir -p "${RELEASE_PATH}" "${BIN_PATH}"

for arch in linux-arm64 linux-x64 macos-arm64 macos-x64 win-x64; do
package="api"
in_name="$package-$arch"
out_name="livepeer-$package"
archive_name="livepeer-$(echo $in_name | sed -e 's/x64/amd64/;s/win/windows/;s/macos/darwin/')"
cd ./packages/$package/bin
case "$arch" in
win-x64)
in_name="$in_name.exe"
out_name="$out_name.exe"
mv "./$in_name" "./$out_name"
zip -q9 "${RELEASE_PATH}/${archive_name}.zip" "./$out_name"
;;
linux-*)
mv "./$in_name" "./$out_name"
tar -czvf "${RELEASE_PATH}/${archive_name}.tar.gz" "./$out_name"
;;
macos-*)
mkdir -p "${BIN_PATH}/$arch"
mv "./$in_name" "${BIN_PATH}/$arch/$out_name"
;;
esac
cd -
done

- name: Upload macos binaries for codesigning (arm64)
uses: actions/upload-artifact@v3
with:
name: macos-arm64
path: bin/macos-arm64

- name: Upload macos binaries for codesigning (amd64)
uses: actions/upload-artifact@v3
with:
name: macos-amd64
path: bin/macos-x64

- name: Upload artifacts for cutting release
uses: actions/upload-artifact@v3
with:
name: release-artifacts
path: releases/

macos:
strategy:
fail-fast: true
matrix:
arch:
- amd64
- arm64
name: Codesign and archive macOS binaries
runs-on: macos-11
needs: build
steps:
- name: Setup env
run: |
echo ARCH="${{ matrix.arch }}" >> "$GITHUB_ENV"

- name: Download binaries from build stage
uses: actions/download-artifact@v3
with:
name: macos-${{ matrix.arch }}
path: bin/

- name: Fix file permissions
run: |
cd bin/
chmod a+x livepeer-*

- uses: actions-ecosystem/action-regex-match@v2
id: match-tag
with:
text: ${{ github.ref_name }}
regex: '^(master|main|v[0-9]+\.\d+\.\d+)$'

- name: Codesign and notarize binaries
if: ${{ steps.match-tag.outputs.match != '' }}
uses: livepeer/action-gh-codesign-apple@latest
with:
developer-certificate-id: ${{ secrets.CI_MACOS_CERTIFICATE_ID }}
developer-certificate-base64:
${{ secrets.CI_MACOS_CERTIFICATE_BASE64 }}
developer-certificate-password:
${{ secrets.CI_MACOS_CERTIFICATE_PASSWORD }}
app-notarization-email: ${{ secrets.CI_MACOS_NOTARIZATION_USER }}
app-notarization-password:
${{ secrets.CI_MACOS_NOTARIZATION_PASSWORD }}
app-notarization-team-id: ${{ secrets.CI_MACOS_NOTARIZATION_TEAM_ID }}
binary-path: "bin/"

- name: Archive built binaries
run: |
mkdir -p releases/
cd bin/
for file in $(find . -type f -perm -a+x); do
tar -czf "../releases/${file}-darwin-${ARCH}.tar.gz" "${file}"
done
mkdir -p "${RELEASE_PATH}"
cd ./packages/api/dist-esbuild
tar -czvf "${RELEASE_PATH}/livepeer-api.tar.gz" livepeer-api

- name: Upload artifacts for cutting release
uses: actions/upload-artifact@v3
Expand All @@ -178,7 +74,6 @@ jobs:
runs-on: ubuntu-latest
needs:
- build
- macos
steps:
- name: Download artifacts
uses: actions/download-artifact@v3
Expand Down
3 changes: 0 additions & 3 deletions .github/workflows/docker.yaml
Expand Up @@ -24,9 +24,6 @@ jobs:
fail-fast: false
matrix:
build:
- path: packages/www
image: livepeerci/www
package: www
- path: packages/api
image: livepeerci/api
package: api
Expand Down
48 changes: 0 additions & 48 deletions .github/workflows/esbuild.yaml

This file was deleted.

42 changes: 0 additions & 42 deletions .github/workflows/static.yaml

This file was deleted.

3 changes: 3 additions & 0 deletions .prettierignore
Expand Up @@ -14,3 +14,6 @@ packages/api/src/schema/validators
packages/api/src/schema/types.d.ts
.env
packages/api/src/schema/schema.yaml
packages/www/static-build
packages/www/static-build-app
packages/api/dist-esbuild/api.js
3 changes: 1 addition & 2 deletions package.json
Expand Up @@ -33,8 +33,7 @@
"prettier": "yarn run prettier:base --write",
"prettier:check": "yarn run prettier:base --check",
"release": "lerna version",
"pkg": "lerna run --stream build && lerna run --stream pkg",
"pkg:local": "lerna run --stream build && lerna run --stream pkg:local"
"esbuild": "cd packages/www && yarn run static && cd ../api && yarn run esbuild"
},
"devDependencies": {
"@commitlint/cli": "^16.2.3",
Expand Down
2 changes: 2 additions & 0 deletions packages/api/.prettierignore
@@ -1 +1,3 @@
data
dist
dist-esbuild
7 changes: 4 additions & 3 deletions packages/api/Dockerfile
Expand Up @@ -4,12 +4,13 @@ WORKDIR /app
COPY . .
RUN yarn install --frozen-lockfile --ignore-scripts
RUN yarn run prepare:lerna
RUN cd packages/api && yarn run pkg:local
RUN cd packages/www && yarn run static
RUN cd packages/api && yarn run esbuild

FROM ubuntu:22.04
FROM node:18

WORKDIR app
COPY --from=builder /app/packages/api/bin/api /usr/local/bin/livepeer-api
COPY --from=builder /app/packages/api/dist-esbuild/livepeer-api /usr/local/bin/livepeer-api
ENV NODE_ENV production
ARG VERSION
ENV VERSION ${VERSION}
Expand Down
69 changes: 64 additions & 5 deletions packages/api/esbuild.mjs
@@ -1,16 +1,75 @@
import esbuild from "esbuild";
// Builds a single-file zero-dependency version of the API server
// with an embedded static version of the frontend.

import * as esbuild from "esbuild";
import { readFile, readdir } from "fs/promises";
import { resolve, relative } from "path";

async function getFiles(dir) {
const dirents = await readdir(dir, { withFileTypes: true });
let files = await Promise.all(
dirents.map((dirent) => {
const res = resolve(dir, dirent.name);
return dirent.isDirectory() ? getFiles(res) : res;
})
);
return Array.prototype.concat(...files).filter((f) => !f.includes("cache"));
}

const generateFrontendManifest = async (rootPath) => {
const output = [`module.exports = {`];
let files = await getFiles(rootPath);
for (const file of files) {
const key = relative(rootPath, file);
output.push(` "${key}": require("${file}"),`);
}
output.push(`};`);
return output.join("\n");
};

let frontendBundlePlugin = {
name: "frontendBundle",
setup(build) {
build.onResolve({ filter: /.*frontend-stub$/ }, async (args) => {
return {
path: resolve(args.resolveDir, "..", "..", "www", "static-build"),
namespace: "frontendBundle",
};
});

// Any files from the static-build directory should be bundled as binary
build.onLoad(
{ namespace: "frontendBundle", filter: /.*static\-build/ },
async (args) => {
return {
contents: await generateFrontendManifest(args.path),
loader: "js",
resolveDir: resolve(args.resolveDir, args.path),
};
}
);

// Any files from the static-build directory should be bundled as binary
build.onLoad({ filter: /.*static\-build.+/ }, async (args) => {
const contents = await readFile(args.path);
return {
contents: contents,
loader: "binary",
};
});
},
};

(async () => {
await esbuild.build({
entryPoints: ["./src/cli.ts"],
bundle: true,
platform: "node",
outfile: "./dist-esbuild/api.js",
outfile: "./dist-esbuild/livepeer-api",
target: "node18",
alias: {
"@livepeer.studio/www": "./src/frontend-stub.ts",
},
external: ["pg-native"],
sourcemap: "inline",
plugins: [frontendBundlePlugin],
minify: true,
});
})();