Gitlab CI deploy AWS EC2

2020-04-19 08:33发布

we have a lumen application we move the project to GitLab we wanna pull the project if all is ok.

We add the two scripts:

.gitlab-ci.yml:

variables:
 - All or variables
stages:
  - test
  - production

testing:
  type: test
  image: php:7.1
  script:
    - echo "ok"

#Production stage
production:   
   stage: production   
   before_script: 
     - mkdir -p ~/.ssh     
     - echo -e "$SSH_PRIVATE_KEY" > ~/.ssh/id_rsa     
     - chmod 600 ~/.ssh/id_rsa     
     - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'   
   script:     
      - bash .gitlab-deploy.sh   
   environment:     
      name: production     
      url: https://alpha.merci.network/
   when: manual

our deploy script ".gitlab-deploy.sh" looks like:

#!/bin/bash

#Get servers list
set -f
string=$DEPLOY_SERVER
array=(${string//,/ })

#Iterate servers for deploy and pull last commit
for i in "${!array[@]}"do    
      echo "Deploy project on server ${array[i]}"    
      ssh ubuntu@${array[i]} "cd /var/www && git pull origin master"
done

We already add the configuration:

enter image description here

When we push changes to the repo/master we saw on the dashboard log the next error:

enter image description here

So what we are missing? Any advice?

1条回答
Fickle 薄情
2楼-- · 2020-04-19 09:25

I just change the for to this for make it work:

for i in "${!array[@]}"; do
  echo "Deploying information to EC2 and Gitlab"
  echo "Deploy project on server ${array[i]}"
  ssh ubuntu@${array[i]} "cd  /var/www/merci_deploy_api && git pull origin master"
done
查看更多
登录 后发表回答