我有一个托管在 GitLab.com 上的存储库和一个运行 Ubuntu Server 的 EC2 实例。我可以通过 SSH 连接到服务器并克隆存储库,Apache2 可以很好地提供服务。但是,当我推送更改时,如何设置 .gitlab-ci.yml 来自动执行此操作。
我想管道将使用密钥对通过 SSH 登录服务器,然后运行一些 glab 和 git 命令来拉取更改
这可能吗?
到目前为止,我只能手动登录服务器并运行 glab 和 git 命令。 IE:glab auth登录...和git克隆...
这是一个简单而基本的示例.gitlab-ci.yml,请按照以下步骤操作。
将 SSH 私钥存储在 GitLab CI/CD 环境变量中:
使用下面的 .gitlab-ci.yml 文件
stages:
- deploy
deploy:
stage: deploy
script:
- echo "$SSH_PRIVATE_KEY" > /tmp/ssh_key # Save the SSH key to a temporary file
- chmod 600 /tmp/ssh_key # Set appropriate permissions for the SSH key file
- ssh -i /tmp/ssh_key user@your_ec2_instance_ip "cd /path/to/your/project && git pull origin master"
environment:
name: production # Optionally, specify the environment name
only:
- master # Optionally, specify the branch to trigger the deployment