0

I am using circleci for CI CD, and here I am running a shell script. Their website says we should use these as best practices,

#!/usr/bin/env bash

# Exit script if you try to use an uninitialized variable.
set -o nounset

# Exit script if a statement returns a non-true return value.
set -o errexit

# Use the error status of the first failure, rather than that of the last item in a pipeline.
set -o pipefail

But it still throws this error -

ami_manager.sh: 5: set: Illegal option -o pipefail

Exited with code exit status 2

I am running this job -

  build_image_if_required:
    docker:
      - image: cimg/base:2023.04
    executor: aws-cli/default
    working_directory: ~/project
    steps:
      - attach_workspace:
          at: .
      - checkout
      - run: 
          name: Install Packer
          command: |
            sudo curl https://releases.hashicorp.com/packer/1.8.4/packer_1.8.4_linux_amd64.zip -o /tmp/packer.zip
            sudo unzip /tmp/packer.zip
            sudo mv ./packer /usr/bin/
            sudo chmod +x /usr/bin/packer
      - aws-cli/setup:
          aws_access_key_id: aws_access_key_id
          aws_secret_access_key: aws_secret_access_key
          region: region
      - run:
          name: Find changes to build image
          command: cd infrastructure/scripts; sh ami_manager.sh dev
      - persist_to_workspace:
          root: .               
          paths:
            - .  

I have changed docker image, and tried to run without these flags but then it sill shows some other errors, like no parentheses in function is expected, and then function is not found, so I figured there's something wrong ... If anyone have faced such issues before please show a way.

ashraf minhaj
  • 504
  • 2
  • 14
  • Probably means the shell is actually `sh` and not really `bash`. When you run `ami_manager.sh` you're even making that explicit. – Charles Duffy Aug 22 '23 at 16:23
  • Also, the advice you're trying to follow is generally considered _bad_ advice. See [BashFAQ #105](https://mywiki.wooledge.org/BashFAQ/105#Exercises) for guidance on why `errexit` is considered an antipattern. (Doing no error handling is also bad, but `somecmd || exit` / `somecmd || return` is far more reliable than relying on `set -e` / `set -o errexit`; also less version- and context-dependent, and thus easier to hand-audit whether it'll actually work when it's needed). – Charles Duffy Aug 22 '23 at 16:23
  • `pipefail` is more often a good idea, but it's still not _always_ a good idea; we have a lot of questions here about bugs caused by people not understanding it -- see f/e [bash zcat head causes pipefail?](https://stackoverflow.com/questions/41516177/bash-zcat-head-causes-pipefail) – Charles Duffy Aug 22 '23 at 16:25
  • And for `nounset`, see [BashFAQ #112](https://mywiki.wooledge.org/BashFAQ/112). Again, unlike `set -e`, `set -u` isn't _always_ considered a bad idea, but you still need to actually know what you're doing to write code that's compatible with it. – Charles Duffy Aug 22 '23 at 16:27
  • ...for a somewhat spectacular demonstration of _just how_ incompatible `set -e`/`set -o errexit` behavior is across different shells (and thus how unreliable it makes your scripts when they need to be portable), see https://www.in-ulm.de/~mascheck/various/set-e/ – Charles Duffy Aug 22 '23 at 16:29
  • Does this answer your question? [Difference between sh and Bash](https://stackoverflow.com/questions/5725296/difference-between-sh-and-bash) (particularly insofar as you're using syntax that would work with bash, but then running `sh yourscript` instead of `bash yourscript` or just `./yourscript`) – Charles Duffy Aug 22 '23 at 16:31
  • Thanks for the comments @CharlesDuffy. It was solved by using `bash` instead of `sh`. Although both `sh` and `bash` worked fine on local machines both linux and OSX, it's just CircleCI that caused this issue. – ashraf minhaj Aug 26 '23 at 06:55
  • Your local machines are probably running a distro where sh is provided by bash (this is true on Red Hat derivatives, for example) while your CircleCI systems are probably running an image (like Ubuntu or Alpine) where sh is provided by dash or ash. Bash disables _some_ features when called under the sh name, but not the full set of its extensions. – Charles Duffy Aug 26 '23 at 12:42

0 Answers0