For a node Cloud Foundry app which is hosted on IBM Cloud and which uses Watson Assistant, we have built a Pipeline within a Toolchain in IBM Cloud. We are having trouble in the deploy stage of the toolchain - receiving an authentication error when calling the Assistant API using our environment variables.
When we run the app locally using the same environment variables, everything works. We can see that the environment variables defined in the Setup Stage of the Toolchain are accessible in the Deploy Stage (we can print them in the Deploy Stage logs using cf env, and the environment variables are set and visible in the IBM Cloud App Runtime UI), but then the node runtime of the app in the deploy stage cannot access them (they are undefined, causing the error in the logs seen below).
In the app files (node js), we are using process.env.VARIABLE_NAME to access the environment properties.
If we manually set the environment variables in the IBM Cloud App – and then cf push from our local terminal, then the app also works fine.
Please see below the bash scripts used in the setup and deploy phase. The build phase runs successfully.
Error in logs:
[APP/PROC/WEB/0] ERR Error: Insufficient credentials provided in constructor argument. Refer to the documentation for the required parameters. Common examples are username/password and iam_access_token.
[APP/PROC/WEB/0] ERR at AssistantV2.BaseService.initCredentials (/home/vcap/app/node_modules/ibm-cloud-sdk-core/lib/base_service.ts:317:15)
[APP/PROC/WEB/0] ERR at AssistantV2.BaseService (/home/vcap/app/node_modules/ibm-cloud-sdk-core/lib/base_service.ts:153:27)
[APP/PROC/WEB/0] ERR at new AssistantV2 (/home/vcap/app/node_modules/ibm-watson/assistant/v2.ts:51:5)
Setup stage script: (the environment properties are defined as secure properties in this stage.)
#!/bin/bash
cf push "${CF_APP}"
# Set environment variables that will be used after build the app
cf set-env "${CF_APP}" WATSON_ASSISTANT_APIKEY "${WATSON_ASSISTANT_APIKEY}"
cf set-env "${CF_APP}" WATSON_ASSISTANT_WORKSPACE "${WATSON_ASSISTANT_WORKSPACE}"
cf set-env "${CF_APP}" WATSON_ASSISTANT_ASSISTANT_ID "${WATSON_ASSISTANT_ASSISTANT_ID}"
# Restage app to get the new credentials availables
cf restage "${CF_APP}"
Deploy stage script: (more or less equivalent to cf push)
#!/bin/bash
# Push app
if ! cf app $CF_APP; then
cf push $CF_APP
echo "no existing app, so doing fresh push"
else
OLD_CF_APP=${CF_APP}-OLD-$(date +"%s")
rollback() {
set +e
if cf app $OLD_CF_APP; then
cf logs $CF_APP --recent
cf delete $CF_APP -f
cf rename $OLD_CF_APP $CF_APP
fi
exit 1
}
set -e
trap rollback ERR
cf rename $CF_APP $OLD_CF_APP
cf push $CF_APP
cf delete $OLD_CF_APP -f
fi
# Export app name and URL for use in later Pipeline jobs
export CF_APP_NAME="$CF_APP"
export APP_URL=http://$(cf app $CF_APP_NAME | grep -e urls: -e routes: | awk '{print $2}')
# View logs
#cf logs "${CF_APP}" --recent