Skip to content

Instantly share code, notes, and snippets.

@RJ
Created December 1, 2024 14:42
Show Gist options
  • Save RJ/230cd5bd6264b3994cf765caccb288aa to your computer and use it in GitHub Desktop.
Save RJ/230cd5bd6264b3994cf765caccb288aa to your computer and use it in GitHub Desktop.
Builds your wasm on a macos runner, which has more resources, then uses ubuntu-latest to dockerize it.
name: WASM build and nginxify
on:
push:
tags:
- 'v*'
workflow_dispatch:
inputs:
version:
description: 'Version number in the format `v1.2.3`'
required: true
type: string
env:
IMAGE_NAME: "username/image-name"
jobs:
compile-wasm-artifact:
# has more resources, but costs more minutes, but comes out of usual minutes allowance.
# default ubuntu-latest for private repos is 2x CPU, 7gb, macos-13 is 4x CPU, 14gb.
# so we build wasm natively on macos, upload artifact, and download in an ubuntu-latest job
# to copy into an nginx docker image for linux deployment.
runs-on: macos-13
steps:
- name: Checkout Code
uses: actions/checkout@v3
- name: Install Rust
uses: actions-rs/toolchain@v1
with:
toolchain: stable
override: true
- name: Install wasm-bindgen-cli
run: |
cargo install wasm-bindgen-cli
- name: Cargo build (for wasm)
run: |
rustup target add wasm32-unknown-unknown
RUSTFLAGS=--cfg=web_sys_unstable_apis TARGET_CC=$(brew --prefix llvm@15)/bin/clang cargo build --release -p spacepit_client --target wasm32-unknown-unknown
- name: Run wasm-bindgen, prep out dir
run: |
wasm-bindgen --no-typescript --target web \
--out-dir ./out/ \
--out-name "spacepit" \
./target/wasm32-unknown-unknown/release/spacepit_client.wasm
cp -r client/www/* ./out/
cp -r assets ./out/
- name: Archive compiled wasm and assets
uses: actions/upload-artifact@v4
with:
name: wasm-bundle
path: out
put-wasm-in-nginx:
runs-on: ubuntu-latest
needs: compile-wasm-artifact
steps:
- name: Download wasm-bundle artifact
uses: actions/download-artifact@v4
with:
name: wasm-bundle
- name: Prepare Dockerfile
run: |
echo '
FROM nginx:mainline-alpine
COPY * /usr/share/nginx/html/
' > Dockerfile.generated
- name: Docker meta
id: meta
uses: docker/metadata-action@v5
with:
images: ${{ env.IMAGE_NAME}}
tags: |
type=semver,pattern={{version}}
type=sha,format=long
latest
- name: Setup buildx
uses: docker/setup-buildx-action@v3
- name: Login to Container Registry
uses: docker/login-action@v3
with:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_PASSWORD }}
- name: Build and push Docker image
uses: docker/build-push-action@v6
with:
push: true
context: .
file: ./Dockerfile.generated
cache-from: type=gha
cache-to: type=gha,mode=max
tags: ${{ steps.meta.outputs.tags }}
labels: ${{ steps.meta.outputs.labels }}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment