Skip to Content
0
Aug 04, 2023 at 09:11 AM

Start a js script before deployment

139 Views

Hi Experts,

I'm having difficulty configuring my project correctly: I would like that when I deploy first of all (or at least, before deploying the db) a script in my project is run.

If it helps, the script is in the same location as the mta, and below is the configuration of both my mta and my package.json

mta.yaml

_schema-version: '3.1'
ID: sfadb
version: 1.0.0
description: "Shared db for sfa applications."
parameters:
  enable-parallel-deployments: true
build-parameters:
  before-all:
    - builder: custom
      commands:
        - npm install --production
        - npx cds build --production

modules:

  #Main Module
  - name: sfadb-srv
    type: nodejs
    path: gen/srv
    parameters:
      buildpack: nodejs_buildpack
    build-parameters:
      builder: npm-ci
    provides:
      - name: srv-api # required by consumers of CAP services (e.g. approuter)
        properties:
          srv-url: ${default-url}
    requires:
      - name: authenticator_service
      - name: db-sfadb
      - name: destination
      - name: Connectivity-srv

  # Approuter
  - name: sfadb-approuter
    type: nodejs
    path: app/ # from cds.env.folders. Consider also cds.env.build.target -> gen/app
    parameters:
      keep-existing-routes: true
      disk-quota: 512MB
      memory: 512MB
    provides:
      - name: approuter-api # required by consumers of CAP services (e.g. approuter)
        properties:
          srv-url: ${default-url}
    requires:
      - name: srv-api
        group: destinations
        properties:
          name: srv-api # must be used in xs-app.json as well
          url: ~{srv-url}
          forwardAuthToken: true
      - name: authenticator_service

  # DB Deployer Module
  - name: db-deployer
    type: custom
    path: gen/db
    parameters:
      buildpacks:
        - https://github.com/cloudfoundry/apt-buildpack#v0.3.0
        - nodejs_buildpack
      no-route: true
      no-start: true
      disk-quota: 2GB
      memory: 1024MB
      tasks:
        - name: deploy_to_postgresql #I need start script before this
          command: export JAVA_OPTS="-Xmx1g" && chmod 755 deploy.sh && ./deploy.sh
          disk-quota: 2GB
          memory: 1024MB
    build-parameters:
      ignore:
        - "node_modules/"
    requires:
      - name: db-sfadb

  #Content Deployer
  - name: destination-content
    type: com.sap.application.content
    requires:
      - name: authenticator_service
        parameters:
          service-key:
            name: authenticator_key
      - name: destination
        parameters:
          content-target: true
    parameters:
      content:
        subaccount:
          destinations:
            - Authentication: OAuth2UserTokenExchange
              Name: authenticator_service
              ServiceInstanceName: authenticator_service
              ServiceKeyName: authenticator_key
              sap.cloud.service: sfa.service
          existing_destinations_policy: update
    build-parameters:
      no-source: true

resources:

  - name: authenticator_service
    type: org.cloudfoundry.managed-service
    parameters:
      service-name: authenticator_service
      service: xsuaa
      service-plan: application
      path: ./xs-security.json
      config:
        xsappname: auth-${space}
        tenant-mode: dedicated

  - name: destination
    type: org.cloudfoundry.managed-service
    requires:
      - name: srv-api
      - name: approuter-api
    parameters:
      service: destination
      service-name: destination-service
      service-plan: lite
      config:
        init_data:
          subaccount:
            existing_destinations_policy: ignore
            destinations:
              - Name: sfadb_srv_api
                Description: CAP sample service
                Authentication: NoAuthentication
                ProxyType: Internet
                Type: HTTP
                URL: ~{srv-api/srv-url}
                HTML5.DynamicDestination: true
                HTML5.ForwardAuthToken: true
              - Name: ZSFA2
                Authentication: BasicAuthentication
                ProxyType: Internet
                Type: HTTP
                URL: DUMMY_URL
                User: sample
                Password: sample
                HTML5.Timeout: true
              - Name: ZSFA2_SOAP
                Authentication: BasicAuthentication
                ProxyType: OnPremise
                Type: HTTP
                URL: DUMMY_URL
                User: sample
                Password: sample
                sap-client: 200
                TrustAll: false

  - name: db-sfadb
    parameters:
      path: ./pg-options.json
      service: postgresql-db
      #service-plan: trial # BTP trial
      service-plan: free    # BTP CPEA - free
      #service-plan: standard # BTP CPEA - standard (not free)
      service-tags:
        - plain
      skip-service-updates:
        parameters: true
    type: org.cloudfoundry.managed-service

  - name: Connectivity-srv
    type: org.cloudfoundry.managed-service
    parameters:
      service: connectivity
      service-plan: lite

package.json

{
    "name": "sfadb",
    "version": "0.0.1",
    "description": "Shared DB for sfa apps",
    "repository": "**",
    "license": "UNLICENSED",
    "private": true,
    "dependencies": {
        "@sap-cloud-sdk/connectivity": "^2.14.0",
        "@sap-cloud-sdk/http-client": "^2.13.0",
        "@sap/cds": "6.5.0",
        "@sap/cds-dk": "^6.4.0",
        "@sap/jobs-client": "^1.7.30",
        "@sap/xsenv": "^3.3.2",
        "@sap/xssec": "^3",
        "cds-dbm": "0.1.2",
        "cds-pg": "0.1.33",
        "child_process": "^1.0.2",
        "express": "^4",
        "fs-extra": "11.1.1",
        "mbt": "^1.2.23",
        "passport": "^0",
        "request": "^2.88.2",
        "uuid": "^9.0.0",
        "xml2js": "^0.4.23"
    },
    "devDependencies": {
        "@sap/cds-odata-v2-adapter-proxy": "latest",
        "@sap/ux-specification": "^1.102.14",
        "rimraf": "^3.0.2",
        "sqlite3": "^5.0.4",
        "ui5-middleware-livereload": "^0.7.0"
    },
    "scripts": {
        "start": "cds run",
        "watch-ui": "cds watch --open tso/webapp/index.html?sap-ui-xx-viewCache=false"
    },
    "engines": {
        "node": "^16.15"
    },
    "eslintConfig": {
        "extends": "eslint:recommended",
        "env": {
            "es2020": true,
            "node": true,
            "jest": true,
            "mocha": true
        },
        "globals": {
            "SELECT": true,
            "INSERT": true,
            "UPDATE": true,
            "DELETE": true,
            "CREATE": true,
            "DROP": true,
            "CDL": true,
            "CQL": true,
            "CXL": true,
            "cds": true
        },
        "rules": {
            "no-console": "off",
            "require-atomic-updates": "off"
        }
    },
    "cds": {
        "i18n": {
            "defaultLocales": [
                "en",
                "it",
                "de"
            ],
            "for_sqlite": [
                "en",
                "it",
                "de"
            ]
        },
        "requires": {
            "ZSFA_ODV4": {
                "kind": "odata",
                "csrf": true,
                "csrfInBatch": true,
                "credentials": {
                    "path": "**",
                    "destination": "ZSFA2"
                }
            },
            "[production]": {
                "db": {
                    "dialect": "plain",
                    "kind": "database"
                },
                "database": {
                    "dialect": "plain",
                    "impl": "cds-pg",
                    "model": [
                        "srv"
                    ],
                    "credentials": {
                        "host": "localhost",
                        "port": 5432,
                        "database": "devtoberfest",
                        "user": "**",
                        "password": "**"
                    }
                }
            },
            "auth": {
                "kind": "xsuaa"
            },
            "db": {
                "dialect": "plain",
                "kind": "database",
                "credentials": {
                    "host": "localhost",
                    "port": 5432,
                    "database": "sfadb",
                    "user": "**",
                    "password": "**"
                }
            },
            "database": {
                "dialect": "plain",
                "impl": "cds-pg",
                "model": [
                    "srv"
                ],
                "credentials": {
                    "host": "localhost",
                    "port": 5432,
                    "database": "sfadb",
                    "user": "**",
                    "password": "**"
                }
            },
            "approuter": {
                "kind": "cloudfoundry"
            },
            "API_CV_ATTACHMENT_SRV": {
                "kind": "odata-v2",
                "credentials": {
                    "path": "**",
                    "destination": "ZSFA2"
                }
            }
        },
        "migrations": {
            "db": {
                "schema": {
                    "default": "public",
                    "clone": "_cdsdbm_clone",
                    "reference": "_cdsdbm_ref"
                },
                "deploy": {
                    "tmpFile": "tmp/_autodeploy.json",
                    "undeployFile": "dbm/undeploy.json"
                }
            }
        },
        "build": {
            "tasks": [
                {
                    "for": "node-cf",
                    "src": "srv",
                    "options": {
                        "model": [
                            "db",
                            "srv",
                            "app"
                        ]
                    }
                },
                {
                    "use": "cds-dbm/dist/build/postgres-cf",
                    "for": "postgres-cf",
                    "src": "db",
                    "options": {
                        "deployCmd": "npx cds-dbm deploy --load-via delta"
                    }
                }
            ]
        }
    }
}

Thank you