diff --git a/.github/workflows/rust.yml b/.github/workflows/rust.yml index 40a9a8478fd..7e7e5a50fbf 100644 --- a/.github/workflows/rust.yml +++ b/.github/workflows/rust.yml @@ -109,6 +109,8 @@ jobs: RUSTFLAGS: -Cinstrument-coverage - uses: marcopolo/cargo@master name: Setup moon toolchain + env: + MOON_TEST: true with: command: run args: -- --log debug setup diff --git a/Makefile.toml b/Makefile.toml index a59cd3e054e..2df6a6c3032 100644 --- a/Makefile.toml +++ b/Makefile.toml @@ -30,6 +30,7 @@ args = ["clippy", "--workspace", "--all-targets"] [tasks.setup-test] command = "cargo" args = ["run", "--", "--log", "debug", "setup"] +env = { MOON_TEST = "true" } cwd = "./tests/fixtures/cases" [tasks.run-test] diff --git a/crates/action-runner/src/actions/run_target.rs b/crates/action-runner/src/actions/run_target.rs index fe78eba0319..029a5f25a0a 100644 --- a/crates/action-runner/src/actions/run_target.rs +++ b/crates/action-runner/src/actions/run_target.rs @@ -1,3 +1,4 @@ +use crate::ActionRunnerError; use console::Term; use moon_action::{Action, ActionContext, ActionStatus, Attempt}; use moon_cache::{CacheItem, RunTargetState}; @@ -23,8 +24,6 @@ use std::collections::HashMap; use std::sync::Arc; use tokio::sync::RwLock; -use crate::ActionRunnerError; - const LOG_TARGET: &str = "moon:action:run-target"; pub enum HydrateFrom { @@ -655,8 +654,21 @@ pub async fn run_target( return Ok(ActionStatus::Passed); } + let mut should_cache = task.options.cache; + + // If the VCS root does not exist (like in a Docker image), + // we should avoid failing and instead log a warning. + if !workspace.vcs.is_enabled() { + should_cache = false; + + warn!( + target: LOG_TARGET, + "VCS root not found, caching will be disabled!" + ); + } + // Abort early if this build has already been cached/hashed - if task.options.cache { + if should_cache { let common_hasher = runner.create_common_hasher(context).await?; let is_cached = match task.platform { @@ -728,7 +740,7 @@ pub async fn run_target( }; // If successful, cache the task outputs - if task.options.cache { + if should_cache { runner.cache_outputs().await?; } diff --git a/crates/cli/src/commands/setup.rs b/crates/cli/src/commands/setup.rs index 721233a03e3..7a58dde5578 100644 --- a/crates/cli/src/commands/setup.rs +++ b/crates/cli/src/commands/setup.rs @@ -1,6 +1,7 @@ use crate::helpers::{create_progress_bar, load_workspace}; use moon_action_runner::{ActionRunner, DepGraph}; use moon_contract::SupportedPlatform; +use moon_utils::is_test_env; pub async fn setup() -> Result<(), Box> { let done = create_progress_bar("Downloading and installing tools..."); @@ -10,6 +11,10 @@ pub async fn setup() -> Result<(), Box> { if workspace.config.node.is_some() { dep_graph.setup_tool(SupportedPlatform::Node); + + if !is_test_env() { + dep_graph.install_deps(SupportedPlatform::Node); + } } ActionRunner::new(workspace).run(dep_graph, None).await?; diff --git a/crates/cli/tests/snapshots/run_node_test__handles_process_exit_code_zero.snap b/crates/cli/tests/snapshots/run_node_test__handles_process_exit_code_zero.snap index 6832236ad60..c95a953ac0a 100644 --- a/crates/cli/tests/snapshots/run_node_test__handles_process_exit_code_zero.snap +++ b/crates/cli/tests/snapshots/run_node_test__handles_process_exit_code_zero.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 139 +assertion_line: 107 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__handles_process_exit_zero.snap b/crates/cli/tests/snapshots/run_node_test__handles_process_exit_zero.snap index 45a28a8b16b..bc897d54850 100644 --- a/crates/cli/tests/snapshots/run_node_test__handles_process_exit_zero.snap +++ b/crates/cli/tests/snapshots/run_node_test__handles_process_exit_zero.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 111 +assertion_line: 79 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__inherits_moon_env_vars.snap b/crates/cli/tests/snapshots/run_node_test__inherits_moon_env_vars.snap index f3c864e9111..81ff6b1870e 100644 --- a/crates/cli/tests/snapshots/run_node_test__inherits_moon_env_vars.snap +++ b/crates/cli/tests/snapshots/run_node_test__inherits_moon_env_vars.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 230 +assertion_line: 198 expression: "get_path_safe_output(&assert, fixture.path())" --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__runs_cjs_files.snap b/crates/cli/tests/snapshots/run_node_test__runs_cjs_files.snap index 927be30c82f..a4cf5af20b4 100644 --- a/crates/cli/tests/snapshots/run_node_test__runs_cjs_files.snap +++ b/crates/cli/tests/snapshots/run_node_test__runs_cjs_files.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 75 +assertion_line: 43 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__runs_from_project_root.snap b/crates/cli/tests/snapshots/run_node_test__runs_from_project_root.snap index 5bfb7a1f384..72d34c81f50 100644 --- a/crates/cli/tests/snapshots/run_node_test__runs_from_project_root.snap +++ b/crates/cli/tests/snapshots/run_node_test__runs_from_project_root.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 242 +assertion_line: 210 expression: "get_path_safe_output(&assert, fixture.path())" --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__runs_from_workspace_root.snap b/crates/cli/tests/snapshots/run_node_test__runs_from_workspace_root.snap index 1c62e024b07..21e065e93a8 100644 --- a/crates/cli/tests/snapshots/run_node_test__runs_from_workspace_root.snap +++ b/crates/cli/tests/snapshots/run_node_test__runs_from_workspace_root.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 254 +assertion_line: 222 expression: "get_path_safe_output(&assert, fixture.path())" --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__runs_mjs_files.snap b/crates/cli/tests/snapshots/run_node_test__runs_mjs_files.snap index 68033b400c4..dc578a01fb2 100644 --- a/crates/cli/tests/snapshots/run_node_test__runs_mjs_files.snap +++ b/crates/cli/tests/snapshots/run_node_test__runs_mjs_files.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 87 +assertion_line: 55 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__runs_package_managers.snap b/crates/cli/tests/snapshots/run_node_test__runs_package_managers.snap index bfcf8b2952b..51493a3f1e3 100644 --- a/crates/cli/tests/snapshots/run_node_test__runs_package_managers.snap +++ b/crates/cli/tests/snapshots/run_node_test__runs_package_managers.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 51 +assertion_line: 19 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__runs_standard_script.snap b/crates/cli/tests/snapshots/run_node_test__runs_standard_script.snap index 5d7cb32648f..9dfe589533a 100644 --- a/crates/cli/tests/snapshots/run_node_test__runs_standard_script.snap +++ b/crates/cli/tests/snapshots/run_node_test__runs_standard_script.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 63 +assertion_line: 31 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__sets_env_vars.snap b/crates/cli/tests/snapshots/run_node_test__sets_env_vars.snap index cbe5e5e5f02..98eecf75410 100644 --- a/crates/cli/tests/snapshots/run_node_test__sets_env_vars.snap +++ b/crates/cli/tests/snapshots/run_node_test__sets_env_vars.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 218 +assertion_line: 186 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install diff --git a/crates/cli/tests/snapshots/run_node_test__supports_top_level_await.snap b/crates/cli/tests/snapshots/run_node_test__supports_top_level_await.snap index c88e75fd106..b2e711d296c 100644 --- a/crates/cli/tests/snapshots/run_node_test__supports_top_level_await.snap +++ b/crates/cli/tests/snapshots/run_node_test__supports_top_level_await.snap @@ -1,6 +1,6 @@ --- source: crates/cli/tests/run_node_test.rs -assertion_line: 99 +assertion_line: 67 expression: get_assert_output(&assert) --- ▪▪▪▪ npm install diff --git a/crates/utils/src/lib.rs b/crates/utils/src/lib.rs index b8912a4c003..5105ba2aff8 100644 --- a/crates/utils/src/lib.rs +++ b/crates/utils/src/lib.rs @@ -9,6 +9,7 @@ pub mod time; use cached::proc_macro::cached; use std::env; +use std::path::PathBuf; #[macro_export] macro_rules! string_vec { @@ -29,6 +30,10 @@ pub fn is_ci() -> bool { } } +pub fn is_docker_container() -> bool { + PathBuf::from("/.dockerenv").exists() +} + // TODO: This doesn't work behind VPN or corporate proxies. Disabling for now // until we can figure out a better solution. #[cached(time = 60)] diff --git a/packages/cli/CHANGELOG.md b/packages/cli/CHANGELOG.md index 44e13fffba6..fd83e07b408 100644 --- a/packages/cli/CHANGELOG.md +++ b/packages/cli/CHANGELOG.md @@ -2,11 +2,18 @@ ## Unreleased +#### 💥 Breaking + +- Reworked how caching/hashing works when running in a Docker container/image. If the VCS root + cannot be found, we disable caching. This removes the requirement of mounting a `.git` volume for + Docker. + #### 🚀 Updates - Updated the project graph to scan and find implicit dependencies based on language specific semantics. For example, will determine moon projects based on `package.json` names and dependencies. +- Updated `moon setup` to also install Node.js dependencies. ## 0.13.0 diff --git a/website/docs/guides/ci.mdx b/website/docs/guides/ci.mdx index 1b3b49c3f21..17349bee32f 100644 --- a/website/docs/guides/ci.mdx +++ b/website/docs/guides/ci.mdx @@ -73,16 +73,16 @@ on: jobs: ci: name: 'CI' - runs-on: ubuntu-latest + runs-on: 'ubuntu-latest' steps: - - uses: actions/checkout@v3 + - uses: 'actions/checkout@v3' with: fetch-depth: 0 - - uses: actions/setup-node@v3 + - uses: 'actions/setup-node@v3' with: - cache: yarn - - run: yarn install --immutable - - run: yarn moon ci + cache: 'yarn' + - run: 'yarn install --immutable' + - run: 'yarn moon ci' ``` @@ -92,8 +92,8 @@ jobs: steps: - label: 'CI' commands: - - yarn install --immutable - - yarn moon ci + - 'yarn install --immutable' + - 'yarn moon ci' ``` @@ -113,13 +113,13 @@ jobs: install-yarn: true node-version: '16.13' - node/install-packages: - check-cache: always - pkg-manager: yarn-berry - - run: yarn moon ci + check-cache: 'always' + pkg-manager: 'yarn-berry' + - run: 'yarn moon ci' workflows: pipeline: jobs: - - ci + - 'ci' ``` @@ -130,7 +130,7 @@ language: node_js node_js: - 16 cache: yarn -script: moon ci +script: 'moon ci' ``` @@ -170,7 +170,7 @@ jobs: index: [0, 1] steps: # ... - - run: yarn moon ci --job ${{ matrix.index }} --jobTotal 2 + - run: 'yarn moon ci --job ${{ matrix.index }} --jobTotal 2' ``` - [Documentation](https://docs.github.com/en/actions/using-jobs/using-a-matrix-for-your-jobs) @@ -185,7 +185,7 @@ steps: parallelism: 10 commands: # ... - - yarn moon ci --job $$BUILDKITE_PARALLEL_JOB --jobTotal $$BUILDKITE_PARALLEL_JOB_COUNT + - 'yarn moon ci --job $$BUILDKITE_PARALLEL_JOB --jobTotal $$BUILDKITE_PARALLEL_JOB_COUNT' ``` - [Documentation](https://buildkite.com/docs/tutorials/parallel-builds#parallel-jobs) @@ -201,7 +201,7 @@ jobs: parallelism: 10 steps: # ... - - run: yarn moon ci --job $CIRCLE_NODE_INDEX --jobTotal $CIRCLE_NODE_TOTAL + - run: 'yarn moon ci --job $CIRCLE_NODE_INDEX --jobTotal $CIRCLE_NODE_TOTAL' ``` - [Documentation](https://circleci.com/docs/2.0/parallelism-faster-jobs/) @@ -219,7 +219,7 @@ env: jobs: - TRAVIS_JOB_INDEX=0 - TRAVIS_JOB_INDEX=1 -script: moon ci --job $TRAVIS_JOB_INDEX --jobTotal $TRAVIS_JOB_TOTAL +script: 'moon ci --job $TRAVIS_JOB_INDEX --jobTotal $TRAVIS_JOB_TOTAL' ``` - [Documentation](https://docs.travis-ci.com/user/speeding-up-the-build/) diff --git a/website/docs/guides/docker.mdx b/website/docs/guides/docker.mdx new file mode 100644 index 00000000000..518499450af --- /dev/null +++ b/website/docs/guides/docker.mdx @@ -0,0 +1,50 @@ +--- +title: Docker usage +--- + +Using [Docker](https://www.docker.com/) to run your applications? Or build your artifacts? No +worries, moon can be utilized with Docker! For the most part, everything should just work, but we +have disabled caching and hashing in Docker containers and images for the following reasons: + +- Avoids having to mount a volume for the `.git` directory. +- Reduces the overall image size as we're avoiding creating tarballs. +- Ensures that builds are fresh and up-to-date. + +:::success + +Looking to speed up your Docker builds? Want to build in the cloud? +[Give Depot a try](https://depot.dev?ref=moonrepo)! + +::: + +## Dockerfile + +This is merely an example of using moon within a `Dockerfile` while taking advantage of its layer +caching. The final 2 steps should be customized to your project's needs! + +```docker +FROM node:latest + +WORKDIR /app + +# Install moon binary +RUN npm install -g @moonrepo/cli + +# Copy moon files +COPY ./.moon ./.moon + +# Copy all package.json's and lockfiles +COPY ... +COPY ./package.json ./package.json +COPY ./yarn.lock ./yarn.lock + +# Install toolchain and dependencies +RUN moon setup + +# Copy project and required files +COPY ./app/example/project ./app/example/project +COPY ... + +# Run target +CMD ["moon", "run", "project:task"] +``` diff --git a/website/docs/guides/open-source.mdx b/website/docs/guides/open-source.mdx index 84b12eb8248..f46ac5f68d3 100644 --- a/website/docs/guides/open-source.mdx +++ b/website/docs/guides/open-source.mdx @@ -26,25 +26,25 @@ jobs: runs-on: ${{ matrix.os }} strategy: matrix: - os: [ubuntu-latest, windows-latest] + os: ['ubuntu-latest', 'windows-latest'] node-version: [14, 16, 18] steps: # Checkout repository - - uses: actions/checkout@v3 + - uses: 'actions/checkout@v3' with: fetch-depth: 0 # Install Node.js so we can install dependencies - - uses: actions/setup-node@v3 + - uses: 'actions/setup-node@v3' with: - cache: yarn + cache: 'yarn' # Set Node.js version to use in moon - - uses: moonrepo/tool-version-action@v1 + - uses: 'moonrepo/tool-version-action@v1' with: node: ${{ matrix.node-version }} # Install dependencies - - run: yarn install --immutable + - run: 'yarn install --immutable' # Run moon and affected tasks - - run: yarn moon ci + - run: 'yarn moon ci' ``` :::info diff --git a/website/docusaurus.config.js b/website/docusaurus.config.js index 2eff0df58b9..31298f24bde 100644 --- a/website/docusaurus.config.js +++ b/website/docusaurus.config.js @@ -185,7 +185,7 @@ const config = { prism: { theme: prismTheme, darkTheme: prismTheme, - additionalLanguages: ['twig'], + additionalLanguages: ['docker', 'twig'], }, }), diff --git a/website/sidebars.js b/website/sidebars.js index 08943408b11..2b8bb98ae0e 100644 --- a/website/sidebars.js +++ b/website/sidebars.js @@ -27,6 +27,7 @@ const sidebars = { items: [ 'guides/ci', 'guides/codegen', + 'guides/docker', 'guides/open-source', 'guides/profile', 'guides/root-project',