Skip to content

Commit 019ecb4

Browse files
committed
fix: build fails when there are spaces in an input json
1 parent 2fa62cd commit 019ecb4

File tree

4 files changed

+4737
-6
lines changed

4 files changed

+4737
-6
lines changed

src/nodejs-build.ts

+8-4
Original file line numberDiff line numberDiff line change
@@ -117,11 +117,15 @@ export class NodejsBuild extends Construct {
117117
build: {
118118
commands: [
119119
'current_dir=$(pwd)',
120+
// Iterate a json array using jq
121+
// https://www.starkandwayne.com/blog/bash-for-loop-over-json-array-using-jq/index.html
120122
`
121-
for obj in $(echo "$input" | jq -c '.[]'); do
122-
assetUrl=$(echo "$obj" | jq -r '.assetUrl')
123-
extractPath=$(echo "$obj" | jq -r '.extractPath')
124-
commands=$(echo "$obj" | jq -r '.commands')
123+
echo "$input"
124+
for obj in $(echo "$input" | jq -r '.[] | @base64'); do
125+
decoded=$(echo "$obj" | base64 --decode)
126+
assetUrl=$(echo "$decoded" | jq -r '.assetUrl')
127+
extractPath=$(echo "$decoded" | jq -r '.extractPath')
128+
commands=$(echo "$decoded" | jq -r '.commands')
125129
126130
# Download the zip file
127131
aws s3 cp "$assetUrl" temp.zip

test/nodejs-build.integ.snapshot/NodejsBuildIntegTest.template.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -384,7 +384,7 @@
384384
]
385385
},
386386
"Source": {
387-
"BuildSpec": "{\n \"version\": \"0.2\",\n \"phases\": {\n \"install\": {\n \"runtime-versions\": {\n \"nodejs\": 18\n }\n },\n \"build\": {\n \"commands\": [\n \"current_dir=$(pwd)\",\n \"\\nfor obj in $(echo \\\"$input\\\" | jq -c '.[]'); do\\n assetUrl=$(echo \\\"$obj\\\" | jq -r '.assetUrl')\\n extractPath=$(echo \\\"$obj\\\" | jq -r '.extractPath')\\n commands=$(echo \\\"$obj\\\" | jq -r '.commands')\\n\\n # Download the zip file\\n aws s3 cp \\\"$assetUrl\\\" temp.zip\\n\\n # Extract the zip file to the extractPath directory\\n mkdir -p \\\"$extractPath\\\"\\n unzip temp.zip -d \\\"$extractPath\\\"\\n\\n # Remove the zip file\\n rm temp.zip\\n\\n # Run the specified commands in the extractPath directory\\n cd \\\"$extractPath\\\"\\n ls -la\\n eval \\\"$commands\\\"\\n cd \\\"$current_dir\\\"\\n ls -la\\ndone\\n \",\n \"ls -la\",\n \"cd \\\"$workingDirectory\\\"\",\n \"eval \\\"$buildCommands\\\"\",\n \"ls -la\",\n \"cd \\\"$current_dir\\\"\",\n \"cd \\\"$outputSourceDirectory\\\"\",\n \"zip -r output.zip ./\",\n \"aws s3 cp output.zip \\\"s3://$destinationBucketName/$destinationObjectKey\\\"\"\n ]\n },\n \"post_build\": {\n \"commands\": [\n \"echo Build completed on `date`\",\n \"\\nSTATUS='SUCCESS'\\nif [ $CODEBUILD_BUILD_SUCCEEDING -ne 1 ] # Test if the build is failing\\nthen\\nSTATUS='FAILED'\\nREASON=\\\"NodejsBuild failed. See CloudWatch Log stream for the detailed reason: \\nhttps://$AWS_REGION.console.aws.amazon.com/cloudwatch/home?region=$AWS_REGION#logsV2:log-groups/log-group/\\\\$252Faws\\\\$252Fcodebuild\\\\$252F$projectName/log-events/$CODEBUILD_LOG_PATH\\\"\\nfi\\ncat <<EOF > payload.json\\n{\\n \\\"StackId\\\": \\\"$stackId\\\",\\n \\\"RequestId\\\": \\\"$requestId\\\",\\n \\\"LogicalResourceId\\\":\\\"$logicalResourceId\\\",\\n \\\"PhysicalResourceId\\\": \\\"$destinationObjectKey\\\",\\n \\\"Status\\\": \\\"$STATUS\\\",\\n \\\"Reason\\\": \\\"$REASON\\\"\\n}\\nEOF\\ncurl -vv -i -X PUT -H 'Content-Type:' -d \\\"@payload.json\\\" \\\"$responseURL\\\"\\n \"\n ]\n }\n }\n}",
387+
"BuildSpec": "{\n \"version\": \"0.2\",\n \"phases\": {\n \"install\": {\n \"runtime-versions\": {\n \"nodejs\": 18\n }\n },\n \"build\": {\n \"commands\": [\n \"current_dir=$(pwd)\",\n \"\\necho \\\"$input\\\"\\nfor obj in $(echo \\\"$input\\\" | jq -r '.[] | @base64'); do\\n decoded=$(echo \\\"$obj\\\" | base64 --decode)\\n assetUrl=$(echo \\\"$decoded\\\" | jq -r '.assetUrl')\\n extractPath=$(echo \\\"$decoded\\\" | jq -r '.extractPath')\\n commands=$(echo \\\"$decoded\\\" | jq -r '.commands')\\n\\n # Download the zip file\\n aws s3 cp \\\"$assetUrl\\\" temp.zip\\n\\n # Extract the zip file to the extractPath directory\\n mkdir -p \\\"$extractPath\\\"\\n unzip temp.zip -d \\\"$extractPath\\\"\\n\\n # Remove the zip file\\n rm temp.zip\\n\\n # Run the specified commands in the extractPath directory\\n cd \\\"$extractPath\\\"\\n ls -la\\n eval \\\"$commands\\\"\\n cd \\\"$current_dir\\\"\\n ls -la\\ndone\\n \",\n \"ls -la\",\n \"cd \\\"$workingDirectory\\\"\",\n \"eval \\\"$buildCommands\\\"\",\n \"ls -la\",\n \"cd \\\"$current_dir\\\"\",\n \"cd \\\"$outputSourceDirectory\\\"\",\n \"zip -r output.zip ./\",\n \"aws s3 cp output.zip \\\"s3://$destinationBucketName/$destinationObjectKey\\\"\"\n ]\n },\n \"post_build\": {\n \"commands\": [\n \"echo Build completed on `date`\",\n \"\\nSTATUS='SUCCESS'\\nif [ $CODEBUILD_BUILD_SUCCEEDING -ne 1 ] # Test if the build is failing\\nthen\\nSTATUS='FAILED'\\nREASON=\\\"NodejsBuild failed. See CloudWatch Log stream for the detailed reason: \\nhttps://$AWS_REGION.console.aws.amazon.com/cloudwatch/home?region=$AWS_REGION#logsV2:log-groups/log-group/\\\\$252Faws\\\\$252Fcodebuild\\\\$252F$projectName/log-events/$CODEBUILD_LOG_PATH\\\"\\nfi\\ncat <<EOF > payload.json\\n{\\n \\\"StackId\\\": \\\"$stackId\\\",\\n \\\"RequestId\\\": \\\"$requestId\\\",\\n \\\"LogicalResourceId\\\":\\\"$logicalResourceId\\\",\\n \\\"PhysicalResourceId\\\": \\\"$destinationObjectKey\\\",\\n \\\"Status\\\": \\\"$STATUS\\\",\\n \\\"Reason\\\": \\\"$REASON\\\"\\n}\\nEOF\\ncurl -vv -i -X PUT -H 'Content-Type:' -d \\\"@payload.json\\\" \\\"$responseURL\\\"\\n \"\n ]\n }\n }\n}",
388388
"Type": "NO_SOURCE"
389389
},
390390
"Cache": {

0 commit comments

Comments
 (0)