Set up CI/CD

When you are rapidly iterating on an application, deploying it manually repeatedly can become tedious. Also, everybody working on the codebase then needs to know how to deploy. To streamline this, you can setup CI/CD pipeline which can automate application deployments on TrueFoundry based on specific triggers, such as image creation or commit merging to the main branch.

The specific CI/CD configuration approach depends on the deployment scenario:

Scenario 1: You have deployed your Service directly from Github by integrating the Github repo in Truefoundry

This will be the case if you have deployed your service using the github repository dropdown.

Scenario 2: You have deployed your Service using the Python SDK

In cases where your Service has been deployed using the Python SDK, you can configure your CI/CD pipeline to trigger a build and deploy when a specific event occurs such as when a commit is pushed to the main branch. In this case the deployment will happen using the python deploy.py file

Scenario 3: Custom CI/CD Pipeline with External Image Build

If you've established your own CI/CD pipeline and are using an external system for image building, you can configure a deployment to be triggered whenever your Image gets built. This approach offers flexibility and integration with your existing CI/CD setup.

Scenario 1: Service is deployed from a GitHub Repository using Github Integration

You can follow the instructions in the following guide to setup GitHub Actions for CI/CD:

Scenario 2: Deployed your Service using the Python SDK

To configure your CI/CD pipeline to trigger a build and deploy when a specific event occurs such as when a commit is pushed to the main branch, you can follow the instruction below:

  1. Configure Environment Variables:

    In your CI/CD pipeline's CD (Continuous Delivery) stage, add the following environment variable as a secret:

    • TFY_API_KEY: Set this to your TrueFoundry API Key. You can generate one from the TrueFoundry dashboard or by following the instructions here.
  2. Install servicefoundry:

    Add a step in your CI/CD pipeline's build stage to install the servicefoundry CLI tool using the following command:

    pip install servicefoundry
    
  3. Deploy using the deploy.py file:

    Add a step in your CI/CD pipeline's CD stage to build and deploy your code to TrueFoundry using the following command:

    python deploy.py
    

For example consider this GitHub Actions CI/CD:

name: Deploy to TrueFoundry

on:
  push:
    branches:
      - main

env:
  TFY_API_KEY: ${{ secrets.TFY_API_KEY }}

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      - name: Install servicefoundry
        run: pip install servicefoundry
      - name: Check out repository code
        uses: actions/checkout@v3
      - name: Deploy
        run: python deploy.py # Add your Deployment Configuration in deploy.py

Scenario 3: Custom CI/CD Pipeline with External Image Build

When deploying a Docker image to TrueFoundry using an external CI/CD pipeline, you utilize TrueFoundry only for the deployment stage, while handling the build process yourself. This involves integrating your CI/CD pipeline with TrueFoundry's CLI tool. To do so, you will have to do these things:

  1. Configure Environment Variables:

    In your CI/CD pipeline's CD (Continuous Delivery) stage, add the following environment variables:

    • TFY_HOST: Set this to the URL of your TrueFoundry control plane. This is typically in the format <your-org-name>.truefoundry.cloud or <your-org-name>.truefoundry.com.

    • TFY_APPLICATION_FQN: Set this to the Application FQN (Fully Qualified Name) of the service you want to deploy to. The format is typically tfy-eus-demo:your-target-workspace:your-service.

    • TFY_API_KEY: Set this to your TrueFoundry API Key. You can generate one from the TrueFoundry dashboard or by following the instructions here. You might also want to wrap this value in a Secret.

  2. Install servicefoundry:

    Add a step in your CI/CD pipeline's build stage to install the servicefoundry CLI tool using the following command:

    pip install servicefoundry
    
  3. Deploy with Patch:

    Add a step in your CI/CD pipeline's CD stage to deploy the Docker image to TrueFoundry using the following command:

    sfy patch-application --application_fqn=tfy-eaus-demo:your-target-workspace:your-service --patch='{"image": {"image_uri": "your-image-uri"}}'
    

    Explanation of the Deployment Command:

    • The sfy patch-application command is used to update an existing application with new image or build source information.

    • The --patch flag specifies the JSON patch to apply. In this case, the patch updates the application's image property with the URI of the Docker image to be deployed.

    • your-cluster-name:your-target-workspace:your-service: This is the Application FQN of the service you want to deploy to.

    • {"image": {"image_uri": "your-image-uri"}}: This JSON patch sets the image_uri property to the value of the Image URI provided from the Pipeline

For example consider this GitHub Actions CI/CD:

name: Deploy to TrueFoundry

on:
  push:
    branches:
      - main

env:
  TFY_HOST: <YOUR_TFY_HOST>
  TFY_APPLICATION_FQN: <YOUR_APPLICATION_FQN>
  TFY_API_KEY: ${{ secrets.TFY_API_KEY }}

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
      // Add your steps to provide the `IMAGE_URI` to be deployed as env
    	...
      - name: Get image URI
        run: echo "IMAGE_URI={value}" >> "$GITHUB_ENV"
      - name: Install servicefoundry
        run: pip install servicefoundry
      - name: Deploy with patch
        run: sfy patch-application --application_fqn=${{ env.TFY_APPLICATION_FQN }} --patch='{"image": {"image_uri": "${{ env.IMAGE_URI }}"}}'