# Rasa Action Server Deployment Documentation

You are viewing documentation for our open source project which is maintained by the community. If you want to get started building assistants with Rasa please check out our latest [documentation here](/content/docs/index.html).

The [Rasa Action Server chart](https://github.com/RasaHQ/helm-charts/tree/main/charts/rasa-action-server) is open source and available in the [helm-charts repository](https://github.com/rasahq/helm-charts). Please [create an issue](https://github.com/RasaHQ/helm-charts/issues/new) in this repository if you discover bugs or have suggestions for improvements.

## Installation Requirements
1. Check that you have installed the Kubernetes or OpenShift command line interface (CLI). You can check this using the following command:
   
   ```bash
   kubectl version --short --client
   ```
   
   The output should be similar to this:
   ```
   Client Version: v1.19.11
   ```
   If this command resulted in an error, please install the [Kubernetes CLI](https://kubernetes.io/docs/tasks/tools/install-kubectl/) or the [OpenShift CLI](https://docs.openshift.com/container-platform/4.7/cli_reference/openshift_cli/getting-started-cli.html#installing-openshift-cli) depending on the cluster you’re using.

2. Make sure that the Kubernetes / OpenShift CLI is correctly connected to your cluster. You can do so by using the following commands:
   
   ```bash
   kubectl version --short
   ```
   
   The output should be similar to this:
   ```
   Client Version: v1.19.11
   Server Version: v1.19.10
   ```
   If you get an error when executing the command, you are not connected to your cluster. To get the command to connect to the cluster please consult your cluster’s admin or the documentation of your cloud provider.

3. Make sure you have the [Helm CLI](https://helm.sh/docs/intro/install/) installed. To check this, run:
   
   ```bash
   helm version --short
   ```
   
   The output should be similar to this:
   ```
   v3.6.0+g7f2df64
   ```
   If this command leads to an error, please install the [Helm CLI](https://helm.sh/docs/intro/install/).
   In case you are using a version `<3.5` of Helm, please update to Helm version `>=3.5`.

## 1. Installation
### a. Create Namespace
We recommend installing Rasa Action Server in a separate [namespace](https://kubernetes.io/docs/concepts/overview/working-with-objects/namespaces/) to avoid interfering with existing cluster deployments. To create a new namespace run the following command:
   
   ```bash
   kubectl create namespace <your namespace>
   ```

### b. Deploy Rasa Action Server
Run the following commands:
   
   ```bash
   # Add the repository which contains the Rasa Action Server Helm chart
   helm repo add rasa https://helm.rasa.com
   
   # Deploy Rasa Action Server
   helm install --namespace <your namespace> <release name> rasa/rasa-action-server
   ```

### c. Access Rasa Action Server
By default the Rasa Action Server deployment is exposed via the `rasa-action-server` (`<release name>`) service and accessible only within a Kubernetes cluster. You can get the IP address using this command:
   
   ```bash
   export SERVICE_PORT=$(kubectl get --namespace <your namespace> -o jsonpath="{.spec.ports[0].port}" services <release name>)
   kubectl port-forward --namespace <your namespace> svc/<release name> ${SERVICE_PORT}:${SERVICE_PORT}
   ```
   
   You can then access the deployment on `http://127.0.0.1:${SERVICE_PORT}`.

## Building an Action Server Image
If you build an image that includes your action code and store it in a container registry, you can run it as part of your deployment. In addition, you can add any additional dependencies of systems or Python libraries that are part of your action code but not included in the base `rasa/rasa-sdk` image.

### Automating your Action Server Image Builds
In addition to a manually creating a new Action Server image, you can use the [Rasa Action Server GitHub Action](https://github.com/RasaHQ/action-server-gha) to automate image builds. If GitHub Actions are new to you, it might be helpful to get familiar with [GitHub Actions Documentation](https://docs.github.com/en/actions).

To create a workflow for building and pushing a Docker image into a DockerHub registry:

1. Add GitHub Secrets with your DockerHub login name and password. You can find details on how to create encrypted secrets for a repository in the [Github docs](https://docs.github.com/en/actions/configuring-and-managing-workflows/creating-and-storing-encrypted-secrets#creating-encrypted-secrets-for-a-repository).
   
   The example uses the following secrets:
   - `DOCKER_HUB_LOGIN` - a login name for DockerHub
   - `DOCKER_HUB_PASSWORD` - a password for DockerHub

2. In your GitHub repository create a file [`.github/workflows/action_server.yml`](https://github.com/RasaHQ/action-server-gha/blob/master/examples/action_server.yml).

```yaml
on:
   push:
     branches:
       - main
     paths:
       - 'actions/**/*'

jobs:
  build_and_deploy:
    runs-on: ubuntu-latest
    name: Build Action Server image and upgrade Rasa X/Enterprise deployment
    steps:
      - name: Checkout repository
        uses: actions/checkout@v2
      - id: action_server
        name: Build an action server with custom actions
        uses: RasaHQ/action-server-gha@main
        with:
          docker_image_name: 'account_username/repository_name'
          docker_registry_login: ${{ secrets.DOCKER_HUB_LOGIN }}
          docker_registry_password: ${{ secrets.DOCKER_HUB_PASSWORD }}
          docker_image_tag: ${{ github.sha }}
```

3. Push your changes to the `main` branch. After changes are pushed, the workflow will build and push a new image into the DockerHub registry.

4. Now, you can use your new brand docker image.

5. You can also extend your workflow, so that you do not have to manually update your Rasa X/Enterprise deployment.

### Manually Building an Action Server
To create your image:
1. Make sure your actions are defined in `actions/actions.py`. The `rasa/rasa-sdk` image will automatically look for the actions in this file.

2. If your actions have any extra dependencies, create a list of them in a file, `actions/requirements-actions.txt`.

3. Create a file named `Dockerfile` in your project directory, in which you'll extend the official SDK image, copy over your code, and add any custom dependencies (if necessary). For example:
   
   ```dockerfile
   # Extend the official Rasa SDK image
   FROM rasa/rasa-sdk:3.6.2
   
   # Use subdirectory as working directory
   WORKDIR /app
   
   # Copy actions folder to working directory
   COPY ./actions /app/actions
   
   # By best practices, don't run the code with root user
   USER 1001
   ```

You can then build the image via the following command:
   
   ```bash
   docker build . -t <account_username>/<repository_name>:<custom_image_tag>
   ```

### Using your Custom Action Server Image
If you're building this image to make it available from another server, you should push the image to a cloud repository. This documentation assumes you are pushing your images to [DockerHub](https://hub.docker.com/).

You can push the image to DockerHub via:
   
   ```bash
   docker login --username <account_username> --password <account_password>
   docker push <account_username>/<repository_name>:<custom_image_tag>
   ```

## Setting a Custom Action Server Image
In order to use a Custom Action Server image along with the Rasa Action Server deployment, you have to use the following values for your deployment.
   
   ```yaml
   image:
     name: "image_name"
     tag: "image_tag"
   ```

then upgrade your deployment by executing the command:
   
   ```bash
   helm upgrade --namespace <namespace> --reuse-values -f values.yaml <release name> rasa/rasa-action-server
   ```

## 2. Connect Rasa Action Server to a Rasa deployment
If you have deployed your assistant using the Rasa Helm chart, and you have deployed your Rasa Action Server as well. Now it's time to connect them together. You can do this easily by following the steps:

a. Create a `rasa-values.yaml` file which will include configuration for the Rasa deployment.
   
   ```yaml
   rasa-action-server:
     external:
       enabled: true
       url: "http://rasa-action-server/webhook"
   ```

b. Upgrade the Rasa deployment
   
   ```bash
   helm upgrade -n <namespace> --reuse-values -f rasa-values.yaml <release name> rasa/rasa
   ```
