Skip to content

Commit

Permalink
feat: env flag for external parameters of docker build and regular …
Browse files Browse the repository at this point in the history
…build (#226)

tested docker build cases:

1. no command in manifest via `--env` cli flags
[sample_no_workspace](https://github.com/dj8yfo/sample_no_workspace/blob/7a9793727ddd6f8be5482944130a9235f62aa1e9/Cargo.toml)
=>


<details>
  <summary>sample-crate-88.testnet</summary><p>
  
```json

{
  "build_info": {
    "build_command": [
      "cargo",
      "near",
      "build",
      "--env",
      "GOOGLE_QUERY=https://www.google.com/search?q=google+translate&sca_esv=3c150c50f502bc5d",
      "--env",
      "KEY=VALUE"
    ],
    "build_environment": "dj8yfo/sourcescan:0.x.x-dev-pr-226@sha256:a33c44bc06ae8411458fb23cf57b6d760f51592481276f66f5bc292ed6b80378",
    "contract_path": "",
    "source_code_snapshot": "git+https://github.com/dj8yfo/sample_no_workspace?rev=7a9793727ddd6f8be5482944130a9235f62aa1e9"
  },
  "link": "https://github.com/dj8yfo/sample_no_workspace/tree/7a9793727ddd6f8be5482944130a9235f62aa1e9",
  "standards": [
    {
      "standard": "nep330",
      "version": "1.2.0"
    }
  ],
  "version": "0.7.7"
}
```

</p></details>

2. via `passed_env` in manifest and exported env
[sample_no_workspace](https://github.com/dj8yfo/sample_no_workspace/blob/485c69441acc449078b1157b812bb69b744c870d/Cargo.toml)

<details>
  <summary>sample-crate-90.testnet</summary><p>
  
```json

{
  "build_info": {
    "build_command": [
      "cargo",
      "near",
      "build",
      "--env",
      "KEY=VALUE",
      "--env",
      "GOOGLE_QUERY=https://www.google.com/search?q=google+translate&sca_esv=3c150c50f502bc5d"
    ],
    "build_environment": "dj8yfo/sourcescan:0.x.x-dev-pr-226@sha256:a33c44bc06ae8411458fb23cf57b6d760f51592481276f66f5bc292ed6b80378",
    "contract_path": "",
    "source_code_snapshot": "git+https://github.com/dj8yfo/sample_no_workspace?rev=485c69441acc449078b1157b812bb69b744c870d"
  },
  "link": "https://github.com/dj8yfo/sample_no_workspace/tree/485c69441acc449078b1157b812bb69b744c870d",
  "standards": [
    {
      "standard": "nep330",
      "version": "1.2.0"
    }
  ],
  "version": "0.7.7"
}
```

</p></details>

3. no command in manifest via `--env` cli flags
[neardevhub-contract](https://github.com/dj8yfo/neardevhub-contract/blob/bba6a9b63c00dcf5399e1eb2bf8bf18d6d8d2c4e/discussions/Cargo.toml)


<details>

<summary>discussions.gothamuniquehandle3.community.devhub-test-4.testnet</summary><p>
  
```json
{
  "build_info": {
    "build_command": [
      "cargo",
      "near",
      "build",
      "--env",
      "KEY=VALUE",
      "--env",
      "GOOGLE_QUERY=https://www.google.com/search?q=google+translate&sca_esv=3c150c50f502bc5d"
    ],
    "build_environment": "dj8yfo/sourcescan:0.x.x-dev-pr-226@sha256:a33c44bc06ae8411458fb23cf57b6d760f51592481276f66f5bc292ed6b80378",
    "contract_path": "discussions",
    "source_code_snapshot": "git+https://github.com/dj8yfo/neardevhub-contract?rev=bba6a9b63c00dcf5399e1eb2bf8bf18d6d8d2c4e"
  },
  "link": "https://github.com/dj8yfo/neardevhub-contract/tree/bba6a9b63c00dcf5399e1eb2bf8bf18d6d8d2c4e",
  "standards": [
    {
      "standard": "nep330",
      "version": "1.2.0"
    }
  ],
  "version": "0.1.0"
}
```
</p></details>

```bash
d673dee7ff8e9e5534796a67154e7d43db27b6e57934c33e2848d6b1b5ebf892  community.devhub-test-4.testnet.wasm
e6ddea282b05a29aae383e46ba7fc26c127caa4de18a8283190a3d86256e70ac  devhub-test-4.testnet.wasm
836a14c755f567a331f9e4ad9f3e99fe696c992525df7c55f8acb18973218c6d  discussions.gothamuniquehandle3.community.devhub-test-4.testnet.wasm
85b3054c02e74e8dbf5eab526e8ee867dce1cc3768aa11b32b4d4110fdc78757  gothamuniquehandle3.community.devhub-test-4.testnet.wasm
85b3054c02e74e8dbf5eab526e8ee867dce1cc3768aa11b32b4d4110fdc78757  standalone-devhub-community-4.testnet.wasm
836a14c755f567a331f9e4ad9f3e99fe696c992525df7c55f8acb18973218c6d  standalone-devhub-discussions-4.testnet.wasm
```

4. via `passed_env` in manifest and exported env ; following cases are
deployed from the same source-code
[neardevhub](https://github.com/dj8yfo/neardevhub-contract/blob/d05268b3583b8273540d94871f897efa120eb0a5/discussions/Cargo.toml)
  - no variables exports

    <details>

<summary>discussions.uniquehandle.community.devhub-no-vars.testnet</summary><p>
  
    ```json
    {
      "build_info": {
        "build_command": [
          "cargo",
          "near",
          "build"
        ],
"build_environment":
"dj8yfo/sourcescan:0.x.x-dev-pr-226@sha256:a33c44bc06ae8411458fb23cf57b6d760f51592481276f66f5bc292ed6b80378",
        "contract_path": "discussions",
"source_code_snapshot":
"git+https://github.com/dj8yfo/neardevhub-contract?rev=d05268b3583b8273540d94871f897efa120eb0a5"
      },
"link":
"https://github.com/dj8yfo/neardevhub-contract/tree/d05268b3583b8273540d94871f897efa120eb0a5",
      "standards": [
        {
          "standard": "nep330",
          "version": "1.2.0"
        }
      ],
      "version": "0.1.0"
    }
    ```
    </p></details>

    ```bash
e8fbe501978ac5458a71913ff2fe7ebc1a72573b615ca3fa346f32677d7ca7fc
community.devhub-no-vars.testnet.wasm
df5992c0f3df6c28b3fa439fdb965cd8ecb8fdec37d65f632affc8de901e0092
devhub-no-vars.testnet.wasm
4382ba6c1e1e004e8b69d98efc0e7652c2c4cf3c16ae9e2dd4d441e85719523f
discussions.uniquehandle.community.devhub-no-vars.testnet.wasm
175ae0c5719a21dfab545d418050f91807c858e68983434f4f60ec75cf99f92c
standalone-devhub-community-no-vars.testnet.wasm
4382ba6c1e1e004e8b69d98efc0e7652c2c4cf3c16ae9e2dd4d441e85719523f
standalone-devhub-discussions-no-vars.testnet.wasm
175ae0c5719a21dfab545d418050f91807c858e68983434f4f60ec75cf99f92c
uniquehandle.community.devhub-no-vars.testnet.wasm
    ```
  - one variable export 

    <details>

<summary>discussions.uniquehandle.community.devhub-one-var.testnet</summary><p>
  
    ```json
    {
      "build_info": {
        "build_command": [
          "cargo",
          "near",
          "build",
          "--env",

"GOOGLE_QUERY=https://www.google.com/search?q=google+translate&sca_esv=3c150c50f502bc5d"
        ],
"build_environment":
"dj8yfo/sourcescan:0.x.x-dev-pr-226@sha256:a33c44bc06ae8411458fb23cf57b6d760f51592481276f66f5bc292ed6b80378",
        "contract_path": "discussions",
"source_code_snapshot":
"git+https://github.com/dj8yfo/neardevhub-contract?rev=d05268b3583b8273540d94871f897efa120eb0a5"
      },
"link":
"https://github.com/dj8yfo/neardevhub-contract/tree/d05268b3583b8273540d94871f897efa120eb0a5",
      "standards": [
        {
          "standard": "nep330",
          "version": "1.2.0"
        }
      ],
      "version": "0.1.0"
    }
    ```
    </p></details>

    ```bash
1787219b148d4298ed56cad04ae430c40d92779c6163e6165b88e5207549f41f
community.devhub-one-var.testnet.wasm
87ecd58d10df0718f599af334601674ffe11f203134358add7b993441a943e19
devhub-one-var.testnet.wasm
18fc865aaa2990d80175033e93ecd471f954bf269b340d700885c427476d6e9c
discussions.uniquehandle.community.devhub-one-var.testnet.wasm
ad3b3a4a26618f32c1fde9958dbd4209b7364ff49e20ab3fc515cff6fcb31ca5
standalone-devhub-community-one-var.testnet.wasm
18fc865aaa2990d80175033e93ecd471f954bf269b340d700885c427476d6e9c
standalone-devhub-discussions-one-var.testnet.wasm
ad3b3a4a26618f32c1fde9958dbd4209b7364ff49e20ab3fc515cff6fcb31ca5
uniquehandle.community.devhub-one-var.testnet.wasm
    ```

  - two variables export 

    <details>

<summary>discussions.uniquehandle.community.devhub-two-var.testnet</summary><p>
  
    ```json
    {
      "build_info": {
        "build_command": [
          "cargo",
          "near",
          "build",
          "--env",
          "KEY=VALUE",
          "--env",

"GOOGLE_QUERY=https://www.google.com/search?q=google+translate&sca_esv=3c150c50f502bc5d"
        ],
"build_environment":
"dj8yfo/sourcescan:0.x.x-dev-pr-226@sha256:a33c44bc06ae8411458fb23cf57b6d760f51592481276f66f5bc292ed6b80378",
        "contract_path": "discussions",
"source_code_snapshot":
"git+https://github.com/dj8yfo/neardevhub-contract?rev=d05268b3583b8273540d94871f897efa120eb0a5"
      },
"link":
"https://github.com/dj8yfo/neardevhub-contract/tree/d05268b3583b8273540d94871f897efa120eb0a5",
      "standards": [
        {
          "standard": "nep330",
          "version": "1.2.0"
        }
      ],
      "version": "0.1.0"
    }
    ```
    </p></details>

    ```bash
cda103ad337d322341f2d8ce442c7cac2b57f945f7c37de814c3951f499967f5
community.devhub-two-var.testnet.wasm
79dc0629f9dde96eb16a823110afd9bcfa6b0e5d7f3b1256fbd3f5da887211b8
devhub-two-var.testnet.wasm
549d312421e600d583fb9847ddc8142909d6f6c5a75a3a44ef9a1e2198b08443
discussions.uniquehandle.community.devhub-two-var.testnet.wasm
804550e43cbfe5f463a8b866a4a5eb65ef845cb9b625af93f43a91ec96dd95a3
standalone-devhub-community-two-var.testnet.wasm
549d312421e600d583fb9847ddc8142909d6f6c5a75a3a44ef9a1e2198b08443
standalone-devhub-discussions-two-var.testnet.wasm
804550e43cbfe5f463a8b866a4a5eb65ef845cb9b625af93f43a91ec96dd95a3
uniquehandle.community.devhub-two-var.testnet.wasm
    ```

---------

Co-authored-by: dj8yf0μl <[email protected]>
  • Loading branch information
dj8yfo and dj8yf0μl authored Sep 24, 2024
1 parent f05c58f commit b0e2996
Show file tree
Hide file tree
Showing 12 changed files with 362 additions and 126 deletions.
64 changes: 32 additions & 32 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion cargo-near-build/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ home = { version = "0.5.9", optional = true }
pathdiff = { version = "0.2.1", features = ["camino"], optional = true }
unix_path = { version = "1.0.1", optional = true }
tempfile = { version = "3.10.1", optional = true }
shell-words = { version = "1.0.0", optional = true}

[target.'cfg(target_os = "linux")'.dependencies]
nix = { version = "0.29.0", features = ["user", "process"], optional = true }
Expand All @@ -48,6 +49,6 @@ abi_build = []
docker = [
"dep:url", "dep:serde", "dep:git2",
"dep:home", "dep:pathdiff", "dep:unix_path",
"dep:tempfile", "dep:nix"
"dep:tempfile", "dep:nix", "dep:shell-words"
]

2 changes: 1 addition & 1 deletion cargo-near-build/src/cargo_native/compile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ where
ColorPreference::Never => cmd.args(["--color", "never"]),
};

tracing::info!("Invoking cargo: {:?}", cmd);
tracing::info!("Invoking cargo: {:#?}", cmd);

let mut child = cmd
// capture the stdout to return from this function as bytes
Expand Down
15 changes: 10 additions & 5 deletions cargo-near-build/src/near/abi/generate/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ pub fn procedure(
generate_docs: bool,
hide_warnings: bool,
cargo_feature_args: &[&str],
env: &[(&str, &str)],
color: ColorPreference,
) -> eyre::Result<near_abi::AbiRoot> {
let root_node = crate_metadata
Expand Down Expand Up @@ -68,15 +69,19 @@ pub fn procedure(

pretty_print::step("Generating ABI");

let dylib_artifact = cargo_native::compile::run::<Dylib>(
&crate_metadata.manifest_path,
cargo_args.as_slice(),
vec![
let compile_env = {
let compile_env = vec![
("CARGO_PROFILE_DEV_OPT_LEVEL", "0"),
("CARGO_PROFILE_DEV_DEBUG", "0"),
("CARGO_PROFILE_DEV_LTO", "off"),
(env_keys::BUILD_RS_ABI_STEP_HINT, "true"),
],
];
[&compile_env, env].concat()
};
let dylib_artifact = cargo_native::compile::run::<Dylib>(
&crate_metadata.manifest_path,
cargo_args.as_slice(),
compile_env,
hide_warnings,
color,
)?;
Expand Down
1 change: 1 addition & 0 deletions cargo-near-build/src/near/abi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub fn build(args: abi_types::Opts) -> eyre::Result<()> {
!args.no_doc,
false,
&[],
&[],
color,
)?;
let abi_types::Result { path } = write_to_file(
Expand Down
29 changes: 21 additions & 8 deletions cargo-near-build/src/near/build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,11 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
let out_dir = crate_metadata.resolve_output_dir(args.out_dir.map(Into::into))?;

let mut build_env = vec![("RUSTFLAGS", "-C link-arg=-s")];
build_env.extend(
args.env
.iter()
.map(|(key, value)| (key.as_ref(), value.as_ref())),
);
let mut cargo_args = vec!["--target", COMPILATION_TARGET];
let cargo_feature_args = {
let mut feat_args = vec![];
Expand All @@ -76,14 +81,22 @@ pub fn run(args: Opts) -> eyre::Result<CompilationArtifact> {
let (builder_version, builder_version_mismatch) =
VersionMismatch::get_coerced_builder_version()?;
if !args.no_abi {
let mut contract_abi = abi::generate::procedure(
&crate_metadata,
args.no_locked,
!args.no_doc,
true,
&cargo_feature_args,
color.clone(),
)?;
let mut contract_abi = {
let env = args
.env
.iter()
.map(|(key, value)| (key.as_ref(), value.as_ref()))
.collect::<Vec<_>>();
abi::generate::procedure(
&crate_metadata,
args.no_locked,
!args.no_doc,
true,
&cargo_feature_args,
&env,
color.clone(),
)?
};

let embedding_binary = args.cli_description.cli_name_abi;
contract_abi.metadata.build = Some(BuildInfo {
Expand Down
21 changes: 16 additions & 5 deletions cargo-near-build/src/near/docker_build/subprocess_step.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,20 @@ pub fn run(

let env = env_vars::EnvVars::new(&docker_build_meta, cloned_repo)?;
let env_args = env.docker_args();
let cargo_cmd = opts
.get_cli_build_command_in_docker(docker_build_meta.container_build_command.clone())?;
println!("{} {}", "build command in container:".green(), cargo_cmd);
let shell_escaped_cargo_cmd = {
let cargo_cmd = opts.get_cli_build_command_in_docker(
docker_build_meta.container_build_command.clone(),
docker_build_meta.passed_env.clone(),
)?;
tracing::debug!("cli_build_command_in_docker {:#?}", cargo_cmd);
shell_words::join(cargo_cmd)
};
println!(
"{} {}",
"build command in container:".green(),
shell_escaped_cargo_cmd
);
println!();

let docker_args = {
let mut docker_args = vec![
Expand All @@ -74,8 +85,8 @@ pub fn run(

docker_args.extend(vec![&docker_image, "/bin/bash", "-c"]);

docker_args.push(&cargo_cmd);
tracing::debug!("docker command : {:?}", docker_args);
docker_args.push(&shell_escaped_cargo_cmd);
tracing::debug!("docker command : {:#?}", docker_args);
docker_args
};

Expand Down
Loading

0 comments on commit b0e2996

Please sign in to comment.