Skip to content

Commit

Permalink
new: Disable caching in Docker containers. (#306)
Browse files Browse the repository at this point in the history
* Add docker check.

* Add docker page.

* Add workdir.

* Wrap in strings.

* Check for VCS instead.

* Move logic.

* Update setup.

* Fix workflow.
  • Loading branch information
milesj committed Sep 14, 2022
1 parent c12d57c commit 06257ca
Show file tree
Hide file tree
Showing 22 changed files with 123 additions and 40 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions Makefile.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
20 changes: 16 additions & 4 deletions crates/action-runner/src/actions/run_target.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::ActionRunnerError;
use console::Term;
use moon_action::{Action, ActionContext, ActionStatus, Attempt};
use moon_cache::{CacheItem, RunTargetState};
Expand All @@ -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 {
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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?;
}

Expand Down
5 changes: 5 additions & 0 deletions crates/cli/src/commands/setup.rs
Original file line number Diff line number Diff line change
@@ -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<dyn std::error::Error>> {
let done = create_progress_bar("Downloading and installing tools...");
Expand All @@ -10,6 +11,10 @@ pub async fn setup() -> Result<(), Box<dyn std::error::Error>> {

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?;
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions crates/utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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)]
Expand Down
7 changes: 7 additions & 0 deletions packages/cli/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
34 changes: 17 additions & 17 deletions website/docs/guides/ci.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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'
```

</TabItem>
Expand All @@ -92,8 +92,8 @@ jobs:
steps:
- label: 'CI'
commands:
- yarn install --immutable
- yarn moon ci
- 'yarn install --immutable'
- 'yarn moon ci'
```

</TabItem>
Expand All @@ -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'
```

</TabItem>
Expand All @@ -130,7 +130,7 @@ language: node_js
node_js:
- 16
cache: yarn
script: moon ci
script: 'moon ci'
```

</TabItem>
Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand All @@ -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/)
Expand All @@ -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/)
Expand Down
50 changes: 50 additions & 0 deletions website/docs/guides/docker.mdx
Original file line number Diff line number Diff line change
@@ -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"]
```
14 changes: 7 additions & 7 deletions website/docs/guides/open-source.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion website/docusaurus.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ const config = {
prism: {
theme: prismTheme,
darkTheme: prismTheme,
additionalLanguages: ['twig'],
additionalLanguages: ['docker', 'twig'],
},
}),

Expand Down
Loading

0 comments on commit 06257ca

Please sign in to comment.