Thursday, April 7, 2022

[SOLVED] shell script with multiple variables

Issue

I'm deploying docker containers on seperate VM's with nginx installed running on differnt ports I have three environments dev, qa and prod.

Now i have 2 ports for dev, 2 ports for qa and 2 ports for prod.

docker run -p $PORT1:$PORT1 --env PORT:$PORT1 -d --name demoapp sample/demoapp

docker run -p $PORT2:$PORT2 --env PORT:$PORT2 -d --name demoapp sample/demoapp

I want to automate this just by giving BRANCH = "DEV" or "QA" or "PROD" in jenkins pipeline.

I'm trying to use a for loop with if.


Solution

Perhaps something like this

run_docker() {
  docker run -p "$1:$1" --env PORT:"$1" -d --name demoapp sample/demoapp
  docker run -p "$2:$2" --env PORT:"$2" -d --name demoapp sample/demoapp
}

case "$BRANCH" in
  DEV)  run_docker 8001 8002 ;;
  QA)   run_docker 4001 4002 ;;
  PROD) run_docker 81 82 ;;
  *) echo "unknown branch: $BRANCH" >&2; exit 1 ;;
esac


Answered By - glenn jackman
Answer Checked By - Mary Flores (WPSolving Volunteer)