How do I create a custom rule action? I have drupal 8, and I want to create a custom rule action. I have created it in Drupal 7, and it's working fine. I have no idea how to do it in Drupal 8.

Can you please explain which code should I use for Drupal 8?

Drupal 7 Code

function custom_rules_action_info() {
    return array(
        'custom_action1' =&gt; array(
            'label' =&gt; t('custom_action1'),
            'group' =&gt; t('Custom'),
            'parameter' =&gt; array(
                'user_id' =&gt; array(
                    'type' =&gt; 'integer',
                    'label' =&gt; t('User ID'),
                    'description' =&gt; t('User ID.'),
                ),
            ),
            'provides' =&gt; array(
                'return_value' =&gt; array(
                    'type' =&gt; 'text',
                    'label' =&gt; t('User Created with return_value'),
                ),
            ),
        ),
    );
}
Check the actions of the Rules module:

http://cgit.drupalcode.org/rules/tree/src/Plugin/RulesAction?h=8.x-3.x

These are the actions provided out of the box by Rules.

Each action class has a @RulesAction annotation. Wire that up and you should see your Rules action appear. Think of the annotation here very similar to what you would have in hook_rules_action_info().

The only exception is instead of defining it in the .module file, it will live in mymodule/src/Rules/Plugin/RulesAction/NameOfYourAction.php.

Here is an example of what it might look like:

namespace Drupal\mymodule\Plugin\RulesAction;

use Drupal\user\UserInterface;
use Drupal\rules\Core\RulesActionBase;

/**
 * Provides a 'custom action' action.
 *
 * @RulesAction(
 *   id = "mymodule_custom_action",
 *   label = @Translation("Custom Action"),
 *   category = @Translation("Custom"),
 *   context = {
 *     "user" = @ContextDefinition("entity:user",
 *       label = @Translation("User"),
 *       description = @Translation("Specifies the user we are taking action on.")
 *     ),
 *   }
 * )
 *
 */
class NameOfYourAction extends RulesActionBase {

  /**
   * Flag that indicates if the entity should be auto-saved later.
   *
   * @var bool
   */
  protected $saveLater = FALSE;

  /**
   * Does something to the user entity.
   *
   * @param \Drupal\user\UserInterface $account
   *   The user to take action on.
   */
  protected function doExecute(UserInterface $account) {
    // execution code
    // you may want to set $this-&gt;saveLater based on your use case
  }

  /**
   * {@inheritdoc}
   */
  public function autoSaveContext() {
    if ($this-&gt;saveLater) {
      return ['user'];
    }
    return [];
  }

}


Please note that at the time of this writing, Rules is in alpha release state. It may change with subsequent releases.