From c733d807539ba98f40956b05c2fbdd1e6c181abb Mon Sep 17 00:00:00 2001 From: riordanp Date: Wed, 21 Dec 2022 11:22:34 +0000 Subject: [PATCH] Allow passing env vars to verifiable build container (#2325) --- CHANGELOG.md | 4 ++++ cli/src/lib.rs | 56 +++++++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 9073e7dc9b..473f5dfd21 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -10,6 +10,10 @@ The minor version will be incremented upon a breaking change and the patch versi ## [Unreleased] +### Features + +- cli: Add `env` option to verifiable builds ([#2325](https://github.com/coral-xyz/anchor/pull/2325)). + ## [0.26.0] - 2022-12-15 ### Features diff --git a/cli/src/lib.rs b/cli/src/lib.rs index 249ac9783e..cfe55d6c1e 100644 --- a/cli/src/lib.rs +++ b/cli/src/lib.rs @@ -101,6 +101,9 @@ pub enum Command { /// verifiable builds. Only works for debian-based images. #[clap(value_enum, short, long, default_value = "none")] bootstrap: BootstrapMode, + /// Environment variables to pass into the docker container + #[clap(short, long, required = false)] + env: Vec, /// Arguments to pass to the underlying `cargo build-bpf` command #[clap(required = false, last = true)] cargo_args: Vec, @@ -141,6 +144,9 @@ pub enum Command { /// verifiable builds. Only works for debian-based images. #[clap(value_enum, short, long, default_value = "none")] bootstrap: BootstrapMode, + /// Environment variables to pass into the docker container + #[clap(short, long, required = false)] + env: Vec, /// Arguments to pass to the underlying `cargo build-bpf` command. #[clap(required = false, last = true)] cargo_args: Vec, @@ -172,6 +178,9 @@ pub enum Command { #[clap(long)] run: Vec, args: Vec, + /// Environment variables to pass into the docker container + #[clap(short, long, required = false)] + env: Vec, /// Arguments to pass to the underlying `cargo build-bpf` command. #[clap(required = false, last = true)] cargo_args: Vec, @@ -237,6 +246,9 @@ pub enum Command { Publish { /// The name of the program to publish. program: String, + /// Environment variables to pass into the docker container + #[clap(short, long, required = false)] + env: Vec, /// Arguments to pass to the underlying `cargo build-bpf` command. #[clap(required = false, last = true)] cargo_args: Vec, @@ -264,6 +276,9 @@ pub enum Command { /// no "CHECK" comments where normally required #[clap(long)] skip_lint: bool, + /// Environment variables to pass into the docker container + #[clap(short, long, required = false)] + env: Vec, /// Arguments to pass to the underlying `cargo build-bpf` command. #[clap(required = false, last = true)] cargo_args: Vec, @@ -387,6 +402,7 @@ pub fn entry(opts: Opts) -> Result<()> { docker_image, bootstrap, cargo_args, + env, skip_lint, no_docs, } => build( @@ -401,6 +417,7 @@ pub fn entry(opts: Opts) -> Result<()> { bootstrap, None, None, + env, cargo_args, no_docs, ), @@ -410,6 +427,7 @@ pub fn entry(opts: Opts) -> Result<()> { solana_version, docker_image, bootstrap, + env, cargo_args, } => verify( &opts.cfg_override, @@ -418,6 +436,7 @@ pub fn entry(opts: Opts) -> Result<()> { solana_version, docker_image, bootstrap, + env, cargo_args, ), Command::Clean => clean(&opts.cfg_override), @@ -442,6 +461,7 @@ pub fn entry(opts: Opts) -> Result<()> { detach, run, args, + env, cargo_args, skip_lint, } => test( @@ -453,6 +473,7 @@ pub fn entry(opts: Opts) -> Result<()> { detach, run, args, + env, cargo_args, ), #[cfg(feature = "dev")] @@ -466,20 +487,23 @@ pub fn entry(opts: Opts) -> Result<()> { Command::Login { token } => login(&opts.cfg_override, token), Command::Publish { program, + env, cargo_args, skip_build, - } => publish(&opts.cfg_override, program, cargo_args, skip_build), + } => publish(&opts.cfg_override, program, env, cargo_args, skip_build), Command::Keys { subcmd } => keys(&opts.cfg_override, subcmd), Command::Localnet { skip_build, skip_deploy, skip_lint, + env, cargo_args, } => localnet( &opts.cfg_override, skip_build, skip_deploy, skip_lint, + env, cargo_args, ), Command::Account { @@ -790,6 +814,7 @@ pub fn build( bootstrap: BootstrapMode, stdout: Option, // Used for the package registry server. stderr: Option, // Used for the package registry server. + env_vars: Vec, cargo_args: Vec, no_docs: bool, ) -> Result<()> { @@ -835,6 +860,7 @@ pub fn build( &build_config, stdout, stderr, + env_vars, cargo_args, skip_lint, no_docs, @@ -848,6 +874,7 @@ pub fn build( &build_config, stdout, stderr, + env_vars, cargo_args, skip_lint, no_docs, @@ -861,6 +888,7 @@ pub fn build( &build_config, stdout, stderr, + env_vars, cargo_args, skip_lint, no_docs, @@ -881,6 +909,7 @@ fn build_all( build_config: &BuildConfig, stdout: Option, // Used for the package registry server. stderr: Option, // Used for the package registry server. + env_vars: Vec, cargo_args: Vec, skip_lint: bool, no_docs: bool, @@ -898,6 +927,7 @@ fn build_all( build_config, stdout.as_ref().map(|f| f.try_clone()).transpose()?, stderr.as_ref().map(|f| f.try_clone()).transpose()?, + env_vars.clone(), cargo_args.clone(), skip_lint, no_docs, @@ -920,6 +950,7 @@ fn build_cwd( build_config: &BuildConfig, stdout: Option, stderr: Option, + env_vars: Vec, cargo_args: Vec, skip_lint: bool, no_docs: bool, @@ -937,6 +968,7 @@ fn build_cwd( stdout, stderr, skip_lint, + env_vars, cargo_args, no_docs, ), @@ -953,6 +985,7 @@ fn build_cwd_verifiable( stdout: Option, stderr: Option, skip_lint: bool, + env_vars: Vec, cargo_args: Vec, no_docs: bool, ) -> Result<()> { @@ -975,6 +1008,7 @@ fn build_cwd_verifiable( build_config, stdout, stderr, + env_vars, cargo_args, ); @@ -1014,6 +1048,7 @@ fn build_cwd_verifiable( result } +#[allow(clippy::too_many_arguments)] fn docker_build( cfg: &WithPath, container_name: &str, @@ -1021,6 +1056,7 @@ fn docker_build( build_config: &BuildConfig, stdout: Option, stderr: Option, + env_vars: Vec, cargo_args: Vec, ) -> Result<()> { let binary_name = Manifest::from_path(&cargo_toml)?.lib_name()?; @@ -1074,6 +1110,7 @@ fn docker_build( binary_name, stdout, stderr, + env_vars, cargo_args, ) }); @@ -1137,6 +1174,7 @@ fn docker_build_bpf( binary_name: String, stdout: Option, stderr: Option, + env_vars: Vec, cargo_args: Vec, ) -> Result<()> { let manifest_path = @@ -1154,6 +1192,13 @@ fn docker_build_bpf( "exec", "--env", "PATH=/root/.local/share/solana/install/active_release/bin:/root/.cargo/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin", + ]) + .args(env_vars + .iter() + .map(|x| ["--env", x.as_str()]) + .collect::>() + .concat()) + .args([ container_name, "cargo", "build-bpf", @@ -1292,6 +1337,7 @@ fn _build_cwd( Ok(()) } +#[allow(clippy::too_many_arguments)] fn verify( cfg_override: &ConfigOverride, program_id: Pubkey, @@ -1299,6 +1345,7 @@ fn verify( solana_version: Option, docker_image: Option, bootstrap: BootstrapMode, + env_vars: Vec, cargo_args: Vec, ) -> Result<()> { // Change to the workspace member directory, if needed. @@ -1324,6 +1371,7 @@ fn verify( bootstrap, // bootstrap docker image None, // stdout None, // stderr + env_vars, cargo_args, false, )?; @@ -2116,6 +2164,7 @@ fn test( detach: bool, tests_to_run: Vec, extra_args: Vec, + env_vars: Vec, cargo_args: Vec, ) -> Result<()> { let test_paths = tests_to_run @@ -2142,6 +2191,7 @@ fn test( BootstrapMode::None, None, None, + env_vars, cargo_args, false, )?; @@ -3120,6 +3170,7 @@ fn login(_cfg_override: &ConfigOverride, token: String) -> Result<()> { fn publish( cfg_override: &ConfigOverride, program_name: String, + env_vars: Vec, cargo_args: Vec, skip_build: bool, ) -> Result<()> { @@ -3250,6 +3301,7 @@ fn publish( BootstrapMode::None, None, None, + env_vars, cargo_args, true, )?; @@ -3332,6 +3384,7 @@ fn localnet( skip_build: bool, skip_deploy: bool, skip_lint: bool, + env_vars: Vec, cargo_args: Vec, ) -> Result<()> { with_workspace(cfg_override, |cfg| { @@ -3349,6 +3402,7 @@ fn localnet( BootstrapMode::None, None, None, + env_vars, cargo_args, false, )?;