Skip to content

Commit

Permalink
Fix .mjs docs lint and format (dagger#5435)
Browse files Browse the repository at this point in the history
Signed-off-by: Helder Correia <174525+helderco@users.noreply.github.com>
  • Loading branch information
helderco authored Jul 11, 2023
1 parent c44009e commit bd94e23
Show file tree
Hide file tree
Showing 29 changed files with 1,026 additions and 888 deletions.
30 changes: 16 additions & 14 deletions docs/current/cookbook/snippets/cache-invalidation/index.mjs
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { connect } from "@dagger.io/dagger"

// create Dagger client
connect(async (client) => {
connect(
async (client) => {
// invalidate cache to force execution
// of second withExec() operation
const output = await client
.pipeline("test")
.container()
.from("alpine")
.withExec(["apk", "add", "curl"])
.withEnvVariable("CACHEBUSTER", Date.now().toString())
.withExec(["apk", "add", "zip"])
.stdout()

// invalidate cache to force execution
// of second withExec() operation
const output = await client.pipeline("test").
container().
from("alpine").
withExec(["apk", "add", "curl"]).
withEnvVariable("CACHEBUSTER", Date.now().toString()).
withExec(["apk", "add", "zip"]).
stdout()

console.log(output)

}, {LogOutput: process.stderr})
console.log(output)
},
{ LogOutput: process.stderr }
)
53 changes: 27 additions & 26 deletions docs/current/cookbook/snippets/multiple-tags/index.mjs
Original file line number Diff line number Diff line change
@@ -1,33 +1,34 @@
import { connect } from "@dagger.io/dagger"

// create Dagger client
connect(async (client) => {
// define tags
const tags = ["latest", "1.0-alpine", "1.0", "1.0.0"]
connect(
async (client) => {
// define tags
const tags = ["latest", "1.0-alpine", "1.0", "1.0.0"]

if(!process.env.DOCKERHUB_USERNAME) {
console.log('DOCKERHUB_USERNAME environment variable must be set');
process.exit();
}
if(!process.env.DOCKERHUB_PASSWORD) {
console.log('DOCKERHUB_PASSWORD environment variable must be set');
process.exit();
}
const username = process.env.DOCKERHUB_USERNAME;
const password = process.env.DOCKERHUB_PASSWORD;
if (!process.env.DOCKERHUB_USERNAME) {
console.log("DOCKERHUB_USERNAME environment variable must be set")
process.exit()
}
if (!process.env.DOCKERHUB_PASSWORD) {
console.log("DOCKERHUB_PASSWORD environment variable must be set")
process.exit()
}
const username = process.env.DOCKERHUB_USERNAME
const password = process.env.DOCKERHUB_PASSWORD

// set secret as string value
const secret = client.setSecret("password", password);
// set secret as string value
const secret = client.setSecret("password", password)

// create and publish image with multiple tags
const container = client.container().
from("alpine")
// create and publish image with multiple tags
const container = client.container().from("alpine")

for (var tag in tags) {
let addr = await container.
withRegistryAuth("docker.io", username, secret).
publish(`${username}/my-alpine:${tags[tag]}`)
console.log(`Published at: ${addr}`)
}

}, {LogOutput: process.stderr})
for (var tag in tags) {
let addr = await container
.withRegistryAuth("docker.io", username, secret)
.publish(`${username}/my-alpine:${tags[tag]}`)
console.log(`Published at: ${addr}`)
}
},
{ LogOutput: process.stderr }
)
33 changes: 19 additions & 14 deletions docs/current/cookbook/snippets/oci-annotations/index.mjs
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
import { connect } from "@dagger.io/dagger"

// create Dagger client
connect(async (client) => {
connect(
async (client) => {
// create and publish image with annotations
const container = client
.container()
.from("alpine")
.withLabel("org.opencontainers.image.title", "my-alpine")
.withLabel("org.opencontainers.image.version", "1.0")
.withLabel("org.opencontainers.image.created", new Date())
.WithLabel(
"org.opencontainers.image.source",
"https://github.com/alpinelinux/docker-alpine"
)
.WithLabel("org.opencontainers.image.licenses", "MIT")

// create and publish image with annotations
const container = client.container().
from("alpine").
withLabel("org.opencontainers.image.title", "my-alpine").
withLabel("org.opencontainers.image.version", "1.0").
withLabel("org.opencontainers.image.created", new Date()).
WithLabel("org.opencontainers.image.source", "https://github.com/alpinelinux/docker-alpine").
WithLabel("org.opencontainers.image.licenses", "MIT")
const addr = await container.publish("ttl.sh/my-alpine")

const addr = await container.publish("ttl.sh/my-alpine")

console.log(addr)

}, {LogOutput: process.stderr})
console.log(addr)
},
{ LogOutput: process.stderr }
)
112 changes: 63 additions & 49 deletions docs/current/cookbook/snippets/secrets-vault/index.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -2,76 +2,90 @@ import { connect } from "@dagger.io/dagger"
import fetch from "node-fetch"

// initialize Dagger client
connect(async (client) => {
connect(
async (client) => {
// get secret from Vault
const secretPlaintext = await getVaultSecret(
"MOUNT-PATH",
"SECRET-ID",
"SECRET-KEY"
)

// get secret from Vault
const secretPlaintext = await getVaultSecret("MOUNT-PATH", "SECRET-ID", "SECRET-KEY")
// load secret into Dagger
const secret = client.setSecret("ghApiToken", secretPlaintext)

// load secret into Dagger
const secret = client.setSecret("ghApiToken", secretPlaintext)
// use secret in container environment
const out = await client
.container()
.from("alpine:3.17")
.withSecretVariable("GITHUB_API_TOKEN", secret)
.withExec(["apk", "add", "curl"])
.withExec([
"sh",
"-c",
`curl "https://api.github.com/repos/dagger/dagger/issues" --header "Accept: application/vnd.github+json" --header "Authorization: Bearer $GITHUB_API_TOKEN"`,
])
.stdout()

// use secret in container environment
const out = await client
.container()
.from("alpine:3.17")
.withSecretVariable("GITHUB_API_TOKEN", secret)
.withExec(["apk", "add", "curl"])
.withExec(["sh", "-c", `curl "https://api.github.com/repos/dagger/dagger/issues" --header "Accept: application/vnd.github+json" --header "Authorization: Bearer $GITHUB_API_TOKEN"`])
.stdout()

// print result
console.log(out)
}, {LogOutput: process.stderr})
// print result
console.log(out)
},
{ LogOutput: process.stderr }
)

async function getVaultSecret(mountPath, secretID, secretKey) {

// check for required variables in host environment
const vars = ["VAULT_ADDRESS", "VAULT_NAMESPACE", "VAULT_ROLE_ID", "VAULT_SECRET_ID"];
vars.forEach(v => {
if(!process.env[v]) {
console.log(`${v} variable must be set`);
process.exit();
const vars = [
"VAULT_ADDRESS",
"VAULT_NAMESPACE",
"VAULT_ROLE_ID",
"VAULT_SECRET_ID",
]
vars.forEach((v) => {
if (!process.env[v]) {
console.log(`${v} variable must be set`)
process.exit()
}
});
})

const address = process.env.VAULT_ADDRESS;
const namespace = process.env.VAULT_NAMESPACE;
const role = process.env.VAULT_ROLE_ID;
const secret = process.env.VAULT_SECRET_ID;
const address = process.env.VAULT_ADDRESS
const namespace = process.env.VAULT_NAMESPACE
const role = process.env.VAULT_ROLE_ID
const secret = process.env.VAULT_SECRET_ID

// request client token
let url = `${address}/v1/auth/approle/login`;
let body = {"role_id": role, "secret_id": secret};
let url = `${address}/v1/auth/approle/login`
let body = { role_id: role, secret_id: secret }
let options = {
method: 'POST',
method: "POST",
headers: {
'Accept': 'application/json',
'X-Vault-Namespace': `${namespace}`
Accept: "application/json",
"X-Vault-Namespace": `${namespace}`,
},
body: JSON.stringify(body)
};
body: JSON.stringify(body),
}

// read client token
let tokenResponse = await fetch(url, options)
.then(res => res.json())
.catch(err => console.error('Error: ' + err));
const token = tokenResponse.auth.client_token;
.then((res) => res.json())
.catch((err) => console.error("Error: " + err))
const token = tokenResponse.auth.client_token

// request secret
url = `${address}/v1/${mountPath}/data/${secretID}`;
url = `${address}/v1/${mountPath}/data/${secretID}`
options = {
method: 'GET',
method: "GET",
headers: {
'Accept': 'application/json',
'X-Vault-Namespace': `${namespace}`,
'X-Vault-Token': `${token}`,
}
};
Accept: "application/json",
"X-Vault-Namespace": `${namespace}`,
"X-Vault-Token": `${token}`,
},
}

// return secret
let secretResponse = await fetch(url, options)
.then(res => res.json())
.catch(err => console.error('Error: ' + err));
.then((res) => res.json())
.catch((err) => console.error("Error: " + err))

return secretResponse.data.data[secretKey];
}
return secretResponse.data.data[secretKey]
}
88 changes: 48 additions & 40 deletions docs/current/guides/snippets/aws-codebuild-codepipeline/index.mjs
Original file line number Diff line number Diff line change
@@ -1,46 +1,54 @@
import { connect } from "@dagger.io/dagger"

// check for required variables in host environment
const vars = ["REGISTRY_ADDRESS", "REGISTRY_USERNAME", "REGISTRY_PASSWORD"];
vars.forEach(v => {
if(!process.env[v]) {
console.log(`${v} variable must be set`);
process.exit();
const vars = ["REGISTRY_ADDRESS", "REGISTRY_USERNAME", "REGISTRY_PASSWORD"]
vars.forEach((v) => {
if (!process.env[v]) {
console.log(`${v} variable must be set`)
process.exit()
}
});
})

// initialize Dagger client
connect(async (client) => {

// set registry password as Dagger secret
const secret = client.setSecret("password", process.env.REGISTRY_PASSWORD);

// get reference to the project directory
const source = client.host().directory(".", { exclude: ["node_modules/", "ci/"] });

// use a node:18-slim container
const node = client.container({ platform: "linux/amd64" })
.from("node:18-slim")

// mount the project directory
// at /src in the container
// set the working directory in the container
// install application dependencies
// build application
// set default arguments
const app = node
.withDirectory('/src', source)
.withWorkdir("/src")
.withExec(["npm", "install"])
.withExec(["npm", "run", "build"])
.withDefaultArgs({args: ["npm", "start"]})

// publish image to registry
// at registry path [registry-username]/myapp
// print image address
const address = await app
.withRegistryAuth(process.env.REGISTRY_ADDRESS, process.env.REGISTRY_USERNAME, secret)
.publish(`${process.env.REGISTRY_USERNAME}/myapp`);
console.log(`Published image to: ${address}`)

}, { LogOutput: process.stdout })
connect(
async (client) => {
// set registry password as Dagger secret
const secret = client.setSecret("password", process.env.REGISTRY_PASSWORD)

// get reference to the project directory
const source = client
.host()
.directory(".", { exclude: ["node_modules/", "ci/"] })

// use a node:18-slim container
const node = client
.container({ platform: "linux/amd64" })
.from("node:18-slim")

// mount the project directory
// at /src in the container
// set the working directory in the container
// install application dependencies
// build application
// set default arguments
const app = node
.withDirectory("/src", source)
.withWorkdir("/src")
.withExec(["npm", "install"])
.withExec(["npm", "run", "build"])
.withDefaultArgs({ args: ["npm", "start"] })

// publish image to registry
// at registry path [registry-username]/myapp
// print image address
const address = await app
.withRegistryAuth(
process.env.REGISTRY_ADDRESS,
process.env.REGISTRY_USERNAME,
secret
)
.publish(`${process.env.REGISTRY_USERNAME}/myapp`)
console.log(`Published image to: ${address}`)
},
{ LogOutput: process.stdout }
)
Loading

0 comments on commit bd94e23

Please sign in to comment.