如何通过 .gitlab-ci.yml 文件将存储库克隆或拉取到 Ubuntu 服务器上

问题描述 投票:0回答:1

我有一个托管在 GitLab.com 上的存储库和一个运行 Ubuntu Server 的 EC2 实例。我可以通过 SSH 连接到服务器并克隆存储库,Apache2 可以很好地提供服务。但是,当我推送更改时,如何设置 .gitlab-ci.yml 来自动执行此操作。

我想管道将使用密钥对通过 SSH 登录服务器,然后运行一些 glab 和 git 命令来拉取更改

这可能吗?

到目前为止,我只能手动登录服务器并运行 glab 和 git 命令。 IE:glab auth登录...和git克隆...

apache ubuntu gitlab
1个回答
0
投票

这是一个简单而基本的示例.gitlab-ci.yml,请按照以下步骤操作。

将 SSH 私钥存储在 GitLab CI/CD 环境变量中:

  1. 导航到设置 > CI / CD > 变量。
  2. 添加一个名为 SSH_PRIVATE_KEY 的新变量。
  3. 将 SSH 私钥的内容粘贴到值字段中。

使用下面的 .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

  1. 我们将 SSH 私钥内容存储到临时文件 /tmp/ssh_key 中。
  2. 我们为临时 SSH 密钥文件设置适当的权限 (chmod 600),以确保它只能由所有者读取。
  3. 我们在 SSH 命令执行期间使用临时 SSH 密钥文件 (/tmp/ssh_key) 对 EC2 实例进行身份验证。
© www.soinside.com 2019 - 2024. All rights reserved.