CodeBuild succeeds while Node build command has failed - json

Let's assume that we have AWS CodeBuild project which buildspec file contains the following:
"phases": {
"install": {
"runtime-versions": {
"nodejs": 14
}
},
"build": {
"commands": [
"echo Build started on `date`",
"npm ci && npm audit --audit-level=critical",
"node -r esm index.js --env=CLOUDTEST"
]
},
"post_build": {
"commands": [
"echo Build completed on `date`"
]
}
It runs successfully, but if, for example, env value is wrong, node command will fail, but the build still succeeds.
Question: what should I do to make CodeBuild project to fail when node command fails?

The build phase transitions to post_build by default, regardless whether the build succeeds or fails. To override this behaviour, explicitly set the phase's on-failure behaviour:
"build": {
"on-failure": "ABORT",
"commands": ["node BOOM"]
},
The bad node BOOM command causes the execution to fail immediately. Logs tail:
[Container] 2022/04/29 11:21:10 Command did not exit successfully node BOOM exit status 1
[Container] 2022/04/29 11:21:10 Phase complete: BUILD State: FAILED_WITH_ABORT
[Container] 2022/04/29 11:21:10 Phase context status code: COMMAND_EXECUTION_ERROR Message: Error while executing command: node BOOM. Reason: exit status 1

Related

How to correct error in Bitbucket Pipeline running md-to-pdf

I am getting the below error when trying to run md-to-pdf (see https://www.npmjs.com/package/md-to-pdf) in a Bitbucket Pipeline script (see script below). 
Error
ERROR:zygote_host_impl_linux.cc(89)] Running as root without --no-sandbox is not supported.
See https://crbug.com/638180.
bitbucket-pipelines.yaml file
image: buildkite/puppeteer:v1.15.0
pipelines:
default:
- step:
caches:
- node
script:
- npm install -g md-to-pdf
- doc="appendix"
- md-to-pdf --config-file config.json ${doc}.md
config.json file
I tried to follow instructions for this. Is this config.json malformed?
{
"launch_options": {
"args": ["no-sandbox"]
}
}
The correct syntax is:
{
"launch_options": {
"args": ["--no-sandbox"]
}
}

using PKG with featherjs app

Has anyone ever tried using pkg with featherjs before? I'm having trouble getting it to work.
I get the following error when running my executable:
WARNING: No configurations found in configuration directory:/../project/config
WARNING: To disable this warning set SUPPRESS_NO_CONFIG_WARNING in the environment.
pkg/prelude/bootstrap.js:1172
throw error;
^
TypeError: root path required
at Function.serveStatic [as static] (/snapshot/../project/node_modules/serve-static/index.js:40:11)
Not sure where to go from here if anyone has any guidance.
"bin": "src/index.js",
"scripts": {
"test": "npm run eslint && npm run mocha",
"dev": "./node_modules/nodemon/bin/nodemon.js src/",
"eslint": "eslint src/. test/. --config .eslintrc.json",
"mocha": "mocha test/ --recursive --exit",
"start": "node src/",
"pkg": "pkg . -t node9-macos-x64 --out-path pkg"
},
"pkg": {
"assets": [
"src/**/*",
"public/**/*",
"config/**/*",
"node_modules/config/**/*.*"
],
"scripts": [
"src/**/*.js",
"config/**/*.json"
]
},
The error is coming from node-config used by #feathersjs/configuration which trying to load config/default.json from the folder of the running application, not the the bundled package. You can either try building with the NODE_CONFIG environment variable set to the content of config/defualt.json or remove app.configure(configuration()) and app.set the configuration options your application needs, for example by requiring config/default.json (which should also work in theh packaged environment):
const config = require('../config/default.json');
Object.keys(config).forEach(key => {
app.set(key, config[key]);
});

Packer Script exited with non-zero exit status : 127

I am trying to provision an AWS AMI but the packer script terminates with the following error.
Build 'amazon-ebs' errored: Script exited with non-zero exit status: 127
==> Some builds didn't complete successfully and had errors:
--> amazon-ebs: Script exited with non-zero exit status: 127
==> Builds finished but no artifacts were created.
My template for Packer is as follows:
{
"variables": {
"aws_access_key": "{{env `MY_ACCESS_KEY`}}",
"aws_secret_key": "{{env `MY_SECRET_KEY`}}"
},
"builders": [{
"type": "amazon-ebs",
"access_key": "{{user `aws_access_key`}}",
"secret_key": "{{user `aws_secret_key`}}",
"region": "us-east-1",
"source_ami":"ami-8c1be5f6",
"instance_type": "t2.micro",
"ssh_username": "ec2-user",
"ami_name": "packer-example {{timestamp}}"
}],
"provisioners":[
{
"type": "shell",
"script": "provision.sh"
}]}
Error log is as follows: PACKER_LOG=1 packer build template.json
2017/11/01 01:30:37 [INFO] (telemetry) ending amazon-ebs
2017/11/01 01:30:37 [INFO] (telemetry) found error: Script exited with non-zero exit status: 127
2017/11/01 01:30:37 ui error: Build 'amazon-ebs' errored: Script exited with non-zero exit status: 127
2017/11/01 01:30:37 Builds completed. Waiting on interrupt barrier...
2017/11/01 01:30:37 machine readable: error-count []string{"1"}
2017/11/01 01:30:37 ui error:
==> Some builds didn't complete successfully and had errors:
2017/11/01 01:30:37 machine readable: amazon-ebs,error []string{"Script exited with non-zero exit status: 127"}
2017/11/01 01:30:37 ui error: --> amazon-ebs: Script exited with non-zero exit status: 127
2017/11/01 01:30:37 ui:
==> Builds finished but no artifacts were created.
Build 'amazon-ebs' errored: Script exited with non-zero exit status: 127
provision.sh comprises of
#!/bin/bash
sudo yum install httpd -y
sudo yum update -y
sudo aws s3 cp s3://zbcxlkxcjvlxkj/index1.html /var/www/html/ --region us-east-1
sudo service httpd start
sudo chkconfig httpd on
Exit code 127 is returned by bash when a command is not found. Most likely you do not have all of the commands used in your script installed on your image prior to running it.

Can't install cloudwatch agent by cloudformation on Amazon ECS-optimized AMI

I am creating a cloudformation template, which creates some resources as EC2 instance, autoscaling group and launchConfiguration.
By the userData property of the launchConfiguration resource, I tried to install the Cloudwatch agent as follows:
"UserData":{ "Fn::Base64" : {
"Fn::Join" : ["", [
"#!/bin/bash -xe\n",
"yum -y install aws-cfn-bootstrap\n",
"/opt/aws/bin/cfn-init -v",
" --stack ", { "Ref": "AWS::StackName" },
" --resource LaunchCongig",
" --region ", { "Ref" : "AWS::Region" },"\n",
"yum -y install wget\n",
"# Get the CloudWatch Logs agent\n",
"wget https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py\n",
"# Install the CloudWatch Logs agent\n",
"python ./awslogs-agent-setup.py -n -r ", { "Ref" : "AWS::Region" }, " -c /etc/cwlogs.cfg || error_exit 'Failed to run CloudWatch Logs agent setup'\n",
"service awslogs start"
]]}
After ssh into the instance, I checked the file /var/log/cloud-init-output.log to see if everything is fine, but here is what I got:
+ wget https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py
--2017-02-17 14:36:10-- https://s3.amazonaws.com/aws-cloudwatch/downloads/latest/awslogs-agent-setup.py
Resolving s3.amazonaws.com (s3.amazonaws.com)... 52.216.226.59
Connecting to s3.amazonaws.com (s3.amazonaws.com)|52.216.226.59|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 47998 (47K) [text/x-python]
Saving to: ‘awslogs-agent-setup.py’
0K .......... .......... .......... .......... ...... 100% 196K=0.2s
2017-02-17 14:36:10 (196 KB/s) - ‘awslogs-agent-setup.py’ saved [47998/47998]
+ python ./awslogs-agent-setup.py -n -r eu-west-1 -c /etc/cwlogs.cfg
Step 1 of 5: Installing pip ...Traceback (most recent call last):
File "./awslogs-agent-setup.py", line 1144, in <module>
main()
File "./awslogs-agent-setup.py", line 1140, in main
setup.setup_artifacts()
File "./awslogs-agent-setup.py", line 693, in setup_artifacts
self.install_pip()
File "./awslogs-agent-setup.py", line 600, in install_pip
fail("Could not install pip. Please try again or see " + AGENT_SETUP_LOG_FILE + " for more details")
TypeError: fail() takes exactly 2 arguments (1 given)
+ error_exit 'Failed to run CloudWatch Logs agent setup'
/var/lib/cloud/instance/scripts/part-001: line 8: error_exit: command not found
Feb 17 14:36:12 cloud-init[2798]: util.py[WARNING]: Failed running /var/lib/cloud/instance/scripts/part-001 [127]
Feb 17 14:36:12 cloud-init[2798]: cc_scripts_user.py[WARNING]: Failed to run module scripts-user (scripts in /var/lib/cloud/instance/scripts)
Feb 17 14:36:12 cloud-init[2798]: util.py[WARNING]: Running module scripts-user (<module 'cloudinit.config.cc_scripts_user' from '/usr/lib/python2.7/dist-packages/cloudinit/config/cc_scripts_user.pyc'>) failed
Cloud-init v. 0.7.6 finished at Fri, 17 Feb 2017 14:36:12 +0000. Datasource DataSourceEc2. Up 85.78 seconds
What is wrong with this script? Is there any other way to install the agent?
Thank you.
EDIT:
I figured out that is because maybe the python-pip package didn't get installed so I added this to the userData:
"yum -y install python-pip\n",
After that I played the template again and strangely I got the same Error.
I am usinh an Amazon ECS-optimized AMI
I solved the problem by installing the agent directly by yum awslogs:
"UserData":{ "Fn::Base64" : {
"Fn::Join" : ["", [
"#!/bin/bash -xe\n",
"yum -y install aws-cfn-bootstrap\n",
"/opt/aws/bin/cfn-init -v",
" --stack ", { "Ref": "AWS::StackName" },
" --resource launchConfig",
" --region ", { "Ref" : "AWS::Region" },"\n",
"yum -y install awslogs\n",
"service awslogs start"
]]}
Here is the output from the log file:
Installed:
awslogs.noarch 0:1.1.2-1.10.amzn1
Dependency Installed:
aws-cli.noarch 0:1.11.29-1.45.amzn1
aws-cli-plugin-cloudwatch-logs.noarch 0:1.3.3-1.15.amzn1
freetype.x86_64 0:2.3.11-15.14.amzn1
libjpeg-turbo.x86_64 0:1.2.90-5.14.amzn1
mailcap.noarch 0:2.1.31-2.7.amzn1
python27-botocore.noarch 0:1.4.86-1.62.amzn1
python27-colorama.noarch 0:0.2.5-1.7.amzn1
python27-dateutil.noarch 0:2.1-1.3.amzn1
python27-docutils.noarch 0:0.11-1.15.amzn1
python27-futures.noarch 0:3.0.3-1.3.amzn1
python27-imaging.x86_64 0:1.1.6-19.9.amzn1
python27-jmespath.noarch 0:0.9.0-1.11.amzn1
python27-ply.noarch 0:3.4-3.12.amzn1
python27-pyasn1.noarch 0:0.1.7-2.9.amzn1
python27-rsa.noarch 0:3.4.1-1.8.amzn1
Complete!
+ service awslogs start
Starting awslogs: [ OK ]
Cloud-init v. 0.7.6 finished at Fri, 17 Feb 2017 15:33:42 +0000. Datasource DataSourceEc2. Up 83.47 seconds
Everything works fine this way. Hope that will help someone someday!
For ECS specifically, see Using CloudWatch Logs with Container Instances in the EC2 Container Service documentation for details on configuring CloudWatch Logs. The documentation recommends using yum install -y awslogs instead of the Python install script.
The documentation provides a complete sample in the Configuring CloudWatch Logs at Launch with User Data section.
In your case, since you're already managing your config files using cfn-init and CloudFormation::Init metadata in CloudFormation, you don't need any complex parsing of config files in your User-Data script, but you can still use the script as a reference. One thing worth adding to your User-Data script is running chkconfig awslogs on to make sure the service continues running on the instance after a reboot.

In node package.json, invoke script from another script with extra parameter, in this case add mocha watcher

in node's package.json I would like to reuse a command that I already have in a 'script'.
Here is the practical example
instead of (note the extra -w on the watch script):
"scripts": {
"test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
"watch": "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list -w",
}
I would like to have something like
"scripts": {
"test" : "./node_modules/mocha/bin/mocha --compilers coffee:coffee-script/register --recursive -R list",
"watch": "npm run script test" + "-w",
}
which doesn't work (can't do string concats in json), but you should get what I would like
I know that npm scripts support:
- & (parallel execution)
- && (sequencial execution)
so maybe there is another option?
This can be done in npm#2.1.17. You don't specify your OS and the version of npm that you are using, but unless you have done something to update it, you are probably running npm#1.4.28 which does not support the syntax below.
On Linux or OSX you can update npm with sudo npm install -g npm#latest. See https://github.com/npm/npm/wiki/Troubleshooting#try-the-latest-stable-version-of-npm for a guide to updating npm on all platforms.
You should be able to do this by passing an additional argument to your script:
"scripts": {
"test": "mocha --compilers coffee:coffee-script/register --recursive -R list",
"watch": "npm run test -- -w"
}
I verified this using the following, simplified package.json:
{
"scripts": { "a": "ls", "b": "npm run a -- -l" }
}
Output:
$ npm run a
> # a /Users/smikes/src/github/foo
> ls
package.json
$ npm run b
> # b /Users/smikes/src/github/foo
> npm run a -- -l
> # a /Users/smikes/src/github/foo
> ls -l
total 8
-rw-r--r-- 1 smikes staff 55 4 Jan 05:34 package.json
$