deployer/docs/basics.md

240 lines
5.9 KiB
Markdown
Raw Normal View History

2021-10-09 19:12:15 +02:00
# Basics
Deployer has two main concepts: [**hosts**](hosts.md) and [**tasks**](tasks.md).
A **recipe** is a file containing definitions for **hosts** and **tasks**.
2022-09-06 09:02:36 +02:00
Deployer CLI requires two arguments to run: a **task** to run and a **selector**.
2021-10-09 19:12:15 +02:00
```
$ dep deploy deployer.org
--- ------ ------------
| | |
2022-09-06 09:02:36 +02:00
| | `--- Selector
| `------------- Task
`------------------ CLI
2021-10-09 19:12:15 +02:00
```
Deployer uses the [selector](selector.md) to choose hosts. Next, it takes the given
2022-09-06 09:17:58 +02:00
task, performs some preparation (described later), and executes the task on all
2022-09-06 09:02:36 +02:00
selected hosts.
If a selector is not specified, Deployer will ask you to choose a host from a list.
2022-09-06 09:02:36 +02:00
If your recipe contains only one host, Deployer will automatically choose it.
To select all hosts, specify a special selector: `all`.
2021-10-09 19:12:15 +02:00
The `dep` CLI looks for a `deploy.php` or `deploy.yaml` file in the current directory.
2021-11-03 13:42:44 +01:00
Or a recipe can be specified explicitly via `-f` or `--file` option.
2022-09-06 09:17:58 +02:00
2021-11-10 23:20:32 +01:00
```
2021-10-09 19:12:15 +02:00
$ dep --file=deploy.php deploy deployer.org
```
2022-09-06 09:17:58 +02:00
Let's write a recipe.
2021-10-09 19:12:15 +02:00
```php
// We are going to use functions declared primarily in the Deployer namespace,
// to simplify the recipe, we will also use the Deployer namespace. Alternatively,
2021-10-09 19:12:15 +02:00
// you can import individual functions via "use function".
namespace Deployer;
host('deployer.org');
2021-11-03 13:42:44 +01:00
task('my_task', function () {
2021-10-09 19:12:15 +02:00
run('whoami');
2022-09-06 09:17:58 +02:00
});
2021-10-09 19:12:15 +02:00
```
2021-11-03 13:42:44 +01:00
Let's try to run our task on deployer.org.
2021-10-09 19:12:15 +02:00
```
2021-11-03 13:42:44 +01:00
$ dep my_task
task my_task
2022-09-06 09:17:58 +02:00
$
2021-10-09 19:12:15 +02:00
```
2022-09-06 09:17:58 +02:00
But where is our `whoami` command output? By default, Deployer runs with normal verbosity
level and shows only the names of executed tasks. Let's increase verbosity to verbose, and
2021-11-03 13:42:44 +01:00
rerun our task.
Add `-v` option to increase verbosity. Read more about [CLI usage](cli.md).
```
$ dep my_task -v
task my_task
[deployer.org] run whoami
[deployer.org] deployer
$
```
Now let's add a second host:
2021-11-03 13:42:44 +01:00
```php
host('deployer.org');
host('medv.io');
```
How does Deployer know how to connect to a host? It uses the same `~/.ssh/config` file as
the `ssh` command. Alternatively, you can specify [connection options](hosts.md) in the recipe.
2021-11-03 13:42:44 +01:00
Let's run `my_task` task on both hosts:
```
2021-11-03 15:49:39 +01:00
$ dep my_task -v all
2021-11-03 13:42:44 +01:00
task my_task
[deployer.org] run whoami
[medv.io] run whoami
[medv.io] anton
[deployer.org] deployer
```
2022-09-06 09:17:58 +02:00
Deployer runs a task in parallel on each host. This is why the output is mixed.
2022-09-06 09:02:36 +02:00
We can limit it to run only on one host at a time.
2021-11-03 13:42:44 +01:00
```
2021-11-03 15:49:39 +01:00
$ dep my_task -v all --limit 1
2021-11-03 13:42:44 +01:00
task my_task
[deployer.org] run whoami
[deployer.org] deployer
[medv.io] run whoami
[medv.io] deployer
```
It is also possible to specify a [limit level](tasks.md#limit) for each individual task.
By specifying the limit level for each task, you can control the degree of parallelism
for each part of your deployment process.
2021-11-03 13:42:44 +01:00
2022-09-06 09:17:58 +02:00
Each host has a configuration: a list of key-value pairs. Let's define our first
2021-11-03 13:42:44 +01:00
configuration option for both our hosts:
```php
host('deployer.org')
->set('my_config', 'foo');
host('medv.io')
->set('my_config', 'bar');
```
In the task we can get the currently executing host using the [currentHost](api.md#currenthost) function:
2021-11-03 13:42:44 +01:00
```php
task('my_task', function () {
$myConfig = currentHost()->get('my_config');
writeln("my_config: " . $myConfig);
});
```
Or with the [get](api.md#get) function:
2021-11-03 13:42:44 +01:00
```diff
task('my_task', function () {
2021-11-03 15:03:27 +01:00
- $myConfig = currentHost()->get('my_config');
+ $myConfig = get('my_config');
2021-11-03 13:42:44 +01:00
writeln("my_config: " . $myConfig);
});
```
Or via the [parse](api.md#parse) function which replaces the `{{ ... }}` brackets
and their enclosed values with the corresponding configuration option.
2021-11-03 13:42:44 +01:00
All functions (writeln, run, runLocally, cd, upload, etc) call the **parse** function
internally. So you don't need to call the **parse** function by yourself.
2021-11-03 13:42:44 +01:00
```diff
task('my_task', function () {
2021-11-03 15:03:27 +01:00
- $myConfig = get('my_config');
- writeln("my_config: " . $myConfig);
+ writeln("my_config: {{my_config}}");
2021-11-03 13:42:44 +01:00
});
```
Let's try to run our task:
```
2021-11-03 15:49:39 +01:00
$ dep my_task all
2021-11-03 13:42:44 +01:00
task my_task
[deployer.org] my_config: foo
[medv.io] my_config: bar
```
2022-09-06 09:17:58 +02:00
Awesome! Each host configuration inherits global configuration. Let's refactor
2021-11-03 13:42:44 +01:00
our recipe to define one global config option:
```php
set('my_config', 'global');
host('deployer.org');
host('medv.io');
```
2021-11-03 15:03:27 +01:00
The config option `my_config` will be equal to `global` on both hosts.
2021-11-03 14:35:24 +01:00
Additionally, the value of a config option can be defined as a callback.
This callback is executed upon its first access, and the returned result
is then stored in the host configuration.
2021-11-03 14:35:24 +01:00
2021-11-03 15:52:38 +01:00
```php
2021-11-03 14:35:24 +01:00
set('whoami', function () {
return run('whoami');
});
task('my_task', function () {
writeln('Who am I? {{whoami}}');
});
```
Let's try to run it:
```
2021-11-03 15:49:39 +01:00
$ dep my_task all
2021-11-03 14:35:24 +01:00
task my_task
[deployer.org] Who am I? deployer
[medv.io] Who am I? anton
```
We can use this to create a dynamic configuration which uses information from the current host.
Only the first call will trigger the callback execution. All subsequent checks use the previously
saved value.
2021-11-03 14:35:24 +01:00
Here is an example:
2021-11-03 15:52:38 +01:00
```php
2021-11-03 14:35:24 +01:00
set('current_date', function () {
return run('date');
});
task('my_task', function () {
writeln('What time is it? {{current_date}}');
run('sleep 5');
writeln('What time is it? {{current_date}}');
});
```
If we run my_task, we will see that `date` is called only once on
2021-11-10 23:28:29 +01:00
`{{current_date}}` access.
2021-11-03 14:35:24 +01:00
```
2021-11-03 15:49:39 +01:00
$ dep my_task deployer.org -v
2021-11-03 14:35:24 +01:00
task my_task
[deployer.org] run date
[deployer.org] Wed 03 Nov 2021 01:16:53 PM UTC
[deployer.org] What time is it? Wed 03 Nov 2021 01:16:53 PM UTC
[deployer.org] run sleep 5
[deployer.org] What time is it? Wed 03 Nov 2021 01:16:53 PM UTC
```
2021-11-03 15:00:01 +01:00
We can override a config option via CLI option `-o` like this:
2021-11-03 15:00:01 +01:00
```
2021-11-03 15:49:39 +01:00
$ dep my_task deployer.org -v -o current_date="I don't know"
2021-11-03 15:00:01 +01:00
task my_task
[deployer.org] What time is it? I don't know
[deployer.org] run sleep 5
[deployer.org] What time is it? I don't know
```
Since the `current_date` config option is overridden there is no need to call the callback.
2021-11-03 15:54:01 +01:00
So there is no 'run date'.