如何解决如何使用 travis ci 配置 SSH 连接?
我正在尝试为 docker 项目设置 travis ci 配置,但我在我的 .travis.yml 中卡在与服务器的 ssh 连接上。我找不到这个视频 https://www.youtube.com/watch?v=xLWDOLhTH38&ab_channel=DevOpsJourney 的 yml 文件,我已经将我的 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
解决方法
经过多次尝试和测试,我做到了这一点,我很高兴它像我想要的那样工作,但我确信它不是那么好。
- 创建您的 Travis CI 帐户
- 启用与您的 Github 存储库的连接
- 将 .travis.yml 文件添加到您的项目的 src 目录中(这也应该是您的 git 存储库,但您可以根据需要对其进行编辑)
- 由于我们将部署一个 docker 项目,因此我们需要在部署之前测试 docker build 是否有效。这里部署将在我们的私人服务器上,这就是为什么我们需要从 Travis 服务器使用 SSH 连接到我们的服务器。
enter image description here
- 我不知道即使您拥有 SSH 密钥,如果防火墙规则阻止您,您也无法连接到服务器?。因此,如果您的 docker 应用程序必须连接到任何服务器,请不要忘记将 Travis 服务器的 IP 地址添加到这些服务器的白名单(防火墙规则)(来自您的主机提供商平台或您的服务器命令行)。查找 Travis CI 服务器的当前 IP 地址 ➢ https://dnsjson.com/nat.travisci.net/A.json
- 对于 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"
-
现在你的私钥在 id_rsa 文件中,你的公钥在 id_rsa.pub 中
- cat /id_rsa
- 将其复制并粘贴到项目的 Travis ci SSH 密钥配置 (https://travis-ci.com/github/[githubUserName]/[github_repo]/settings)
- cat /id_rsa.pub
- 复制粘贴到你的github项目设置的deploy key中
- 现在 Travis 的 SSH 代理会为您完成这项工作
-
在您的 .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},请转到您的项目设置 enter image description here
- 对文件中未解释的内容进行一些演练
- 制作 arch: amd64 ==> 选择 CPU 架构
- services: docker ==> 将启动 docker 服务
- 建立 SSH 连接后
- 更新 git 仓库
- mvn clean install 用于目标文件夹和 jar
- 构建您的 docker 镜像,将其推送到 docker hub
- 拉取并运行它
- “docker stop ${DOCKER_CONTAINER_NAME} || true && docker rm ${DOCKER_CONTAINER_NAME} || true” ==> 如果 docker 容器不存在
此外,如果您希望它们隐藏在 Travis ci 日志中,您可以将 ${anyValue} 添加到 Travis 设置中。
感谢您的时间。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。