Skip to Content
Technical Articles

DevOps: CAP CI/CD with GitHub Actions

This post will show you how to get started with GitHub Actions to build & deploy a CAP application on SCP Cloud Foundry.

GitHub Actions

There are many different solutions to implement Continuous Integration and Continuous Delivery/Deployment for CAP applications. You could use Azure DevOps (nice series from Jhodel Cailan), the new CI/CD Service on SCP or Project Piper for instance. However if you already use GitHub, GitHub Actions might be a good solution as everything is integrated and it’s very easy to get started.

Prerequisites

  • SAP Business Application Studio or Visual Studio Code
  • SAP Cloud Platform Account
  • GitHub Account

Initial Setup

Before you can use GitHub Actions, you need a basic CAP application. You can follow the instructions below to create the app or just clone my repository. I won’t go into the details in this post as the CAP documentation was updated recently and will help you understand every step.

Create a New CAP Project

So first create a new CAP project with some sample code and check everything works properly with cds watch:

cds init cap-github-actions --add samples
cds watch

 

Enhance Project Configuration for SAP HANA Cloud

In order to deploy the application to SCP CF later, you need to modify the package.json file to add the configuration for SAP HANA (Cloud) :

{
  "cds": {
    "requires": {
      "db": {
        "kind": "sql"
      }
    },
    "hana": {
      "deploy-format": "hdbtable"
    }
  }
}

and add the hdb driver for SAP HANA as a dependency to the project:

npm add hdb --save

 

Build and Deploy from your local machine

Before using GitHub Actions to build and deploy the application, you can do it locally to check everything is fine.

Add the MTA deployment descriptor file (mta.yaml)

In order to deploy using MTA you need to add an mta.yaml file:

cds add mta

 

Build the MTA

Then the Cloud MTA Build Tool can be used to build the MTA:

npm install -g mbt
mbt build -p cf

 

Deploy the MTA to Cloud Foundry

Finally the MTA can be deployed to SCP CF:

cf login
cf deploy mta_archives/cap-github-actions_1.0.0.mtar

 

Build and Deploy Using GitHub Actions

Now that you a simple CAP app and you can build and deploy it from a local machine, let’s see how to do the same with GitHub Actions.

Encrypted Secrets

As you don’t want to expose the details of your Cloud Foundry account (organization, space and so on) you first need to create some encrypted secrets in GitHub. This can be done in the repository Settings on GitHub:

Create GitHub Workflow

Now you need to create a GitHub Workflow to build and deploy the project to Cloud Foundry when an event occurs (i.e push to master branch for instance). You don’t have to build everything from scratch and you can just reuse an existing action to deploy to CF.

To create a new Workflow, just click on the Actions tab and then on the setup a workflow yourself link:

This will open an editor where we can define our own workflow. Replace the content of the editor with the following code:

# Build Multitarget Application & Deploy it to Cloud Foundry
name: Build MTA & Deploy it to CF

# Controls when the action will run. Triggers the workflow on push or pull request
# events but only for the master branch
on:
  push:
    branches: [ master ]

# A workflow run is made up of one or more jobs that can run sequentially or in parallel
jobs:
  build:
    runs-on: ubuntu-latest
    steps:
    - uses: actions/checkout@v2
    - name: Use Node.js
      uses: actions/setup-node@v1
      with:
        node-version: 12
    - name: Install MTA Build Tool
      run: npm install -g mbt
    # Do not generate package-lock.json
    # See https://blogs.sap.com/2020/07/07/escaping-eintegrity-hell/
    - name: Disable package-lock.json
      run: npm config set package-lock false
    - name: Build MTA
      run: mbt build -p cf
    - name: Upload Artifact
      uses: actions/upload-artifact@master	
      with:	
        name: mta
        path: ./mta_archives/cap-github-actions_1.0.0.mtar
        
  deploy:
     needs: build
     runs-on: ubuntu-latest
     steps:
       - name: Download Artifact
         uses: actions/download-artifact@master
         with:
           name: mta
           path: ./
       - name: Deploy to Cloud Foundry
         uses: guerric-p/cf-cli-action@master
         with:
           cf_api: ${{ secrets.CF_API }}
           cf_username: ${{ secrets.CF_USERNAME }}
           cf_password: ${{ secrets.CF_PASSWORD }}
           cf_org: ${{ secrets.CF_ORG }}
           cf_space: ${{ secrets.CF_SPACE }}
           command: deploy ./cap-github-actions_1.0.0.mtar -f

This simple workflow contains 2 jobs, one to build the app and one to deploy the MTA to CF. There are multiple steps in each job to replicate everything you did locally using the GitHub infrastructure when code is pushed to the master branch.

Note:

There’s a temporary fix in the workflow to prevent EINTEGRITY errors. I believe this is due to the fact that SCP has not switched yet to the public npm repository (please read the following post from DJ Adams).

Test the Workflow

Now you just need to push to the master branch to trigger the new workflow and to automatically build & deploy the application.

You should see the status of the workflow on GitHub:

and you can also navigate to the details:

Closing

This post just gives you the basics to get started, there are many things you can do with GitHub Actions:

  • trigger workflow with various events: push, issue creation, pull request creation etc.
  • use your own self-hosted runners if the provided runners don’t meet your needs
  • re-use workflows and actions provided by the community

If you have any experience with GitHub Actions or other CI/CD solutions please do share your thoughts!

4 Comments
You must be Logged on to comment or reply to a post.
  • Good read Pierre, short question: would you like to use piper if it could run in the context of GH actions?

    (Disclaimer: I’m Product Owner for some of the project “Piper” scenarios and the SAP Cloud Platform CI CD Service)

     

    Christoph

    • Hi Christoph,

      It could be an option and AFAIK there is already a GH action for Piper (https://github.com/SAP/project-piper-action). We are currently evaluating GitHub and this is why I did a quick test with GH actions but I’d like to try other solutions (Azure DevOps, SCP CI/CD service, Piper etc.). Having everything integrated in GitHub is pretty nice.

      What we are looking for is an easy to use CI/CD solution because we are not an IT company and don’t have a lot of developers and we can’t spend too much time setting up and configuring our pipelines. And SCP is only one of our platforms so we need a solution which is compatible with other platforms and frameworks.