首页 > 解决方案 > 如何使用 travis ci 配置 SSH 连接?

问题描述

我正在尝试为 docker 项目设置 travis ci 配置,但我在 .travis.yml 中卡在与服务器的 ssh 连接上。我找不到此视频的 yml 文件https://www.youtube.com/watch?v=xLWDOLhTH38&ab_channel=DevOpsJourney,我已经将我的 ssh 私钥添加到 travis ci 设置中,以及我当前工作的 .travis.yml文件是:

arch:
  - amd64

env:
  - IMGNAME=myImageName

language: java

services:
  - docker

before_install:
  - mvn clean install
  - docker build -t ${IMGNAME} .

script:
  - echo test

after_success:
  - docker login -u ${DHUB_USERNAME} -p ${DHUB_PASSWORD}
  - echo test

标签: javadockersshtravis-cissh-keys

解决方案


经过大量尝试和测试后,我做到了这一点,我很高兴它按我想要的方式工作,但我确信它不是那么好。

  1. 创建您的 Travis CI 帐户
  2. 启用与您的 Github 存储库的连接
  3. .travis.yml文件添加到项目的 src 目录(这也应该是您的 git 存储库,但您可以根据需要对其进行编辑)
  4. 由于我们将部署一个 docker 项目,因此我们需要在部署之前测试 docker 构建是否有效。在这里部署将在我们的私有服务器上,这就是为什么我们需要从 Travis 服务器使用 SSH 连接到我们的服务器。 在此处输入图像描述
    1. 我不知道即使您有 SSH 密钥,如果有防火墙规则阻止您,您也无法连接到服务器。因此,如果您的 docker 应用程序必须连接到任何服务器,请不要忘记将 Travis 服务器的 IP 地址添加到这些服务器的白名单(防火墙规则)中(来自您的主机提供商平台或服务器命令行)。查找 Travis CI 服务器的当前 IP 地址 ➢ https://dnsjson.com/nat.travisci.net/A.json
    2. 对于 SSH 密钥,在生成您的密钥后,将其添加到您服务器的授权密钥中
// From your (windows, I don't know how it is for other OS but I think that it is pretty the same) computer execute this command to generate your SSH key,
// to make it in pem format which is needed for the travis ci ssh key format,
// accept all default paramaters, no paraphrase (travis doesn't like it),
// and for the location paramater also (you can change it if you want)

ssh-keygen -m PEM -t rsa -b 4096 -C "your_email@example.com"

// Inside your server execute this command
// this will add your SSH key to your server authorized SSH keys
type $env:USERPROFILE\.ssh\id_rsa.pub | ssh [user]@[yourServerIP] "cat >> .ssh/authorized_keys"
  1. 现在您的私钥在 id_rsa 文件中,而您的公钥在 id_rsa.pub 中

    1. 猫 /id_rsa
    2. 将其复制并粘贴到项目的 Travis ci SSH 密钥配置中(https://travis-ci.com/github/[githubUserName]/[github_repo]/settings
    3. 猫 /id_rsa.pub
    4. 将其复制并粘贴到您的 github 项目设置的部署密钥中
    5. 现在 Travis 的 SSH 代理将为您完成工作
  2. 在你的 .travis.yml 中:

arch:
  - amd64

language: java #Default install Gradle, Maven, Ant
sudo: true # ./mvnw ==> for the permission denied error 

services:
  - docker

before_install:
  - chmod +x mvnw # ./mvnw ==> for the permission denied error

script:
  - mvn clean install # not sure if it's useful before a docker build
  - docker build -t ${DHUB_TAG} .

after_success:
  - eval "$(ssh-agent -s)"
  #- chmod 600 ./deploy_key ==> don't need that anymore
  - echo -e "Host ${SERVER_IP_ADDRESS}\n\tStrictHostKeyChecking no\n" >> ~/.ssh/config
  #- ssh-add ./deploy_key ==> don't need that anymore
  - ssh ${SERVER_USER}@${SERVER_IP_ADDRESS} "
    cd ${WORK_FOLDER} &&
    git config user.name ${GITHUB_USERNAME} &&
    git config user.email ${GITHUB_EMAIL} &&
    git pull 'https://${GITHUB_USERNAME}:${GITHUB_PASSWORD}@github.com/${GITHUB_REPO}' production &&
    git config --unset-all user.name &&
    git config --unset-all user.email &&
    sudo apt update &&
    sudo apt install maven &&
    mvn clean install &&
    docker login -u ${DHUB_USERNAME} -p ${DHUB_PASSWORD} &&
    docker build -t ${DHUB_TAG} . &&
    docker push ${DHUB_TAG} &&
    docker pull ${DHUB_TAG} &&
    docker stop ${DOCKER_CONTAINER_NAME} || true && docker rm ${DOCKER_CONTAINER_NAME} || true &&
    docker run -p 8080:8080 --name=${DOCKER_CONTAINER_NAME} -d ${DHUB_TAG}"
  • 要添加您的 ${params} 转到您的项目设置,请 在此处输入图像描述
  • 文件中未解释的内容的小演练
    • make arch: amd64 ==> 选择 CPU 架构
    • services: docker ==> 将启动 docker 服务
    • 建立 SSH 连接后
      • 更新 git 仓库
      • 目标文件夹和 jar 的mvn clean install
      • 构建您的 docker 映像,将其推送到 docker hub
      • 拉动它并运行它
      • "docker stop ${DOCKER_CONTAINER_NAME} || true && docker rm ${DOCKER_CONTAINER_NAME} || true" ==> 如果 docker 容器不存在

此外,如果您希望将 ${anyValue} 隐藏在 Travis ci 日志中,可以将它们添加到 Travis 设置中。

谢谢你的时间。


推荐阅读