Skip to content

Commit

Permalink
feat: Fixed GitHub CI staging workflow generated by cargo near new
Browse files Browse the repository at this point in the history
…command to work correctly with docker case (#193)
  • Loading branch information
dj8yfo authored Sep 3, 2024
1 parent 17e3dd3 commit a805427
Show file tree
Hide file tree
Showing 14 changed files with 84 additions and 25 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,8 @@ cargo near new

Initializes a new project skeleton to create a contract from a template.

[Example](./docs/workflows.md) of github [workflows](./cargo-near/src/commands/new/new-project-template/.github/workflows) configuration, created by `cargo near new`.

---

```console
Expand Down
2 changes: 1 addition & 1 deletion cargo-near-build/src/near/docker_build/git_checks/dirty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ pub fn check_then_handle(
) -> eyre::Result<()> {
let result = check(repo_root);
match (result, context) {
(Err(err), BuildContext::Deploy) => {
(Err(err), BuildContext::Deploy { .. }) => {
println!(
"{}",
"Either commit and push, or revert following changes to continue deployment:"
Expand Down
34 changes: 23 additions & 11 deletions cargo-near-build/src/near/docker_build/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,29 @@ pub fn run(docker_opts: DockerBuildOpts) -> eyre::Result<CompilationArtifact> {
metadata::ReproducibleBuild::parse(cloned_repo.crate_metadata())
})?;

if let BuildContext::Deploy = docker_opts.context {
pretty_print::handle_step(
"Performing check that current HEAD has been pushed to remote...",
|| {
git_checks::pushed_to_remote::check(
// this unwrap depends on `metadata::ReproducibleBuild::validate` logic
&docker_build_meta.repository.clone().unwrap(),
crate_in_repo.head,
)
},
)?;
if let BuildContext::Deploy {
skip_git_remote_check,
} = docker_opts.context
{
if !skip_git_remote_check {
pretty_print::handle_step(
"Performing check that current HEAD has been pushed to remote...",
|| {
git_checks::pushed_to_remote::check(
// this unwrap depends on `metadata::ReproducibleBuild::validate` logic
&docker_build_meta.repository.clone().unwrap(),
crate_in_repo.head,
)
},
)?;
} else {
pretty_print::handle_step(
"Check that current HEAD has been pushed to remote was configured out by `--skip-git-remote-check` flag",
|| {
Ok(())
},
)?;
}
}
if std::env::var(env_keys::nep330::nonspec::SERVER_DISABLE_INTERACTIVE).is_err() {
pretty_print::handle_step("Performing `docker` sanity check...", || {
Expand Down
2 changes: 1 addition & 1 deletion cargo-near-build/src/types/near/build/input/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ mod docker_context;
#[derive(Debug, Clone, Copy)]
pub enum BuildContext {
Build,
Deploy,
Deploy { skip_git_remote_check: bool },
}

/// argument of [build](crate::build) function
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl ClonedRepo {
println!("{}", WARN_BECOMES_ERR.red(),);
std::thread::sleep(Duration::new(5, 0));
}
(true, BuildContext::Deploy) => {
(true, BuildContext::Deploy { .. }) => {
println!(
"{}",
"Check in Cargo.lock for contract being built into source control.".yellow()
Expand Down
3 changes: 1 addition & 2 deletions cargo-near-build/src/types/source_id/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,8 +80,7 @@ impl std::fmt::Display for Precise {
/// Information to find a specific commit in a Git repository.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum GitReference {
/// From a specific revision. Can be a commit hash (either short or full),
/// or a named reference like `refs/pull/493/head`.
/// From a specific revision. Can be a commit hash (only full form)
Rev(String),
}

Expand Down
8 changes: 8 additions & 0 deletions cargo-near/src/commands/build_command/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ pub struct BuildCommand {
impl BuildCommand {
pub fn run(self, context: BuildContext) -> color_eyre::eyre::Result<BuildArtifact> {
if self.no_docker() {
if let BuildContext::Deploy {
skip_git_remote_check: true,
} = context
{
return Err(color_eyre::eyre::eyre!(
"`--skip-git-remote-check` flag is only applicable for docker builds"
));
}
cargo_near_build::build(self.into())
} else {
cargo_near_build::docker::build(cargo_near_build::docker::DockerBuildOpts {
Expand Down
10 changes: 9 additions & 1 deletion cargo-near/src/commands/deploy/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@ pub struct Contract {
#[interactive_clap(flatten)]
/// Specify a build command args:
build_command_args: build_command::BuildCommand,
/// whether to check that code has been pushed to repository during docker build
#[interactive_clap(long)]
skip_git_remote_check: bool,
#[interactive_clap(skip_default_input_arg)]
/// What is the contract account ID?
contract_account_id: near_cli_rs::types::account_id::AccountId,
Expand All @@ -28,7 +31,9 @@ impl ContractContext {
let file_path = scope
.build_command_args
.clone()
.run(cargo_near_build::BuildContext::Deploy)?
.run(cargo_near_build::BuildContext::Deploy {
skip_git_remote_check: scope.skip_git_remote_check,
})?
.path;

Ok(Self(
Expand Down Expand Up @@ -82,9 +87,12 @@ impl interactive_clap::FromCli for Contract {
.clone()
.expect("Unexpected error");

let skip_git_remote_check = clap_variant.skip_git_remote_check;

let new_context_scope = InteractiveClapContextScopeForContract {
build_command_args,
contract_account_id,
skip_git_remote_check,
};

let output_context =
Expand Down
6 changes: 5 additions & 1 deletion cargo-near/src/commands/new/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,11 @@ impl NewContext {
&new_file_path,
new_project_file
.content
.replace("cargo-near-new-project-name", project_name),
.replace("cargo-near-new-project-name", project_name)
.replace(
"cargo-near-new-ci-tool-version-self",
env!("CARGO_PKG_VERSION"),
),
)
.wrap_err_with(|| format!("Failed to write to file: {}", new_file_path.display()))?;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Checkout repository
uses: actions/checkout@v4
- name: Install cargo-near CLI
run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-v0.6.2/cargo-near-installer.sh | sh
run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-vcargo-near-new-ci-tool-version-self/cargo-near-installer.sh | sh
- name: Deploy to production
run: |
cargo near deploy "${{ vars.NEAR_CONTRACT_PRODUCTION_ACCOUNT_ID }}" \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,14 @@ jobs:
send
- name: Install cargo-near CLI
run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-v0.6.2/cargo-near-installer.sh | sh
run: curl --proto '=https' --tlsv1.2 -LsSf https://github.com/near/cargo-near/releases/download/cargo-near-vcargo-near-new-ci-tool-version-self/cargo-near-installer.sh | sh
- name: Deploy to staging
# `--skip-git-remote-check` was used
# as pull request git refs `refs/pull/NUMBER/merge` are somewhat harder to access and live only as long as PRs do
#
# WASM reproducibility check akin to SourceScan won't be available for staging contracts, deployed from PRs
run: |
cargo near deploy "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \
cargo near deploy --skip-git-remote-check "${{ env.NEAR_CONTRACT_PR_STAGING_ACCOUNT_ID }}" \
without-init-call \
network-config "${{ vars.NEAR_CONTRACT_STAGING_NETWORK }}" \
sign-with-plaintext-private-key \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,7 @@ async fn test_contract_is_operational() -> Result<(), Box<dyn std::error::Error>
.await?;
assert!(outcome.is_success());

let user_message_outcome = contract
.view("get_greeting")
.args_json(json!({}))
.await?;
let user_message_outcome = contract.view("get_greeting").args_json(json!({})).await?;
assert_eq!(user_message_outcome.json::<String>()?, "Hello World!");

Ok(())
Expand Down
Binary file added docs/gh_settings_secrets_and_vars.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
25 changes: 25 additions & 0 deletions docs/workflows.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
![gh_settings_secrets_and_vars](./gh_settings_secrets_and_vars.png)

Example production account values:

```bash
# variables
NEAR_CONTRACT_PRODUCTION_ACCOUNT_ID=cargo_near_test_workflows.testnet
NEAR_CONTRACT_PRODUCTION_NETWORK=testnet
NEAR_CONTRACT_PRODUCTION_ACCOUNT_PUBLIC_KEY=ed25519:EvCRvguSjeaTGuzQPQmLg1GqWLqgihBKKcSHT4xtS8K
# secrets
NEAR_CONTRACT_PRODUCTION_ACCOUNT_PRIVATE_KEY=ed25519:4diBhcs9ECg3sDPE97gCFhHbB21BSRheWSzrqt1UVciEoMhpecnFjqapeSfrxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

Example staging account values:

```bash
# variables
NEAR_CONTRACT_STAGING_ACCOUNT_PUBLIC_KEY=ed25519:EvCRvguSjeaTGuzQPQmLg1GqWLqgihBKKcSHT4xtS8K
NEAR_CONTRACT_STAGING_ACCOUNT_ID=cargo_near_test_workflows.testnet
NEAR_CONTRACT_STAGING_NETWORK=testnet
# secrets
NEAR_CONTRACT_STAGING_ACCOUNT_PRIVATE_KEY=ed25519:4diBhcs9ECg3sDPE97gCFhHbB21BSRheWSzrqt1UVciEoMhpecnFjqapeSfrxxxxxxxxxxxxxxxxxxxxxxxxxxxx
```

NOTE: last chars of both private keys examples are redacted/replaced with `'x'` char.

0 comments on commit a805427

Please sign in to comment.