Skip to content

Allow passing env vars to verifiable build container #2325

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

Merged
merged 7 commits into from
Dec 21, 2022
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
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
56 changes: 55 additions & 1 deletion cli/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<String>,
/// Arguments to pass to the underlying `cargo build-bpf` command
#[clap(required = false, last = true)]
cargo_args: Vec<String>,
Expand Down Expand Up @@ -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<String>,
/// Arguments to pass to the underlying `cargo build-bpf` command.
#[clap(required = false, last = true)]
cargo_args: Vec<String>,
Expand Down Expand Up @@ -172,6 +178,9 @@ pub enum Command {
#[clap(long)]
run: Vec<String>,
args: Vec<String>,
/// Environment variables to pass into the docker container
#[clap(short, long, required = false)]
env: Vec<String>,
/// Arguments to pass to the underlying `cargo build-bpf` command.
#[clap(required = false, last = true)]
cargo_args: Vec<String>,
Expand Down Expand Up @@ -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<String>,
/// Arguments to pass to the underlying `cargo build-bpf` command.
#[clap(required = false, last = true)]
cargo_args: Vec<String>,
Expand Down Expand Up @@ -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<String>,
/// Arguments to pass to the underlying `cargo build-bpf` command.
#[clap(required = false, last = true)]
cargo_args: Vec<String>,
Expand Down Expand Up @@ -387,6 +402,7 @@ pub fn entry(opts: Opts) -> Result<()> {
docker_image,
bootstrap,
cargo_args,
env,
skip_lint,
no_docs,
} => build(
Expand All @@ -401,6 +417,7 @@ pub fn entry(opts: Opts) -> Result<()> {
bootstrap,
None,
None,
env,
cargo_args,
no_docs,
),
Expand All @@ -410,6 +427,7 @@ pub fn entry(opts: Opts) -> Result<()> {
solana_version,
docker_image,
bootstrap,
env,
cargo_args,
} => verify(
&opts.cfg_override,
Expand All @@ -418,6 +436,7 @@ pub fn entry(opts: Opts) -> Result<()> {
solana_version,
docker_image,
bootstrap,
env,
cargo_args,
),
Command::Clean => clean(&opts.cfg_override),
Expand All @@ -442,6 +461,7 @@ pub fn entry(opts: Opts) -> Result<()> {
detach,
run,
args,
env,
cargo_args,
skip_lint,
} => test(
Expand All @@ -453,6 +473,7 @@ pub fn entry(opts: Opts) -> Result<()> {
detach,
run,
args,
env,
cargo_args,
),
#[cfg(feature = "dev")]
Expand All @@ -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 {
Expand Down Expand Up @@ -790,6 +814,7 @@ pub fn build(
bootstrap: BootstrapMode,
stdout: Option<File>, // Used for the package registry server.
stderr: Option<File>, // Used for the package registry server.
env_vars: Vec<String>,
cargo_args: Vec<String>,
no_docs: bool,
) -> Result<()> {
Expand Down Expand Up @@ -835,6 +860,7 @@ pub fn build(
&build_config,
stdout,
stderr,
env_vars,
cargo_args,
skip_lint,
no_docs,
Expand All @@ -848,6 +874,7 @@ pub fn build(
&build_config,
stdout,
stderr,
env_vars,
cargo_args,
skip_lint,
no_docs,
Expand All @@ -861,6 +888,7 @@ pub fn build(
&build_config,
stdout,
stderr,
env_vars,
cargo_args,
skip_lint,
no_docs,
Expand All @@ -881,6 +909,7 @@ fn build_all(
build_config: &BuildConfig,
stdout: Option<File>, // Used for the package registry server.
stderr: Option<File>, // Used for the package registry server.
env_vars: Vec<String>,
cargo_args: Vec<String>,
skip_lint: bool,
no_docs: bool,
Expand All @@ -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,
Expand All @@ -920,6 +950,7 @@ fn build_cwd(
build_config: &BuildConfig,
stdout: Option<File>,
stderr: Option<File>,
env_vars: Vec<String>,
cargo_args: Vec<String>,
skip_lint: bool,
no_docs: bool,
Expand All @@ -937,6 +968,7 @@ fn build_cwd(
stdout,
stderr,
skip_lint,
env_vars,
cargo_args,
no_docs,
),
Expand All @@ -953,6 +985,7 @@ fn build_cwd_verifiable(
stdout: Option<File>,
stderr: Option<File>,
skip_lint: bool,
env_vars: Vec<String>,
cargo_args: Vec<String>,
no_docs: bool,
) -> Result<()> {
Expand All @@ -975,6 +1008,7 @@ fn build_cwd_verifiable(
build_config,
stdout,
stderr,
env_vars,
cargo_args,
);

Expand Down Expand Up @@ -1014,13 +1048,15 @@ fn build_cwd_verifiable(
result
}

#[allow(clippy::too_many_arguments)]
fn docker_build(
cfg: &WithPath<Config>,
container_name: &str,
cargo_toml: PathBuf,
build_config: &BuildConfig,
stdout: Option<File>,
stderr: Option<File>,
env_vars: Vec<String>,
cargo_args: Vec<String>,
) -> Result<()> {
let binary_name = Manifest::from_path(&cargo_toml)?.lib_name()?;
Expand Down Expand Up @@ -1074,6 +1110,7 @@ fn docker_build(
binary_name,
stdout,
stderr,
env_vars,
cargo_args,
)
});
Expand Down Expand Up @@ -1137,6 +1174,7 @@ fn docker_build_bpf(
binary_name: String,
stdout: Option<File>,
stderr: Option<File>,
env_vars: Vec<String>,
cargo_args: Vec<String>,
) -> Result<()> {
let manifest_path =
Expand All @@ -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::<Vec<[&str; 2]>>()
.concat())
.args([
container_name,
"cargo",
"build-bpf",
Expand Down Expand Up @@ -1292,13 +1337,15 @@ fn _build_cwd(
Ok(())
}

#[allow(clippy::too_many_arguments)]
fn verify(
cfg_override: &ConfigOverride,
program_id: Pubkey,
program_name: Option<String>,
solana_version: Option<String>,
docker_image: Option<String>,
bootstrap: BootstrapMode,
env_vars: Vec<String>,
cargo_args: Vec<String>,
) -> Result<()> {
// Change to the workspace member directory, if needed.
Expand All @@ -1324,6 +1371,7 @@ fn verify(
bootstrap, // bootstrap docker image
None, // stdout
None, // stderr
env_vars,
cargo_args,
false,
)?;
Expand Down Expand Up @@ -2116,6 +2164,7 @@ fn test(
detach: bool,
tests_to_run: Vec<String>,
extra_args: Vec<String>,
env_vars: Vec<String>,
cargo_args: Vec<String>,
) -> Result<()> {
let test_paths = tests_to_run
Expand All @@ -2142,6 +2191,7 @@ fn test(
BootstrapMode::None,
None,
None,
env_vars,
cargo_args,
false,
)?;
Expand Down Expand Up @@ -3120,6 +3170,7 @@ fn login(_cfg_override: &ConfigOverride, token: String) -> Result<()> {
fn publish(
cfg_override: &ConfigOverride,
program_name: String,
env_vars: Vec<String>,
cargo_args: Vec<String>,
skip_build: bool,
) -> Result<()> {
Expand Down Expand Up @@ -3250,6 +3301,7 @@ fn publish(
BootstrapMode::None,
None,
None,
env_vars,
cargo_args,
true,
)?;
Expand Down Expand Up @@ -3332,6 +3384,7 @@ fn localnet(
skip_build: bool,
skip_deploy: bool,
skip_lint: bool,
env_vars: Vec<String>,
cargo_args: Vec<String>,
) -> Result<()> {
with_workspace(cfg_override, |cfg| {
Expand All @@ -3349,6 +3402,7 @@ fn localnet(
BootstrapMode::None,
None,
None,
env_vars,
cargo_args,
false,
)?;
Expand Down