简体   繁体   English

创建 Jenkins 作业以在服务器上部署文件

[英]Create a Jenkins job to deploy a file on a server

I am working on a Jenkins job that will download a specific file from a GIT repo and then deploy it on a server to a specified location.我正在处理 Jenkins 作业,该作业将从 GIT 存储库下载特定文件,然后将其部署到服务器上的指定位置。 Jenkins job will ask below variables from the user: Jenkins 作业将向用户询问以下变量:

  1. Commit ID and name of the file in repo that needs to be copied.需要复制的 repo 中文件的提交 ID 和名称。
  2. Path on the target server where the file needs to be copied.需要复制文件的目标服务器上的路径。

Here's what I have done so far:这是我到目前为止所做的:

  • Installed and configured the GIT so it will pull the repo to the Jenkins server安装并配置了 GIT,因此它将 repo 拉到 Jenkins 服务器
  • Configured the target servers so Jenkins can copy the file to one of the 3 target servers using 'execute shell option'.配置目标服务器,以便 Jenkins 可以使用“执行 shell 选项”将文件复制到 3 个目标服务器之一。
  • In the build action, wrote an ssh command that will checkout a specific commit of a file from the git repo.在构建操作中,编写了一个 ssh 命令,该命令将从 git 存储库中检出文件的特定提交。
  • Wrote a shell command that will scp the checked out file and to the one of the 3 target servers.写了一个 shell 命令,它将 scp 签出的文件和 3 个目标服务器之一。 Of course the target server will be provided by the user.当然目标服务器将由用户提供。

Here's what the code looks like so far:到目前为止,代码如下所示:

git checkout $commit $filename
scp $filename username@$servername:/path/to/deploy/

The challenge is how to feed the variables to the shell script.挑战在于如何将变量提供给 shell 脚本。 Is it even possible to do it this way?甚至可以这样做吗? Any help would be appreciated.任何帮助,将不胜感激。

Below solution worked.以下解决方案有效。

Added the GIT repo to the Jenkins freestyle job将 GIT 回购添加到 Jenkins 自由式作业在此处输入图像描述

After checking the option, 'this project is parameterized', I added 3 variables as shown.选中选项“此项目已参数化”后,我添加了 3 个变量,如图所示。 在此处输入图像描述

Then modified the shell script as below然后修改shell脚本如下在此处输入图像描述

#!/bin/bash
pwd
ssh ansible@$server_name "cp "$File_path"/"$file_name" "$File_path"/"$file_name"_"$(date +"%Y-%m-%d")";"
scp $file_name ansible@$server_name:$File_path

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM