From 013900c2a24f0d36a3e61a61630f1c736da33ce1 Mon Sep 17 00:00:00 2001 From: Solomon Hykes Date: Tue, 8 Feb 2022 17:54:42 +0000 Subject: [PATCH] bash.#Run: new and improved * Script is written to filesystem, not inlined as argument * Pass script either as string, or directory+filename Signed-off-by: Solomon Hykes --- pkg/universe.dagger.io/bash/bash.cue | 54 +++++++++--- pkg/universe.dagger.io/bash/test/bash.bats | 10 +++ .../bash/test/run-simple/hello.sh | 3 + .../bash/test/run-simple/simple.cue | 87 +++++++++++++++++++ pkg/universe.dagger.io/netlify/netlify.cue | 57 ++++-------- .../netlify/test/netlify-test.cue | 4 +- .../yarn/test/yarn-test.cue | 2 +- pkg/universe.dagger.io/yarn/yarn.cue | 29 ++----- 8 files changed, 171 insertions(+), 75 deletions(-) create mode 100644 pkg/universe.dagger.io/bash/test/bash.bats create mode 100644 pkg/universe.dagger.io/bash/test/run-simple/hello.sh create mode 100644 pkg/universe.dagger.io/bash/test/run-simple/simple.cue diff --git a/pkg/universe.dagger.io/bash/bash.cue b/pkg/universe.dagger.io/bash/bash.cue index ba694e99..afd3d50f 100644 --- a/pkg/universe.dagger.io/bash/bash.cue +++ b/pkg/universe.dagger.io/bash/bash.cue @@ -2,25 +2,59 @@ package bash import ( + "dagger.io/dagger" + "dagger.io/dagger/engine" + "universe.dagger.io/docker" ) -// Run a bash command or script in a container +// Run a bash script in a Docker container +// Since this is a thin wrapper over docker.#Run, we embed it. +// Whether to embed or wrap is a case-by-case decision, like in Go. #Run: { - // Contents of the bash script - script: string + // The script to execute + script: { + // A directory containing one or more bash scripts + directory: dagger.#FS + + // Name of the file to execute + filename: string + + _directory: directory + _filename: filename + } | { + // Script contents + contents: string + + _filename: "run.sh" + _write: engine.#WriteFile & { + input: engine.#Scratch + path: _filename + "contents": contents + } + _directory: _write.output + } + + // Arguments to the script + args: [...string] + + // Where in the container to mount the scripts directory + _mountpoint: "/bash/scripts" - // FIXME: don't pass the script as argument: write to filesystme instead docker.#Run & { command: { - name: "bash" + name: "bash" + "args": ["\(_mountpoint)/\(script._filename)"] + args + // FIXME: make default flags overrideable flags: { - "-c": script - "--noprofile": true - "--norc": true - "-e": true - "-o": "pipefail" + "--norc": true + "-e": true + "-o": "pipefail" } } + mounts: "Bash scripts": { + contents: script._directory + dest: _mountpoint + } } } diff --git a/pkg/universe.dagger.io/bash/test/bash.bats b/pkg/universe.dagger.io/bash/test/bash.bats new file mode 100644 index 00000000..42186abf --- /dev/null +++ b/pkg/universe.dagger.io/bash/test/bash.bats @@ -0,0 +1,10 @@ +setup() { + load '../../bats_helpers' + + common_setup +} + +@test "bash.#Run" { + dagger up ./run-simple +} + diff --git a/pkg/universe.dagger.io/bash/test/run-simple/hello.sh b/pkg/universe.dagger.io/bash/test/run-simple/hello.sh new file mode 100644 index 00000000..2bf977e9 --- /dev/null +++ b/pkg/universe.dagger.io/bash/test/run-simple/hello.sh @@ -0,0 +1,3 @@ +#!/bin/sh + +echo Hello, world > /out.txt diff --git a/pkg/universe.dagger.io/bash/test/run-simple/simple.cue b/pkg/universe.dagger.io/bash/test/run-simple/simple.cue new file mode 100644 index 00000000..3227b9b0 --- /dev/null +++ b/pkg/universe.dagger.io/bash/test/run-simple/simple.cue @@ -0,0 +1,87 @@ +package bash + +import ( + "dagger.io/dagger" + + "universe.dagger.io/docker" + "universe.dagger.io/alpine" +) + +dagger.#DAG & { + actions: { + "Run from source directory": { + build: alpine.#Build & { + packages: bash: _ + } + run: #Run & { + image: build.output + script: { + directory: loadScripts.output + filename: "hello.sh" + } + export: files: "/out.txt": _ + } + output: run.export.files."/out.txt".contents & "Hello, world\n" + } + + "Run from source directory with custom image": { + debian: docker.#Pull & { + source: "index.docker.io/debian" + } + run: #Run & { + image: debian.output + export: files: "/out.txt": _ + script: { + directory: loadScripts.output + filename: "hello.sh" + } + } + output: run.export.files."/out.txt".contents & "Hello, world\n" + } + + "Run from string": { + run: #Run & { + script: contents: "echo 'Hello, inlined world!' > /output.txt" + export: files: "/output.txt": _ + } + output: run.export.files."/output.txt".contents & "Hello, inlined world!\n" + } + + "Run from string with custom image": { + debian: docker.#Pull & { + source: "index.docker.io/debian" + } + run: #Run & { + image: debian.output + export: files: "/output.txt": _ + script: contents: "echo 'Hello, inlined world!' > /output.txt" + } + output: run.export.files."/output.txt".contents & "Hello, inlined world!\n" + } + + // Same thing but without bash.#Run + control: { + run: docker.#Run & { + image: base.output + command: { + name: "sh" + args: ["/bash/scripts/hello.sh"] + } + mounts: scripts: { + contents: loadScripts.output + dest: "/bash/scripts" + } + export: files: "/out.txt": _ + } + output: run.export.files."/out.txt".contents & "Hello, world\n" + base: docker.#Pull & { + source: "alpine" + } + } + + loadScripts: dagger.#Source & { + path: "." + include: ["*.sh"] + } + } +} diff --git a/pkg/universe.dagger.io/netlify/netlify.cue b/pkg/universe.dagger.io/netlify/netlify.cue index bc278481..f526fd67 100644 --- a/pkg/universe.dagger.io/netlify/netlify.cue +++ b/pkg/universe.dagger.io/netlify/netlify.cue @@ -4,9 +4,11 @@ package netlify import ( "dagger.io/dagger" + "dagger.io/dagger/engine" "universe.dagger.io/alpine" "universe.dagger.io/docker" + "universe.dagger.io/bash" ) // Deploy a site to Netlify @@ -34,12 +36,7 @@ import ( // Create the site if it doesn't exist create: *true | false - // Source code of the Netlify package - _source: dagger.#Source & { - path: "." - include: ["*.sh"] - } - + // Build a docker image to run the netlify client _build: docker.#Build & { steps: [ alpine.#Build & { @@ -50,45 +47,26 @@ import ( yarn: {} } }, + // FIXME: make this an alpine custom package, that would be so cool. docker.#Run & { command: { name: "yarn" args: ["global", "add", "netlify-cli@8.6.21"] } }, - docker.#Copy & { - contents: _source.output - dest: "/app" - }, ] } - // Execute `netlify deploy` in a container - command: docker.#Run & { - // FIXME: custom base image not supported - // Container image. `netlify` must be available in the execution path - // *{ - // _buildDefaultImage: docker.#Build & { - // input: alpine.#Build & { - // bash: version: "=~5.1" - // jq: version: "=~1.6" - // curl: {} - // yarn: version: "=~1.22" - // } - // steps: [{ - // run: command: { - // name: "sh" - // flags: "-c": "yarn global add netlify-cli@3.38.10" - // } - // }] - // } - - // // No nested tasks, boo hoo hoo - // image: _buildDefaultImage.output - // env: CUSTOM_IMAGE: "0" - // } | { - // env: CUSTOM_IMAGE: "1" - // } + // Run the netlify client in a container + container: bash.#Run & { + script: { + _load: engine.#Source & { + path: "." + include: ["*.sh"] + } + directory: _load.output + filename: "deploy.sh" + } image: _build.output @@ -114,7 +92,6 @@ import ( contents: token } } - command: name: "/app/deploy.sh" export: files: { "/netlify/url": _ @@ -124,11 +101,11 @@ import ( } // URL of the deployed site - url: command.export.files."/netlify/url".contents + url: container.export.files."/netlify/url".contents // URL of the latest deployment - deployUrl: command.export.files."/netlify/deployUrl".contents + deployUrl: container.export.files."/netlify/deployUrl".contents // URL for logs of the latest deployment - logsUrl: command.export.files."/netlify/logsUrl".contents + logsUrl: container.export.files."/netlify/logsUrl".contents } diff --git a/pkg/universe.dagger.io/netlify/test/netlify-test.cue b/pkg/universe.dagger.io/netlify/test/netlify-test.cue index 33658e6e..4de4864d 100644 --- a/pkg/universe.dagger.io/netlify/test/netlify-test.cue +++ b/pkg/universe.dagger.io/netlify/test/netlify-test.cue @@ -47,8 +47,8 @@ dagger.#Plan & { // Check if the website was deployed verify: bash.#Run & { - input: image.output - script: #""" + input: image.output + script: contents: #""" test "$(curl \#(deploy.deployUrl))" = "\#(marker)" """# } diff --git a/pkg/universe.dagger.io/yarn/test/yarn-test.cue b/pkg/universe.dagger.io/yarn/test/yarn-test.cue index d35d535f..aaacb5ae 100644 --- a/pkg/universe.dagger.io/yarn/test/yarn-test.cue +++ b/pkg/universe.dagger.io/yarn/test/yarn-test.cue @@ -33,7 +33,7 @@ dagger.#Plan & { dest: "/build" contents: pkg.output } - script: #""" + script: contents: #""" test "$(cat /build/test)" = "output" """# } diff --git a/pkg/universe.dagger.io/yarn/yarn.cue b/pkg/universe.dagger.io/yarn/yarn.cue index 46e34b7e..ae6b813b 100644 --- a/pkg/universe.dagger.io/yarn/yarn.cue +++ b/pkg/universe.dagger.io/yarn/yarn.cue @@ -48,33 +48,19 @@ import ( // yarnVersion: *"=~1.22" | string // FIXME: custom base image not supported - image: alpine.#Build & { + _buildImage: alpine.#Build & { packages: { bash: {} yarn: {} } } - // Run yarn in a containerized build environment - command: bash.#Run & { - // FIXME: not working? - // *{ - // _image: alpine.#Build & { - // packages: { - // bash: version: "=~5.1" - // yarn: version: yarnVersion - // } - // } + // Run yarn in a docker container + container: bash.#Run & { + image: _buildImage.output - // image: _image.output - // env: CUSTOM_IMAGE: "0" - // } | { - // env: CUSTOM_IMAGE: "1" - // } - - "image": image.output - - script: #""" + // FIXME: move shell script to its own file + script: contents: #""" # Create $ENVFILE_NAME file if set [ -n "$ENVFILE_NAME" ] && echo "$ENVFILE" > "$ENVFILE_NAME" @@ -94,7 +80,6 @@ import ( dest: "/src" contents: source } - // FIXME: mount secrets } export: directories: "/build": _ @@ -115,5 +100,5 @@ import ( } // The final contents of the package after build - output: command.export.directories."/build".contents + output: container.export.directories."/build".contents }