Envoy deployment tool


Writing Task

  • Configuration
  • Stories
  • Slack
    • Discord
    • ##

      Introduction

      Laravel Envoy provides a set of concise and lightweight syntax for defining daily tasks of remote servers. Blade style syntax can be used to configure deployment tasks, execute Artisan commands, etc. Currently, Envoy only supports Mac and Linux operating systems. .

      Installation

      First, run Composer’s global require command to install Envoy globally. :

      composer global require laravel/envoy

      Since global Composer libraries can sometimes cause package version conflicts, you may want to consider using cgr, which is a drop-in replacement for the composer global require command. cgr The installation instructions of the library can be found found on GitHub.

      Make sure to put the ~/.composer/vendor/bin directory In the PATH so that the envoy executable is found when running the envoy command in the terminal.

      Updating Envoy

      You can also use Composer to keep your Envoy installation up to date. Using the composer global update command will update all globally installed Composer packages:

      composer global update

      ##Writing tasks

      All your Envoy tasks should be defined in the

      Envoy.blade.php file in the root directory of your project. Here's an example to get you started:

      @servers(['web' => ['user@192.168.1.1']])
      @task('foo', ['on' => 'web'])
          ls -la
      @endtask

      As you can see, there is a

      @server array defined at the top of the file, allowing you to on These servers are referenced in the options. In your @ task declaration, you should place the Bash code that should run on your server when the task executes.

      You can force the script to run locally by specifying the server's IP address as

      127.0.0.1:

      @servers(['localhost' => '127.0.0.1'])

      Configuration

      Sometimes, you may need to execute some PHP code before executing the Envoy task. You can use the

      @ setup directive to declare variables and perform other general PHP work before performing any other tasks:

      @setup
          $now = new DateTime();    
          $environment = isset($env) ? $env : "testing";
      @endsetup

      If you need additional PHP files before performing a task, you can do so in

      Use the @include directive at the top of the Envoy.blade.php file:

      @include('vendor/autoload.php')
      @task('foo')   
       # ...
      @endtask

      ##Variables

      If desired, you can use the command line to pass option values ​​to the Envoy task:

      envoy run deploy --branch=master

      You can access options in the task through Blade's "echo" syntax. You can also use

      if

      statements and loops within tasks. For example, before executing the git pull command, let's verify the existence of the $ branch variable:

      @servers(['web' => '192.168.1.1'])
      @task('deploy', ['on' => 'web'])
          cd site
      
          @if ($branch)
              git pull origin {{ $branch }}
          @endif
      
          php artisan migrate
      @endtask

      Stories

      Stories Group a set of tasks under a convenient name, allowing you to group small, focused tasks into larger tasks. For example, the deploy story can run the git and composer tasks by listing the task name in its definition:

      @servers(['web' => '192.168.1.1'])
      @story('deploy')
          git
          composer
      @endstory
      
      @task('git')
          git pull origin master
      @endtask
      
      @task('composer')
          composer install
      @endtask

      Once the story is written , you can run it like a typical task:

      envoy run deploy

      ##Multiple Servers

      Envoy allows you to easily span Multiple servers run tasks. First, add additional servers in the

      @server declaration. Each server should be assigned a unique name. After defining the other servers, list each server in the "on" array of the task:

      @servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2'])
      @task('deploy', ['on' => ['web-1', 'web-2']])
          cd site
          git pull origin {{ $branch }}
          php artisan migrate
      @endtask

      Run in parallel

      By default, each server will be Execute tasks serially. In other words, the task will finish running on the first server before continuing on the second server. If you want to run tasks on multiple servers in parallel, add the

      parallel option to the task declaration:

      @servers(['web-1' => '192.168.1.1', 'web-2' => '192.168.1.2'])
      @task('deploy', ['on' => ['web-1', 'web-2'], 'parallel' => true])
          cd site
          git pull origin {{ $branch }}
          php artisan migrate
      @endtask

      Run a task

      To run a task or story defined in the

      Envoy.blade.php file, execute Envoy's run command, passing the The name of the quest or story. When the task runs, Envoy will run the task and display the output from the server:

      envoy run task

      Task Confirmation

      If you If you want to be prompted for confirmation before running a given task on the server, you should add the

      confirm directive to the task declaration. This option is particularly useful for destructive operations:

      @task('deploy', ['on' => 'web', 'confirm' => true])
          cd site
          git pull origin {{ $branch }}
          php artisan migrate
      @endtask

      Message Notification

      Slack

      Envoy also supports sending notifications to

      Slack after each task is executed. @slack The directive accepts a Slack hook URL and channel name. You can retrieve your webhook URL by creating an Incoming WebHooks integration in your Slack dashboard. You should pass the entire webhook URL to the @slack directive:

      @finished
          @slack('webhook-url', '#bots')
      @endfinished

      You can provide one of the following as a channel parameter:

        to the channel Send notification:
      • #channel
      • Send notification to user:
      • @user

      ##

      Discord

      Envoy also supports sending notifications to Discord after each task is performed. @ discord The directive accepts Discord hook URLs and messages. You can retrieve your webhook URL by creating a webhook in Webhook and selecting the channel to which the webhook should be published. You should pass the entire webhook URL to the @ discord command:

      @finished
          @discord('discord-webhook-url')
      @endfinished
      This article first appeared on the LearnKu.com website.