# Deploy Strapi on Upsun Fixed

**Note**: 

Before you start, check out the [Upsun demo app](https://console.upsun.com/projects/create-project) and the main [Getting started guide](https://docs.upsun.com/get-started/here.md).
They provide all of the core concepts and common commands you need to know before using the materials below.

## Before you begin

You need:

- [Git](https://git-scm.com/downloads).
  Git is the primary tool to manage everything your app needs to run.
  Push commits to deploy changes and control configuration through YAML files.
  These files describe your infrastructure, making it transparent and version-controlled.
- A Upsun account.
  If you don't already have one, [register for a trial account](https://auth.upsun.com/register).
  You can sign up with an email address or an existing GitHub, Bitbucket, or Google account.
  If you choose one of these accounts, you can set a password for your Upsun account later.
- The [Upsun CLI](https://docs.upsun.com/administration/cli.md).
  This lets you interact with your project from the command line.
  You can also do most things through the [Web Console](https://docs.upsun.com/administration/web.md).

## 1. Create a Strapi project

To create your Strapi app, follow these steps.

1. Follow the Strapi [installation guide](https://docs.strapi.io/dev-docs/installation). 
   To fast track the process, run the following commands:

   ```bash  {location="Terminal"}
   npx create-strapi-app@latest my-strapi-project --quickstart --no-run
   ```

   **Note**: 

If the ``create-strapi-app`` script fails, run ``export SHARP_IGNORE_GLOBAL_LIBVIPS=true``,
remove the previous attempt (``rm -rf my-strapi-project``), and run the command again.

2. To initialize the local Git repository and commit local files, run the following commands:

   ```bash  {location="Terminal"}
   cd my-strapi-project
   echo "\nhttp:" >> .gitignore
   git add .
   git commit -m "Init Strapi application."
   ```

   **Note**: 

You can view the running app locally by running ``yarn develop``.
The local server will be visible at ``localhost:1337``.
You can create your first administrator user locally, but you will have to recreate that user once you’ve deployed to Upsun.

## 2. Create a new project

To create a project on Upsun,
follow these steps.

**Remember**: 

After creating your Upsun project, copy your new **project ID** for later use.

```bash {}
upsun project:create
```

**Note**: 

When creating a new project using the Upsun CLI command ``project:create``,
you are asked if you want to set the local remote to your new project. Enter **Yes (y)**.

Your local source code is automatically linked to your newly created Upsun project
through the creation of a ``.upsun/local/project.yaml``.
This file contains the corresponding ``<projectId>`` for the Upsun CLI to use,
and sets a Git remote to ``upsun``.

 - Create an organization or select an existing one.

 - Click **Create from scratch**.

 - Fill in details like the project name and [region](https://docs.upsun.com/development/regions.md).
**Note**: 

You can define resources for your project later on, after your first push.

 - To link your local source code to your new Upsun project,
run the following command:

    Terminal

```bash {}
upsun project:set-remote <projectId>
```

This command adds a new remote called ``upsun`` to your local Git repository,
which is equivalent to the following commands:

    Terminal

```bash {}
git remote
origin
upsun
```

It also creates a new ``.upsun/local/project.yaml`` file that contains the ``<projectId>``
for the ``upsun`` CLI to use.
**Tip**: 

If you forget your ``<projectId>``, run the following command and find your project in the list:

    Terminal

```bash {}
upsun project:list
```

## 3. Choose your Git workflow

You can use Upsun projects as a classic Git repository,
where you are able to push your source code in different ways,
using either the Git CLI or the Upsun CLI.
You can choose which way —or Git workflow— you want to use for your project from the following options:

- Your project source code is **hosted on an Upsun Git repository**
- Your project source code is **hosted on your own GitHub repository**

Upsun provides a [Github integration](https://docs.upsun.com/integrations/source/github.md) that allows your Upsun project to be fully integrated with your Github repository.
This enables you, as a developer, to use a normal Git workflow (``git add . && git commit -m "message" && git push``) to deploy your environment—with no need to connect to the Upsun Console.
**Note**: 

Make sure you complete the following steps before adding a [Github integration](https://docs.upsun.com/integrations/source/github.md):

 - Create a Git repository in your own organization following the relevant [Github repository creation guide](https://docs.github.com/en/repositories/creating-and-managing-repositories/creating-a-new-repository).

 - Create a [Github integration](https://docs.upsun.com/integrations/source/github.md).

 - Add a Git remote to your local project, from the root of your Strapi directory. 
To do so, run the following commands:

    Terminal

```bash {}
git remote add origin <urlOfYourOwnGitHubRepo>
git add . && git commit -m "init strapi"
git push origin
```

## 4. Configure your project

To host your Strapi application on Upsun,
you need to have a few YAML configuration files at the root of your project.
These files manage your app's behavior.
They are located in a `.upsun/` folder at the root of your source code
and structured in a similar way to this:

```txt {no-copy="true"}
my-strapi-project
├── .upsun
│   └── config.yaml
├── [.environment]
└── 
```

To generate these files, run the following command at the root of your project:

```  {location="Terminal"}
upsun project:init
```

Follow the prompts.

To commit your new files, run the following commands:

```bash  {location="Terminal"}
git add .
git commit -m "Add Upsun config files"
```

## 5. Deploy

And just like that, it’s time to deploy!

Depending on the Git workflow you chose at the beginning of this tutorial,
there are two ways to deploy your source code changes.

```bash {}
upsun push
```

When you choose to use a third-party Git hosting service, the Upsun Git
repository becomes a read-only mirror of the third-party repository. All your
changes take place in the third-party repository.
Add an integration to your existing third-party repository:

 - [BitBucket](https://docs.upsun.com/integrations/source/bitbucket.md)
 - [GitHub](https://docs.upsun.com/integrations/source/github.md)
 - [GitLab](https://docs.upsun.com/integrations/source/gitlab.md)

If you are using an integration, on each code updates,
use the normal Git workflow (``git add . && git commit -m "message" && git push``) to push your code to your external repository.
To do so, run the following command:

    Terminal

```bash {}
git push origin
```

Your GitHub, GitLab, or Bibucket integration process then automatically deploys changes to your environment.
If you’re pushing a new Git branch, a new environment is created.

Upsun then reads your configuration files,
and deploys your project using [default container resources](https://docs.upsun.com/manage-resources/resource-init.md).
If you don't want to use those default resources,
define your own [resource initialization strategy](https://docs.upsun.com/manage-resources/resource-init.md#specify-a-resource-initialization-strategy),
or [amend those default container resources](https://docs.upsun.com/manage-resources/adjust-resources.md) after your project is deployed.

Et voilà, your Strapi application is live!

**Tip**: 

Each environment has its own domain name.
To open the URL of your new environment, run the following command:

    Terminal

```bash {}
upsun environment:url --primary
```

## 6. Make changes to your project

Now that your project is deployed, you can start making changes to it.
For example, you might want to fix a bug or add a new feature.

In your project, the `main` branch always represents the production environment.
Other branches are for developing new features, fixing bugs, or updating the infrastructure.

To make changes to your project, follow these steps:

1. Create a new environment (a Git branch) to make changes without impacting production:

   ```bash  {location="Terminal"}
   upsun branch feat-a
   ```

   This command creates a new local `feat-a` Git branch based on the main Git branch,
   and activates a related environment on Upsun.
   The new environment inherits the data (service data and assets) of its parent environment (the production environment here).

2. Make changes to your project. 
   For example, you can create a new API and Collection (Content Type).
   To do so, run the following command:

   ```bash  {location="Terminal"}
   npm run strapi -- generate content-type
   ```

   Then follow the prompts to set up the `Blog` API containing an `Article` Content Type (Collection):

   ```bash {location="Suggested responses",}
   ? Content type display name Article
   ? Content type singular name article
   ? Content type plural name articles
   ? Please choose the model type Collection Type
   ? Use draft and publish? Yes
   ? Do you want to add attributes? Yes
   ? Name of attribute title
   ? What type of attribute string
   ? Do you want to add another attribute? Yes
   ? Name of attribute body
   ? What type of attribute richtext
   ? Do you want to add another attribute? Yes
   ? Name of attribute image
   ? What type of attribute media
   ? Choose media type Single
   ? Do you want to add another attribute? No
   ? Where do you want to add this model? Add model to new API
   ? Name of the new API? blog
   ? Bootstrap API related files? Yes
   ✔  ++ /api/blog/content-types/article/schema.json
   ✔  +- /api/blog/content-types/article/schema.json
   ✔  ++ /api/blog/controllers/article.js
   ✔  ++ /api/blog/services/article.js
   ✔  ++ /api/blog/routes/article.js
   ```

   Verify that the new `Article` collection has been created.
   To do so, run the local server (`yarn develop`) again,
   and visit
   [http://localhost:1337/admin/content-manager](http://localhost:1337/admin/content-manager).

   This results in the following changes:

   ```bash
   $ git status
   On branch feat-a
   Changes not staged for commit:
     (use "git add ..." to update what will be committed)
     (use "git restore ..." to discard changes in working directory)
	   modified:   types/generated/contentTypes.d.ts

   Untracked files:
     (use "git add ..." to include in what will be committed)
	   src/api/blog/

   no changes added to commit (use "git add" and/or "git commit -a")
   ```

3. Commit your changes:

   ```bash  {location="Terminal"}
   git add .
   git commit -m "Add Article collection"
   ```

4. Deploy your changes to the `feat-a` environment:

   ```bash  {location="Terminal"}
   upsun push
   ```

   **Tip**: 

Note that each environment has its own domain name.
To open the URL of your new environment, run the following command:

    Terminal

```bash {}
upsun environment:url --primary
```

5. Iterate by changing the code, committing, and deploying. 
   When satisfied with your changes, merge them to the main branch,
   and remove the feature branch:

   ```bash  {location="Terminal"}
   upsun merge
       Are you sure you want to merge feat-a into its parent, main? [Y/n] y
   upsun checkout main
   git pull upsun main
   upsun environment:delete feat-a
   git fetch --prune
   ```

   Note that deploying to production is fast because the image built for the `feat-a` environment is reused.

   For a long running branch, to keep the code up-to-date with the main branch, use `git merge main` or `git rebase main`.
   You can also keep the data in sync with the production environment by using `upsun env:sync`.

## Further resources

### Documentation

- [JavaScript/Node.js documentation](https://docs.upsun.com/languages/nodejs.md)
- [Managing dependencies](https://docs.upsun.com/languages/nodejs.md#dependencies)
- [Add a database to Strapi](https://docs.upsun.com/get-started/stacks/strapi/add-database.md)

### Community content

- [Strapi topics](https://support.platform.sh/hc/en-us/search?utf8=%E2%9C%93&query=strapi)
- [Node.js topics](https://support.platform.sh/hc/en-us/search?utf8=%E2%9C%93&query=node)
- [JavaScript topics](https://support.platform.sh/hc/en-us/search?utf8=%E2%9C%93&query=js)

### Blogs

- [Up(sun) and running with Strapi: a seamless journey to a powerful decoupled headless CMS](https://upsun.com/blog/strapi-deployment-on-upsun/)

