Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 43 additions & 1 deletion docs/current_docs/cookbook/cookbook.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,12 @@ The following Dagger module uses a constructor to set a module-wide `Directory`
```typescript file=./snippets/set-common-default-path/typescript/index.ts
```

</TabItem>
<TabItem value="PHP">

```php file=./snippets/set-common-default-path/php/src/MyModule.php
```

</TabItem>
</Tabs>

Expand Down Expand Up @@ -456,6 +462,12 @@ The following Dagger Function performs a multi-stage build.
```typescript file=./snippets/builds/multi-stage-build/typescript/index.ts
```

</TabItem>
<TabItem value="PHP">

```php file=./snippets/builds/multi-stage-build/php/src/MyModule.php
```

</TabItem>
</Tabs>

Expand Down Expand Up @@ -489,6 +501,12 @@ The following Dagger Function performs a matrix build.
```typescript file=./snippets/builds/matrix-build/typescript/index.ts
```

</TabItem>
<TabItem value="PHP">

```php file=./snippets/builds/matrix-build/php/src/MyModule.php
```

</TabItem>
</Tabs>

Expand Down Expand Up @@ -612,14 +630,20 @@ The following Dagger Function builds an image from a Dockerfile.
```typescript file=./snippets/builds/dockerfile/typescript/index.ts
```

</TabItem>
<TabItem value="PHP">

```php file=./snippets/builds/dockerfile/php/src/MyModule.php
```

</TabItem>
</Tabs>

#### Example
Build and publish an image from an existing Dockerfile

```shell
dagger call build --src https://github.com/dockersamples/python-flask-redis
dagger call build --src=https://github.com/dockersamples/python-flask-redis
```

### Build image from Dockerfile using different build context
Expand Down Expand Up @@ -750,6 +774,12 @@ The following function demonstrates how to invalidate the Dagger layer cache and
```typescript file=./snippets/builds/cache/typescript/index.ts
```

</TabItem>
<TabItem value="PHP">

```php file=./snippets/builds/cache/php/src/MyModule.php
```

</TabItem>
</Tabs>

Expand Down Expand Up @@ -1302,6 +1332,12 @@ The following Dagger Function uses a cache volume for application dependencies.
```typescript file=./snippets/cache-dependencies/typescript/index.ts
```

</TabItem>
<TabItem value="PHP">

```php file=./snippets/cache-dependencies/php/src/MyModule.php
```

</TabItem>
</Tabs>

Expand Down Expand Up @@ -1335,6 +1371,12 @@ The following Dagger Function demonstrates how to set a single environment varia
```typescript file=./snippets/set-env-var/typescript/index.ts
```

</TabItem>
<TabItem value="PHP">

```php file=./snippets/set-env-var/php/src/MyModule.php
```

</TabItem>
</Tabs>

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "daggermodule/my-module",
"description": "",
"version": "1.0.0",
"minimum-stability": "dev",
"license": "proprietary",
"authors": [
],
"repositories": [
{
"type": "path",
"url": "./sdk"
}
],
"require": {
"php": "^8.1",
"dagger/dagger": "*@dev"
},
"autoload": {
"psr-4": {
"DaggerModule\\": "src/"
}
},
"config": {
"platform": {
"php": "8.3.7"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "my-module",
"engineVersion": "v0.15.3",
"sdk": "php"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

/**
* This is the entry point for the module, called from the dagger engine.
* Editing this file is highly discouraged and may stop your module from functioning entirely.
*/

use Symfony\Component\Console\Application;
use Dagger\Command\EntrypointCommand;

if (file_exists(__DIR__.'/../../autoload.php')) {
// The usual location, since this file will reside in vendor/bin
require __DIR__.'/../../autoload.php';
} else {
// Useful when doing development on this package
require __DIR__.'/vendor/autoload.php';
}

$console = new Application();

$console->add(new EntrypointCommand());
$console->setDefaultCommand('dagger:entrypoint');

$console->run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

declare(strict_types=1);

namespace DaggerModule;

use Dagger\Attribute\DaggerFunction;
use Dagger\Attribute\DaggerObject;
use Dagger\Directory;

use function Dagger\dag;

#[DaggerObject]
class MyModule
{
// Run a build with cache invalidation
#[DaggerFunction]
public function build(): string
{
return dag()
->container()
->from('alpine')
// comment out the line below to see the cached date output
->withEnvVariable('CACHEBUSTER', date(DATE_RFC2822))
->withExec(['date'])
->stdout();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "daggermodule/my-module",
"description": "",
"version": "1.0.0",
"minimum-stability": "dev",
"license": "proprietary",
"authors": [
],
"repositories": [
{
"type": "path",
"url": "./sdk"
}
],
"require": {
"php": "^8.1",
"dagger/dagger": "*@dev"
},
"autoload": {
"psr-4": {
"DaggerModule\\": "src/"
}
},
"config": {
"platform": {
"php": "8.3.7"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "my-module",
"engineVersion": "v0.15.3",
"sdk": "php"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

/**
* This is the entry point for the module, called from the dagger engine.
* Editing this file is highly discouraged and may stop your module from functioning entirely.
*/

use Symfony\Component\Console\Application;
use Dagger\Command\EntrypointCommand;

if (file_exists(__DIR__.'/../../autoload.php')) {
// The usual location, since this file will reside in vendor/bin
require __DIR__.'/../../autoload.php';
} else {
// Useful when doing development on this package
require __DIR__.'/vendor/autoload.php';
}

$console = new Application();

$console->add(new EntrypointCommand());
$console->setDefaultCommand('dagger:entrypoint');

$console->run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

declare(strict_types=1);

namespace DaggerModule;

use Dagger\Attribute\DaggerFunction;
use Dagger\Attribute\DaggerObject;
use Dagger\Directory;

use function Dagger\dag;

#[DaggerObject]
class MyModule
{
// Build and publish image from existing Dockerfile
#[DaggerFunction]
public function build(
// location of directory containing Dockerfile
Directory $src,
): string {
$ref = $src
->dockerBuild() // build from Dockerfile
->publish('ttl.sh/hello-dagger');
return $ref;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "daggermodule/my-module",
"description": "",
"version": "1.0.0",
"minimum-stability": "dev",
"license": "proprietary",
"authors": [
],
"repositories": [
{
"type": "path",
"url": "./sdk"
}
],
"require": {
"php": "^8.1",
"dagger/dagger": "*@dev"
},
"autoload": {
"psr-4": {
"DaggerModule\\": "src/"
}
},
"config": {
"platform": {
"php": "8.3.7"
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"name": "my-module",
"engineVersion": "v0.15.3",
"sdk": "php"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#!/usr/bin/env php
<?php declare(strict_types=1);

/**
* This is the entry point for the module, called from the dagger engine.
* Editing this file is highly discouraged and may stop your module from functioning entirely.
*/

use Symfony\Component\Console\Application;
use Dagger\Command\EntrypointCommand;

if (file_exists(__DIR__.'/../../autoload.php')) {
// The usual location, since this file will reside in vendor/bin
require __DIR__.'/../../autoload.php';
} else {
// Useful when doing development on this package
require __DIR__.'/vendor/autoload.php';
}

$console = new Application();

$console->add(new EntrypointCommand());
$console->setDefaultCommand('dagger:entrypoint');

$console->run();
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace DaggerModule;

use Dagger\Attribute\DaggerFunction;
use Dagger\Attribute\DaggerObject;
use Dagger\Directory;

use function Dagger\dag;

#[DaggerObject]
class MyModule
{
// define build matrix
const GOOSES = ['linux', 'darwin'];
const GOARCHES = ['amd64', 'arm64'];

// Build and return directory of go binaries
#[DaggerFunction]
public function build(Directory $src): Directory
{
// create empty directory to put build artifacts
$outputs = dag()->directory();

$golang = dag()
->container()
->from('golang:latest')
->withDirectory('/src', $src)
->withWorkdir('/src');

foreach (self::GOOSES as $goos) {
foreach (self::GOARCHES as $goarch) {
// create a directory for each OS and architecture
$path = "build/$goos/$goarch/";
Comment thread
vikram-dagger marked this conversation as resolved.

// build artifact
$build = $golang
->withEnvVariable('GOOS', $goos)
->withEnvVariable('GOARCH', $goarch)
->withExec(['go', 'build', '-o', $path]);

// add build to outputs
$outputs = $outputs->withDirectory($path, $build->directory($path));
}
}

return $outputs;
}
}
Loading