IBM Cloud Code Engine: How to overwrite environment variable in cron-invoked job run?

132 views Asked by At

When working with jobs in IBM Cloud Code Engine, I would submit a jobrun for the actual invocation. For the jobrun I can specify environment variables to be passed into the runtime environment (--env FOO=BAR).

How can I do the same when using a cron subscription to trigger the job, i.e., to set FOO=BAR?

1

There are 1 answers

2
Susan Cline On

I believe the correct flag to pass to the CLI is the --ext or --extension.

% ibmcloud ce subscription cron create --
NAME:
  create - Create a cron event subscription.

USAGE:
  create --name CRON_SOURCE_NAME  --destination DESTINATION_REF [options...]

OPTIONS:
  --destination, -d value         Required. The name of the resource that will receive events.  
  --name, -n value                Required. The name of the cron event subscription. Use a name that is unique within the project.  
  --content-type, --ct value      The media type of the 'data' or 'data-base64' option. Examples include 'application/json',  
                                  'application/x-www-form-urlencoded', 'text/html', and 'text/plain'.  
  --data, --da value              The data to send to the destination.  
  --data-base64, --db value       The base64-encoded data to send to the destination.  
  --destination-type, --dt value  The type of the 'destination'. Valid values are 'app' and 'job'. (default: "app")  
  --extension, --ext value        Set CloudEvents extensions to send to the destination. Must be in NAME=VALUE format. This option can be  
                                  specified multiple times.  (accepts multiple inputs)  

For example:

% ibmcloud ce subscription cron create -d application-5c -n sample-cron-sub --ext FOO=BAR
Creating cron event subscription 'sample-cron-sub'...
Run 'ibmcloud ce subscription cron get -n sample-cron-sub' to check the cron event subscription status.
% ibmcloud ce subscription cron get -n sample-cron-sub
Getting cron event subscription 'sample-cron-sub'...
OK

Name:          sample-cron-sub  
ID:            xxxx  
Project Name:  susan-project  
Project ID:    xxxx  
Age:           3m16s  
Created:       2022-06-06T11:17:17-07:00  

Destination Type:  app  
Destination:       application-5c  
Schedule:          * * * * *  
Time Zone:         UTC  
Ready:             true  

CloudEvents Extensions:    
  Name  Value  
  FOO   BAR  

Events:                    
  Type    Reason                       Age                    Source                 Messages  
  Normal  PingSourceSkipped            3m17s                  pingsource-controller  PingSource is not ready  
  Normal  PingSourceDeploymentUpdated  3m17s (x2 over 3m17s)  pingsource-controller  PingSource adapter deployment updated  
  Normal  PingSourceSynchronized       3m17s                  pingsource-controller  PingSource adapter is synchronized 

After looking at this a bit more it appears that the name=value pairs you pass in to an event subscription prepend the string 'CE_' to the name.
Therefore, to allow for this in the running job, you would need to prepend the environment variable in the job with the CE_. For example:
When I create the jobdefinition I add the environment variable like this: CE_FOO=BAR

Then, when I create the event subscription, for the --ext flag, I use the original suggestion: --ext FOO=BAR

I believe since the FOO variable in the event subscription automatically gets the prepended CE_ to the FOO variable it should work.
Please let me know if this does not work or I misunderstood you.