deploy: stage:deploy image:docker:latest before_script: ## Install ssh-agent if not already installed -'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )' ## Run ssh-agent -eval$(ssh-agent-s) ## ## Give the right permissions, otherwise ssh-add will refuse to add files ## Add the SSH key stored in SSH_PRIVATE_KEY file type CI/CD variable to the agent store ## -chmod400"$SSH_PRIVATE_KEY" -ssh-add"$SSH_PRIVATE_KEY" ## Create the SSH directory and give it the right permissions -mkdir-p~/.ssh -chmod700~/.ssh
# 每个任务执行前都会执行下面这段代码 default: before_script: ## Install ssh-agent if not already installed -'command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y )' ## Run ssh-agent -eval$(ssh-agent-s) ## ## Give the right permissions, otherwise ssh-add will refuse to add files ## Add the SSH key stored in SSH_PRIVATE_KEY file type CI/CD variable to the agent store ## -chmod400"$SSH_PRIVATE_KEY" -ssh-add"$SSH_PRIVATE_KEY" ## Create the SSH directory and give it the right permissions -mkdir-p~/.ssh -chmod700~/.ssh ## Verify the SSH host key -ssh-keyscanxx.xx.xx.xx>>~/.ssh/known_hosts
#!/bin/bash ## Install ssh-agent if not already installed command -v ssh-agent >/dev/null || ( apt-get update -y && apt-get install openssh-client -y ) ## Run ssh-agent eval $(ssh-agent -s) ## ## Give the right permissions, otherwise ssh-add will refuse to add files ## Add the SSH key stored in SSH_PRIVATE_KEY file type CI/CD variable to the agent store ## chmod 400 "$SSH_PRIVATE_KEY" ssh-add "$SSH_PRIVATE_KEY" ## Create the SSH directory and give it the right permissions mkdir -p ~/.ssh chmod 700 ~/.ssh ## Verify the SSH host key ssh-keyscan xx.xx.xx.xx >> ~/.ssh/known_hosts