Skip to content

Commit

Permalink
Merge pull request #621 from AmbientRun/deploy-web
Browse files Browse the repository at this point in the history
Deploy ambient web client
  • Loading branch information
ten3roberts authored Jul 28, 2023
2 parents 8ec06c7 + 527b8e7 commit f43e866
Show file tree
Hide file tree
Showing 9 changed files with 218 additions and 59 deletions.
28 changes: 28 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,34 @@ jobs:
- name: Check that guest/rust builds under all configurations
run: cargo run -p campfire example check-all

build-web:
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3

- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
target: wasm32-unknown-unknown

- name: "Build campfire"
uses: actions-rs/cargo@v1
with:
command: build
args: --package campfire

- name: "Build web client"
uses: actions-rs/cargo@v1
with:
command: run
args: --package campfire -- web build --profile release --target standalone

- name: Upload package
uses: actions/upload-artifact@v3
with:
name: ambient-web-standalone
path: ./web/pkg

build:
strategy:
matrix:
Expand Down
66 changes: 66 additions & 0 deletions .github/workflows/deploy-web.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: "deploy-web"

on:
workflow_dispatch:
push:
tags: [v*]
branches:
- main

env:
PACKAGE_NAME: ambient-web-${{ github.sha }}

jobs:
build:
permissions:
id-token: write
runs-on: ubuntu-20.04
steps:
- uses: actions/checkout@v3

- uses: dtolnay/rust-toolchain@stable
with:
toolchain: stable
target: wasm32-unknown-unknown

- name: "Build campfire"
uses: actions-rs/cargo@v1
with:
command: build
args: --package campfire

- name: "Build web client"
uses: actions-rs/cargo@v1
with:
command: run
args: --package campfire -- web build --profile release --target standalone --pkg-name ${{ env.PACKAGE_NAME }}

- name: Upload package
uses: actions/upload-artifact@v3
with:
name: ambient-web-standalone
path: ./web/${{ env.PACKAGE_NAME }}

- id: auth
name: Authenticate with Google Cloud
uses: google-github-actions/auth@v0
with:
token_format: access_token
workload_identity_provider: projects/549180905870/locations/global/workloadIdentityPools/github-pool/providers/github-provider
service_account: [email protected]
access_token_lifetime: 1800s

- name: Upload to Google Cloud
uses: google-github-actions/upload-cloud-storage@v1
with:
path: "./web/${{ env.PACKAGE_NAME }}"
destination: "ambient-artifacts/ambient-web-clients"

- name: Upload Canary to Google Cloud
uses: google-github-actions/upload-cloud-storage@v1
with:
path: "./web/${{ env.PACKAGE_NAME }}"
destination: "ambient-artifacts/ambient-web-clients/ambient-canary"
parent: false


1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 3 additions & 11 deletions campfire/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,15 @@ simplelog = "0.12.1"
cargo_toml = "0.15"
guppy = "0.15"
indicatif = "0.17.3"
tokio = { version = "1.0", features = [
"fs",
"macros",
"parking_lot",
"process",
"rt",
"time",
] }
tokio = { version = "1.0", features = ["fs", "macros", "rt", "process"] }
futures = "0.3"
num_cpus = "1.15.0"
home = "0.5"
tracing = "0.1"
base64 = "0.21"
cfg-if = "1.0"

[target.'cfg(target_os="windows")'.dependencies]
openssl = { version = "0.10", features = ["vendored"] }
openssl = { version = "0.10", features = ["vendored"], optional = true }

[target.'cfg(not(target_os="windows"))'.dependencies]
openssl = { version = "0.10" }
openssl = { version = "0.10", optional = true }
2 changes: 1 addition & 1 deletion campfire/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use campfire::{
cli::Cli,
doc, example, golden_images, install, release,
web::{self, Web},
web::{self},
};
use clap::Parser;

Expand Down
91 changes: 66 additions & 25 deletions campfire/src/web/build.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,6 @@
use std::{
path::{Path, PathBuf},
sync::Arc,
};

use anyhow::Context;
use clap::{Args, Subcommand, ValueEnum};
use tokio::process::Command;
use clap::{Args, ValueEnum};
use std::{path::PathBuf, process::Command};

#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
pub(crate) enum Target {
Expand All @@ -19,31 +14,75 @@ pub(crate) enum Target {
pub struct BuildOptions {
#[arg(long, default_value = "dev")]
pub profile: String,
#[arg(long, default_value = "pkg")]
pub pkg_name: String,
#[arg(long, value_enum, default_value = "bundler")]
target: Target,
}

pub async fn run(opts: BuildOptions) -> anyhow::Result<()> {
ensure_wasm_pack().await?;
let output_path = run_cargo_build(&opts).await?;
pub fn run(opts: BuildOptions) -> anyhow::Result<()> {
ensure_wasm_pack()?;

let output_path = run_cargo_build(&opts)?;

eprintln!("Built package: {:?}", output_path);

Ok(())
}

pub async fn ensure_wasm_pack() -> anyhow::Result<()> {
#[cfg(not(target_os = "linux"))]
pub(crate) fn install_wasm_pack() -> anyhow::Result<()> {
eprintln!("Installing wasm-pack from source");
let status = Command::new("cargo")
.args(["install", "wasm-pack"])
.spawn()?
.wait()?;

if !status.success() {
anyhow::bail!("Failed to install wasm-pack");
}

Ok(())
}

#[cfg(target_os = "linux")]
pub(crate) fn install_wasm_pack() -> anyhow::Result<()> {
eprintln!("Installing wasm-pack");
let mut curl = Command::new("curl")
.args([
"https://rustwasm.github.io/wasm-pack/installer/init.sh",
"-sSf",
])
.stdout(std::process::Stdio::piped())
.spawn()
.context("Failed to spawn curl")?;

let mut sh = Command::new("sh")
.stdin(std::process::Stdio::from(curl.stdout.take().unwrap()))
.spawn()
.context("Failed to spawn sh")?;

let sh = sh.wait()?;

let curl = curl.wait()?;

if !curl.success() {
anyhow::bail!("Failed to fetch install script")
}

if !sh.success() {
anyhow::bail!("Failed to run install script for wasm-pack")
}

Ok(())
}

pub fn ensure_wasm_pack() -> anyhow::Result<()> {
match which::which("wasm-pack") {
Err(_) => {
eprintln!("Installing wasm-pack");
let status = Command::new("carg")
.args(["install", "wasm-pack"])
.spawn()?
.wait()
.await?;
if !status.success() {
anyhow::bail!("Failed to install wasm-pack");
}
install_wasm_pack()?;

assert!(which::which("wasm-pack").is_ok(), "wasm-pack is in PATH");

Ok(())
}
Expand All @@ -54,7 +93,7 @@ pub async fn ensure_wasm_pack() -> anyhow::Result<()> {
}
}

pub async fn run_cargo_build(opts: &BuildOptions) -> anyhow::Result<PathBuf> {
pub fn run_cargo_build(opts: &BuildOptions) -> anyhow::Result<PathBuf> {
let mut command = Command::new("wasm-pack");

command.args(["build", "client"]).current_dir("web");
Expand All @@ -67,21 +106,23 @@ pub async fn run_cargo_build(opts: &BuildOptions) -> anyhow::Result<PathBuf> {

match opts.target {
Target::Bundler => command.args(["--target", "bundler"]),
Target::Standalone => command.args(["--target", "no-modules", "--no-pack"]),
Target::Standalone => command.args(["--target", "web", "--no-pack"]),
};

// See: https://doc.rust-lang.org/cargo/guide/build-cache.html
let output_path = ["web", "pkg"]
let mut output_path = ["web"]
.iter()
.collect::<PathBuf>()
.canonicalize()
.context("Produced build artifact does not exist")?;

output_path.push(&opts.pkg_name);

command.arg("--out-dir").arg(output_path.clone());

eprintln!("Building web client");

let res = command.spawn()?.wait().await?;
let res = command.spawn()?.wait()?;

if !res.success() {
anyhow::bail!("Building package failed with status code: {res}");
}
Expand Down
14 changes: 12 additions & 2 deletions campfire/src/web/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use clap::Subcommand;

use self::build::BuildOptions;

#[cfg(feature = "openssl")]
mod browser;
mod build;

Expand All @@ -16,7 +17,16 @@ pub enum Web {

pub async fn run(command: Web) -> anyhow::Result<()> {
match command {
Web::Build(args) => build::run(args).await,
Web::OpenBrowser => browser::open().await,
Web::Build(args) => build::run(args),
Web::OpenBrowser => {
#[cfg(feature = "openssl")]
{
browser::open().await
}
#[cfg(not(feature = "openssl"))]
{
anyhow::bail!("The `openssl` feature must be enabled to use this command")
}
}
}
}
2 changes: 1 addition & 1 deletion guest/rust/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit f43e866

Please sign in to comment.