Automated Deployments#

This guide describes how to automate deployments to CodeRed Cloud using the command line tool.

GitHub Actions#

An example GitHub project which deploys a Django app is available here: https://github.com/coderedcorp/demo-django

Create a workflow file#

The first step is to create a file .github/workflows/action.yml in your project folder. This file can be named anything, as long as it is in the .github/workflows/ folder.

Within a GitHub Action, activities are organized as Jobs which contain Steps (usually a “uses” or “run”).

The recommended setup is to create two Jobs: the first to check and test your project code, the second to perform the CodeRed Cloud deployment. This way, if there is an error in the code, the site will not be deployed.

Below is an example .github/workflows/action.yml file which will deploy from the main git branch. If the pipeline is run on a different branch, the deployment will be skipped. Replace WEBAPP below with your webapp handle.

# This action will be run on the `main` branch
# or on any pull requests going into the `main` branch.
on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main
jobs:
  # TODO: add a job here which runs checks and unit tests.
  # test:
  #   ...

  deploy:
    name: Deploy to CodeRed Cloud
    # Only deploy if the `test` job succeeded (uncomment the line below).
    # needs: test
    # Only run in the `main` branch (i.e. not pull requests).
    if: github.ref == 'refs/heads/main'
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v3

      # If you need any build steps such as `npm run` or `yarn build`
      # Add them here before the deployment.

      # CodeRed Cloud command line tool.
      # See: https://www.codered.cloud/docs/cli/
      - name: Install cr tool
        run: |
          wget -nv -O /usr/local/bin/cr "https://www.codered.cloud/cli/cr-linux"
          chmod +x /usr/local/bin/cr
      # Run the deployment.
      - name: Deploy
        run: cr deploy WEBAPP
        env:
          CR_TOKEN: ${{ secrets.cr_token }}

Set your API Token#

Follow the GitHub docs to create a secret named cr_token which contains your CodeRed Cloud API key.

The action should now be fully set up. Change and push some code to trigger it.

GitLab Pipelines#

An example GitLab project which deploys a Django app is available here: https://gitlab.com/coderedcorp/demo-django-gitlab

Create a YAML file#

The first step is to create a file .gitlab-ci.yml in your project folder.

Within a GitLab Pipeline, activities are organized into Stages, which contain Jobs, which contain Scripts.

The recommended setup is to create two Stages: the first to check and test your project code, the second to perform the CodeRed Cloud deployment. This way, if there is an error in the code, the site will not be deployed.

Below is an example .gitlab-ci.yml file which will deploy from the main git branch. If the pipeline is run on a different branch, the deployment will be skipped. Replace WEBAPP below with your webapp handle.

stages:
  # - test
  - deploy

# TODO: add a job here which runs checks and unit tests.
# test-job:
#   stage: test
#   ...

deploy-job:
  stage: deploy
  image: ubuntu:latest
  # Only run in the `main` branch (i.e. not pull requests).
  rules:
    - if: $CI_COMMIT_REF_NAME == "main"
  script:

    # If you need any build steps such as `npm run` or `yarn build`
    # Add them here before the deployment.

    - apt-get -qq update && apt-get -qq install wget
    # CodeRed Cloud command line tool.
    # See: https://www.codered.cloud/docs/cli/
    - |
      wget -nv -O /usr/local/bin/cr "https://www.codered.cloud/cli/cr-linux"
      chmod +x /usr/local/bin/cr
    # Run the deployment.
    - cr deploy WEBAPP

Set your API Token#

Follow the GitLab docs to create a variable named CR_TOKEN which contains your CodeRed Cloud API key.

Be sure to check Protect variable and Mask variable. This is to avoid a malicious pull request from leaking the variable and obtaining its value.

The action should now be fully set up. Change and push some code to trigger it.

Azure DevOps#

Create a YAML file#

The first step is to create an azure-pipelines.yml file in your project folder.

Within an Azure pipeline, activities are organized hierarchically into Stages, which contain Jobs, which contain Steps (usually a “task” or “script”). Stages are run sequentially; if one stage fails the pipeline will stop. Jobs within a stage are run in parallel, on separate machines, so each job should be unrelated to the others. Finally, Steps within a Job are run sequentially.

The recommended setup is to create two Stages: the first to check and test your project code, the second to perform the CodeRed Cloud deployment. This way, if there is an error in the code, the site will not be deployed.

Below is an example azure-pipelines.yml file which will deploy from the main git branch. If the pipeline is run on a different branch, the deployment will be skipped. Replace WEBAPP below with your webapp handle.

# This pipeline will be run on the `main` branch
# or on any pull requests going into the `main` branch.
trigger:
- main

stages:
# TODO: add a stage here which runs checks and unit tests.
# - stage: test
#   ...

- stage: deploy
  displayName: Deploy to CodeRed Cloud
  # Only run if the previous stages succeeded,
  # and in the `main` branch (i.e. not pull requests).
  condition: and(succeeded(), eq(variables['build.sourceBranch'], 'refs/heads/main'))
  jobs:
  - job: cr
    displayName: Deploy
    pool:
      vmImage: 'ubuntu-latest'
    steps:

    # If you need any build steps such as `npm run` or `yarn build`
    # Add them here before the deployment.

    # CodeRed Cloud command line tool.
    # See: https://www.codered.cloud/docs/cli/
    - script: |
        wget -nv -O /usr/local/bin/cr "https://www.codered.cloud/cli/cr-linux"
        chmod +x /usr/local/bin/cr
      displayName: Install cr tool
    # Run the deployment.
    - script: cr deploy WEBAPP
      displayName: Deploy
      env:
        CR_TOKEN: $(cr_token)

Be sure to commit the file and push to your git repo.

Create the Pipeline#

Next, create the Pipeline from the Azure DevOps web portal.

  1. Log in to Azure DevOps and navigate to your projet.

  2. Click the Pipelines link in the sidebar then click New Pipeline.

  3. Select Azure Repos Git (you may also use any other git source if you happen to be hosting your git repo elsewhere). Then select your repo.

  4. Under “Configure your pipeline” select Existing Azure Pipelines YAML file.

  5. Select your main branch, then /azure-pipelines.yml as the path. Click Continue.

Set your API token#

Follow the Azure docs to create a secret variable named cr_token which contains your CodeRed Cloud API key.

The pipeline should now be fully set up. Run the pipeline manually, or change and push some code to trigger it.

Other (shell scripts)#

Deployments can be automated using simple shell scripts.

First, set the CR_TOKEN environment variable to your CodeRed Cloud API token.

Replace WEBAPP below with your webapp handle.

# CodeRed Cloud command line tool.
# See: https://www.codered.cloud/docs/cli/
wget -nv -O /usr/local/bin/cr "https://www.codered.cloud/cli/cr-linux"
chmod +x /usr/local/bin/cr

# Run the deployment.
cr deploy WEBAPP