CodeDeploy fails "stop: command not found"

Viewed 306

I am trying to deploy a React Application via CodePipeline; my 3 steps are

  • Github source (passes)
  • CodeBuild (passes)
  • CodeDeploy (fails)

Buildspec.yml

version: 0.2
phases:
  pre_build:
    commands:
      - npm install
      - npm install -g webpack-cli
  build:
    commands:
      - echo Build started on `date`
      - echo Compiling
      - webpack --config config/webpack.prod.js --env BACKEND_URL=$BACKEND_URL
  post_build:
    commands:
      - echo Build completed on `date`
artifacts:
  files:
    - build/**/*
    - public/**/*
    - dist/**/*
    - src/**/*
    - config/**/*
    - scripts/**/*
    - package.json
    - appspec.yml

Appspec.yml

version: 0.0
os: linux

files:
  - source: /
    destination: /home/ec2-user/server

permissions:
  - object: /
    pattern: "**"
    owner: ec2-user
    group: ec2-user

hooks:

  ApplicationStop:
    - location: scripts/app_stop.sh
      timeout: 300
      runas: root

  BeforeInstall:
    - location: scripts/before_install.sh
      timeout: 300
      runas: root

  ApplicationStart:
    - location: scripts/app_start.sh
      timeout: 300
      runas: root

app_stop.sh

#!/bin/bash
sudo systemctl stop nginx

before_install.sh

#!/bin/bash
cd /home/ec2-user/server
amazon-linux-extras install nginx1

app_start.sh

#!/bin/bash
cd /home/ec2-user/server
cp build/nginx.conf /etc/nginx/nginx.conf
cp -R dist /var/www/html/taxi-booker
nginx

CodeDeploy is currently failing with this log in the "ApplicationStop" step. The first time I ran this pipeline it passed fine (without ApplicationStop) however if I run it without this step, the nginx port is already in use.

LifecycleEvent - ApplicationStop
Script - scripts/app_stop.sh
[stderr]/opt/codedeploy-agent/deployment-root/9ce189ed-1f48-4cdb-b01f-e7cd8986c8b7/d-QUYBUBMKC/deployment-archive/scripts/app_stop.sh: line 2: stop: command not found

Any idea what the proper way to deploy with nginx is? Thanks a lot

Logs:enter image description here

1 Answers

Try to change your application stop hook with :

#!/bin/bash
service nginx stop

complement :

Try to execute the command to stop nginx inside your instance EC2, not from CodeDeploy, like a root / ec2-user maybe its permissions Idk, but try to fix the status of nginx from the command, once you fix it, you can re-run the CodeDeploy job, the error is not from CodeDeploy but with the command.

You see that 127 code, that means command not found. Fix it first in your terminal inside your EC2, then rerun the CodeDeploy job

Related