cdk deploy with context parameters not working on linux agents
sh("cdk deploy -c awsAcctId=XXXXXXX StackName --require-approval never") in JenkinsFile from a Jenkins agent(Linux) does not recognize the context variables passed in. Running the same command from my local(windows-10) using command prompt works fine.
Also, when i run the above command without specifying the context parameter, then that works fine from the jenkins agent(linux)
Reproduction Steps
JenkinsFIle code: from jenkins agent
sh("cdk deploy -c awsAcctId=XXXXXXX StackName --require-approval never")
C# code: Program.cs
var app = new App();
var ctxParam = "";
ctxParam = app.Node.TryGetContext("awsAcctId").ToString();
app.Node.TryGetContext("awsAcctId") is NULL
As, I said, the above sh command works fine when called from Windows(command prompt) but not when called from Linux agent(in this case jenkins)
Environment
- **CLI Version :**1.31
- Framework Version:
- OS :
- Language : C#
Other
This is :bug: Bug Report
Hello @skinny85, it will be great if you could give me some pointers as I am nearing a production deployment and would be good to get this going. Sorry for the rush as I am sure you must be swamped as well.
Hey @sacag ,
what's the sh function quoted in your Jenkins config? Is that a Groovy script...? What language is that?
Thanks, Adam
Yep, that's groovy @skinny85
Can you add --verbose to the argument, and paste the output? (if it gets too big, maybe use GitHub's gists).
So, I tried that, but doesn't even get to that point as it fails with null ref exception before. because in the C# code it doesn't find the value in the context. Shall, I remove the --context option and try that? Would that be helpful?
I don't understand...? --verbose is an argument to the cdk command, and it's the cdk command that executes the C# code...?
If it didn't work, your C# code would never be executed.
Let me try it again. What I meant was when the cdk deploy executes, it calls the C# code for deploying the stack with context parameters and that's when the C# code can't find the variable in the context. So, it wasn't printing any verbose log. But i can try again. Maybe I misplaced the -v option
sh("cdk deploy --context testVar=1234 pra-messaging-ssmparams-app --verbose=true") I see this:
21:46:37 + cdk bootstrap aws://XXXXXXXX/us-east-1
21:46:40 (node:1139) ExperimentalWarning: The fs.promises API is experimental
21:46:42 Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
21:46:42 at FMI.BioPharma.Messaging.Program.Main(String[] args) in /home/jenkins/workspace/ma.Messaging_release-1.1-testcdk/src/FMI.BioPharma.Messaging/Program.cs:line 23
21:46:42 Subprocess exited with error 134
What's this?
21:46:37 + cdk bootstrap aws://XXXXXXXX/us-east-1
?
bootstrapping the account...just masked the account number here
I understand. Where does that command comes from? You said the command was sh("cdk deploy --context testVar=1234 pra-messaging-ssmparams-app --verbose=true").
just above that line i have another sh command: sh("cdk bootstrap aws://"+GetRoleAccountNumber(params.DeployTo)+"/us-east-1")
Can you add --verbose to that bootstrap command, and show the output?
that build is still running, but i tried this - removed the cdk bootstrap command and used this: cdk deploy --context testVar=1234 pra-messaging-ssmparams-app -v
that worked fine. maybe it worked because this stack doesn't have any assets and bootstarp is not needed.Maybe?
I don't understand - was it the bootstrap command that failed previously...?
no bootstrap didn't fail. here is the log
22:00:36 CDK toolkit version: 1.31.0 (build 8f3ac79)
22:00:36 Command line arguments: { _: [ 'bootstrap' ],
22:00:36 verbose: true,
22:00:36 v: true,
22:00:36 'ignore-errors': false,
22:00:36 ignoreErrors: false,
22:00:36 json: false,
22:00:36 j: false,
22:00:36 ec2creds: undefined,
22:00:36 i: undefined,
22:00:36 'version-reporting': undefined,
22:00:36 versionReporting: undefined,
22:00:36 'path-metadata': true,
22:00:36 pathMetadata: true,
22:00:36 'asset-metadata': true,
22:00:36 assetMetadata: true,
22:00:36 'role-arn': undefined,
22:00:36 r: undefined,
22:00:36 roleArn: undefined,
22:00:36 staging: true,
22:00:36 'no-color': false,
22:00:36 noColor: false,
22:00:36 fail: false,
22:00:36 'bootstrap-bucket-name': undefined,
22:00:36 b: undefined,
22:00:36 'toolkit-bucket-name': undefined,
22:00:36 toolkitBucketName: undefined,
22:00:36 bootstrapBucketName: undefined,
22:00:36 'bootstrap-kms-key-id': undefined,
22:00:36 bootstrapKmsKeyId: undefined,
22:00:36 tags: [],
22:00:36 t: [],
22:00:36 execute: true,
22:00:36 trust: [],
22:00:36 'cloudformation-execution-policies': [],
22:00:36 cloudformationExecutionPolicies: [],
22:00:36 '$0': '/usr/bin/cdk',
22:00:36 ENVIRONMENTS: [ 'aws://397090398856/us-east-1' ],
22:00:36 environments: [ 'aws://397090398856/us-east-1' ] }
22:00:36 Determining whether we're on an EC2 instance.
22:00:36 Looks like EC2 instance.
22:00:36 cdk.json: {
22:00:36 "app": "dotnet run -p src/FMI.BioPharma.Messaging/FMI.BioPharma.Messaging.csproj",
22:00:36 "context": {
'vpc-id-dev': 'cxccxcxcxcxc',
22:00:36 'subnet-ids-dev': 'xxccxcxcx',
22:00:36 'routeTable-ids-dev': 'sdsdsdssd',
22:00:36 'securityGroup-id-dev': 'dssddsd',
22:00:36 }
22:00:36 }
22:00:36 cdk.context.json: {
22:00:36 "@aws-cdk/core:enableStackNameDuplicates": "true"
22:00:36 }
22:00:36 merged settings: { versionReporting: true,
22:00:36 pathMetadata: true,
22:00:36 output: 'cdk.out',
22:00:36 app:
22:00:36 'dotnet run -p src/FMI.BioPharma.Messaging/FMI.BioPharma.Messaging.csproj',
22:00:36 context:
22:00:36 { 'vpc-id-dev': 'cxccxcxcxcxc',
22:00:36 'subnet-ids-dev': 'xxccxcxcx',
22:00:36 'routeTable-ids-dev': 'sdsdsdssd',
22:00:36 'securityGroup-id-dev': 'dssddsd',
22:00:36
22:00:36 tags: [],
22:00:36 assetMetadata: true,
22:00:36 toolkitBucket: {},
22:00:36 staging: true }
22:00:36 Setting "CDK_DEFAULT_REGION" environment variable to us-east-1
22:00:36 Resolving default credentials
22:00:36 Looking up default account ID from STS
22:00:36 Default account ID: 397090398856
22:00:36 Setting "CDK_DEFAULT_ACCOUNT" environment variable to 397090398856
22:00:36 context: { '@aws-cdk/core:enableStackNameDuplicates': 'true',
22:00:36 'vpc-id-dev': 'vpc-6b37dc0d',
22:00:36 'subnet-ids-dev': 'subnet-583a9103,subnet-22cf790f,subnet-eedfb9a7',
22:00:36 'routeTable-ids-dev': 'rtb-94134af2,rtb-d8134abe,rtb-b5134ad3',
22:00:36 'securityGroup-id-dev': 'sg-fb0b5e84',
22:00:36 'aws:cdk:enable-path-metadata': true,
22:00:36 'aws:cdk:enable-asset-metadata': true }
22:00:36 outdir: cdk.out
22:00:36 env: { CDK_DEFAULT_REGION: 'us-east-1',
22:00:36 CDK_DEFAULT_ACCOUNT: '397090398856',
22:00:36 CDK_CONTEXT_JSON:
22:00:36 '{"@aws-cdk/core:enableStackNameDuplicates":"true","vpc-id-dev":"dsdsds","subnet-ids-dev":"dsdsdsds","routeTable-ids-dev":"ssddsds","securityGroup-id-dev":"ccsdss",
"aws:cdk:enable-path-metadata":true,"aws:cdk:enable-asset-metadata":true}',
22:00:36 CDK_OUTDIR: 'cdk.out',
22:00:36 CDK_CLI_ASM_VERSION: '1.21.0',
22:00:36 CDK_CLI_VERSION: '1.31.0' }
22:00:39 Unhandled exception. System.NullReferenceException: Object reference not set to an instance of an object.
22:00:39 at FMI.BioPharma.Messaging.Program.Main(String[] args) in /home/jenkins/workspace/ma.Messaging_release-1.1-testcdk/src/FMI.BioPharma.Messaging/Program.cs:line 23
22:00:39 Subprocess exited with error 134
22:00:39 Error: Subprocess exited with error 134
22:00:39 at ChildProcess.proc.on.code (/usr/lib/node_modules/aws-cdk/lib/api/cxapp/exec.ts:115:23)
22:00:39 at ChildProcess.emit (events.js:182:13)
22:00:39 at ChildProcess.EventEmitter.emit (domain.js:441:20)
22:00:39 at Process.ChildProcess._handle.onexit (internal/child_process.js:240:12)
What command failed? If it was cdk deploy, where is this part of the output:
22:00:36 CDK toolkit version: 1.31.0 (build 8f3ac79)
22:00:36 Command line arguments: { _: [ 'bootstrap' ],
that you can see for cdk bootstrap --verbose?
+ cdk deploy -c aws_account_id=397090398856
22:21:27 + DEV pra-messaging-ssmparams-app --require-approval never -v
22:21:27 /home/jenkins/workspace/ma.Messaging_release-1.1-testcdk@tmp/durable-31dcff0d/script.sh: 1: /home/jenkins/workspace/ma.Messaging_release-1.1-testcdk@tmp/durable-31dcff0d/script.sh: DEV: not found
22:21:28 (node:1129) ExperimentalWarning: The fs.promises API is experimental
22:21:36 Since this app includes more than a single stack, specify which stacks to use (wildcards are supported)
22:21:36 Stacks: pra-messaging-check-health-app pra-messaging-client-app pra-messaging-msgDetails-app pra-messaging-msgDetails-filtered-app pra-messaging-notification-app pra-messaging-ssmparams-app pra-messaging-stepfunction-apigateway-app pra-messaging-study-app pra-messaging-variant-app
not sure why that message as i am specifying the correct stackname
I think the message is pretty clear: you need to specify which stack to deploy (for example, cdk deploy pra-messaging-check-health-app). For all stacks, use cdk deploy '*'.