Создайте автоматическое развертывание с помощью GitLab CI за 10 минут
Шаг 1. Добавьте открытый ключ в профиль GitLab
Добавьте открытый ключ сервера в открытые ключи профиля GitLab. Это делается путем копирования содержимого cat ~/.ssh/id_rsa.pub
и вставки его в https://gitlab.com/profile/keys.
Шаг 2. Добавьте открытый ключ к авторизованным ключам
Добавьте открытый ключ сервера к авторизованным ключам сервера. Внутри сервера это делается простым запуском этой строки:
cat ~/.ssh/id_rsa.pub >> ~/.ssh/authorized_keys
Шаг 3. Добавьте закрытый ключ в проект GitLab
Добавьте закрытый ключ сервера в переменные проекта Gitlab. Это делается путем копирования содержимого cat ~/.ssh/id_rsa
и вставки его в https://gitlab.com/username/project/-/settings/ci_cd с именем SSH_PRIVATE_KEY
.
Шаг 4. Создайте файл .gitlab-ci-yml
Создайте файл .gitlab-ci.yml
в корневом проекте и поместите следующий код:
before_script: ## Install ssh-agent if not already installed, it is required by Docker. ## (change apt-get to yum if you use an RPM-based image) ## - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )' ## Run ssh-agent (inside the build environment) ## - eval $(ssh-agent -s) ## Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store ## We're using tr to fix line endings which makes ed25519 keys work ## without extra base64 encoding. ## https://gitlab.com/gitlab-examples/ssh-private-key/issues/1#note_48526556 ## - ssh-add <(echo "$SSH_PRIVATE_KEY") ## Create the SSH directory and give it the right permissions ## - mkdir -p ~/.ssh - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' ## Optionally, if you will be using any Git commands, set the user name and email. ## #- git config --global user.email "[email protected]" #- git config --global user.name "User name" stages: - deploy_production deploy_production: stage: deploy_production type: deploy environment: production script: - ssh ubuntu@yourIP "cd /var/www/project && git checkout master && git pull && exit" only: - master
Спасибо, что прочитали. Если вам это нравится… Дайте мне аплодисменты и следуйте за мной!!