How to automate Appwrite Functions deployment with GitHub
Tags: webdev, devops, programming
Published: 2023-10-30 16:21:55


Cloud functions are powerful tools for developing applications and reducing server costs. [Appwrite Functions](https://appwrite.io/docs/products/functions?utm_source=hackmamba&utm_medium=hackmamba-blog), a cloud function service offered by the backend-as-a-service platform [Appwrite](https://appwrite.io/?utm_source=hackmamba&utm_medium=hackmamba-blog), offers the flexibility and efficiency for implementing serverless solutions effortlessly.

Appwrite functions must be deployed, but manual deployment can lead to insufficient version control, wasted time, inconsistent app environments, and exposure of secret environment variables, all of which can cause errors and security issues. Automating your deployments with [GitHub](https://github.com/) can help avoid these problems.

In this tutorial, you'll learn the benefits of automating Appwrite function deployments and explore the step-by-step process of automating your Appwrite function deployments using GitHub without manually configuring environment variables.

## Prerequisites

Before you dive into the automation process, you'll need to ensure you have the following prerequisites covered:

- **Basic knowledge of Node.js**: You should have a foundational understanding of [Node.js](https://nodejs.org/en), which is commonly used for developing serverless functions.
- **An Appwrite cloud account**: To use Appwrite functions, you need to have an active Appwrite cloud account. If you don't have one, sign up on the [Appwrite](https://cloud.appwrite.io/register?utm_source=hackmamba&utm_medium=hackmamba-blog) [website](https://appwrite.io/) to get started.
- **A basic knowledge of Git and a GitHub account:** You'll need a GitHub account to set up your repository. If you don't have one, sign up at [GitHub](https://github.com/).

The full project is in this [repository](https://github.com/trex013/seo-analyser.git).

## Setting up Appwrite and GitHub repository

Get started by creating a GitHub repository using these [instructions](https://docs.github.com/en/get-started/quickstart/create-a-repo).

Next, you’ll need to create a project within the Appwrite Cloud console. If you just signed up, you’ll be immediately prompted to create a project.

![A screenshot the project’s name field](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697495903781_Screenshot+2023-10-16+at+23.38.07.png)

If not, click on the **Create a new Project,** as indicated below:

![A screenshot showing the Create a new project](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697496058780_Screenshot+2023-10-16+at+18.04.22.png)

Once you've finished this step, you'll be able to see the dashboard for your project.

![A screenshot showing the project’s dashboard](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697497896659_Screenshot+2023-10-17+at+00.11.14.png)

## Creating your Appwrite Function on Appwrite

Next, you’ll need to create your function on the Appwrite console. To continue, click the **F****unctions** menu on the navigation bar.

![A screenshot showing the Functions menu](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697498219645_Screenshot+2023-10-17+at+00.16.15.png)

Afterward, click the **Create function** button indicated below.

![A screenshot showing the Create function button](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697498324491_Screenshot+2023-10-17+at+00.18.08.png)

Next, select the starter template you’d like to use. For this case, click **Node.js**.

![A screenshot showing Node.js selection](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697499044822_Screenshot+2023-10-17+at+00.29.56.png)

Enter the name of your function and select **Node.js - 18.0** as the runtime. Then, click **Next**.

![A screenshot showing the create function configuration screen](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697499231390_Screenshot+2023-10-17+at+00.33.06.png)

In the next step, Appwrite gives you the option to use an existing API key or generate a new one. To continue with this tutorial, select the **Generate API key on completion** checkbox, then click **Next**.

![A screenshot showing the section to select the generate API key section](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697499491001_Screenshot+2023-10-17+at+00.38.00.png)

Next, you can choose to use an existing repository or have Appwrite clone the template to a new one. Select **Create a new repository**, then click **Next**.

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697499985371_Screenshot+2023-10-17+at+00.45.16.png)

Once you've completed that step, you’ll need to connect your GitHub account to your Appwrite console. Click on the GitHub option as indicated below.

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697500595292_Screenshot+2023-10-17+at+00.56.26.png)

If you haven’t connected your GitHub account to Appwrite, you’ll be redirected to GitHub to do so.

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697504037271_Screenshot+2023-10-17+at+01.44.28.png)

Make sure to select the repository you initially created.

Once that’s done, enter the name of the new repository and click **Next**. This step specifies the name of the new repository Appwrite will create on your GitHub account. 

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697504143854_Screenshot+2023-10-17+at+01.49.09.png)

Next, select your desired Git branch and enter the root directory of your function.

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697504391953_Screenshot+2023-10-17+at+01.58.07.png)

At this point, the function has been successfully created and deployed on your Appwrite cloud account. Subsequent deployments will be triggered when you push changes to the GitHub repository Appwrite created for you.

![A screenshot showing the deployments](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697504746259_Screenshot+2023-10-17+at+02.05.35.png)

## Adding your Appwrite Function code

Now that you've created the Appwrite Function, it's time to update the template in the GitHub repository generated by Appwrite on your GitHub account.

> The GitHub repository created by Appwrite is usually a private repository. Therefore, it’s preferred that you use SSH to clone this repository. Check [here](https://docs.github.com/en/authentication/connecting-to-github-with-ssh) to configure SSH on your GitHub account.

To continue, go to your GitHub repository created by Appwrite, and then follow these [steps](https://docs.github.com/en/repositories/creating-and-managing-repositories/cloning-a-repository) to clone your repository locally. 

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697506533027_Screenshot+2023-10-17+at+02.35.18.png)

Once the repository is successfully cloned, open the projects folder with your code editor.

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697506676008_Screenshot+2023-10-17+at+02.37.43.png)

The **main.js** file is the entry point for the function, which means that this is where you should place your function code.

Open the **main.js** file and replace its contents with the following code. 

```js
import axios from "axios";
import cheerio from "cheerio";
async function getSEOInfo(url) {
  try {
    const response = await axios.get(url);
    const html = response.data;
    const $ = cheerio.load(html);
    const title = $("title").text();
    const headers = {};
    for (let i = 1; i <= 6; i++) {
      headers[`h${i}`] = $(`h${i}`).length;
    }
    const metaDescription = $("meta[name=description]").attr("content") || "";
    const metaKeywords = $("meta[name=keywords]").attr("content") || "";
    const images = $("img").length;
    const canonicalURL = $("link[rel=canonical]").attr("href") || "";
    const internalLinks = $('a[href^="/"]').length;
    const externalLinks = $('a[href^="http"]').length;
    return {
      title,
      headers,
      metaDescription,
      metaKeywords,
      images,
      canonicalURL,
      internalLinks,
      externalLinks,
    };
  } catch (error) {
    console.error("Error: ", error);
    return null;
  }
}
export default async ({ req, res, log, error }) => {
  if (req.method !== "GET") {
    return error("Request method not supported");
  }
  const info = await getSEOInfo(req.query.url);
  if (!info) return error("failed to get SEO information");
  log(info);
  return res.json(info);
};
```

The code above uses [axios](https://axios-http.com/) to fetch the content of a given URL and [cheerio](https://cheerio.js.org/?utm_source=hackmamba&utm_medium=hackmamba-blog) to extract the SEO information.

After that, run the following command to install the project’s package dependencies:

```sh
yarn add axios cheerio
```

## Triggering and testing the deployment

Once you're finished developing your function locally, you can deploy it by committing your changes to Git and pushing them to the GitHub repository.

![A screenshot showing the recent deployment](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697510032530_Screenshot+2023-10-17+at+03.33.12.png)

You’ll notice that a new deployment has been added to the list of deployments.

To test your deployment, click **Execute now**.

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697511034680_Screenshot+2023-10-17+at+03.49.30.png)

Next, add the parameters required by your function. In this case, you’re adding a query parameter `url` as indicated below.

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697512293273_Screenshot+2023-10-17+at+04.00.43.png)

Afterwards, click **Execute**.

![](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697512323137_Screenshot+2023-10-17+at+04.01.05.png)

Click on the execution to view more information about it.

![A screenshot showing the function execution](https://paper-attachments.dropboxusercontent.com/s_DEC51C919D08F06B07D387F9B1CBBA5C5EF4E02A421D225B287339C2F8AB7A6A_1697512407194_Screenshot+2023-10-17+at+04.01.55.png)

## Conclusion

Automating your Appwrite function deployments using this method is a great way to streamline your development and deployment workflows, free up valuable time, and improve efficiency while eliminating the need to add your Appwrite environment variables to GitHub.

## Resources

- [Appwrite Function documentation](https://appwrite.io/docs/products/functions?utm_source=hackmamba&utm_medium=hackmamba-blog)
- [Git](https://docs.github.com/)[H](https://docs.github.com/)[ub documentation](https://docs.github.com/)
- [Axios documentation](https://axios-http.com/)
- [Cheerio documentation](https://cheerio.js.org/?utm_source=hackmamba&utm_medium=hackmamba-blog)