Ubuntu & GitLab CI & Do

前端之家收集整理的这篇文章主要介绍了Ubuntu & GitLab CI & Do前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。



为明天做准备的最好方法就是集中你所有智慧,所有的热忱,把今天的工作做得尽善尽美。


简介


相关博文:

  • Ubuntu 简单安装和配置 GitLab(http://www.cnblogs.com/xishuai/p/ubuntu-install-gitlab.html

  • Ubuntu 简单安装 Docker(http://www.cnblogs.com/xishuai/p/ubuntu-install-docker.html

  • Ubuntu Docker 简单安装 GitLab(http://www.cnblogs.com/xishuai/p/ubuntu-install-gitlab-with-docker.html

  • Ubuntu Docker 安装和配置 GitLab CI 持续集成(http://www.cnblogs.com/xishuai/p/ubuntu-install-gitlab-runner-with-docker.html

服务器版本 Ubuntu 16.04 LTS。

经过上面四篇博文中的相关安装和配置,我们主要完成了两个容器的创建和运行:gitlabgitlab-runnerGitLab 站点GitLab CI 服务):

$ docker ps CONTAINER ID        IMAGE                         COMMAND                  CREATED             STATUS                PORTS                                                            NAMES 696d559ce382        gitlab/gitlab-runner:latest   "/usr/bin/dumb-ini..."   5 days ago          Up 25 minutes                                                                          gitlab-runner ff95f354200d        gitlab/gitlab-ce:latest       "/assets/wrapper"        7 days ago          Up 6 days (healthy)   0.0.0.0:80->80/tcp,0.0.0.0:443->443/tcp,0.0.0.0:8888->22/tcp   gitlab

本篇博文目的:使用 GitLab CI 脚本编译 ASP.NET Core 2.0 程序,然后将编译后的文件传输到服务器上,最后使用 SSH 连接服务器,并运行程序,完成发布和部署。

简单来说,就是我们每次使用git push提交完代码自动完成发布和部署。

我们再理一下实现上面目的关键点:

  1. 创建一个 ASP.NET Core 2.0 示例程序

  2. 完善并正确的 .gitlab-ci.yml 文件配置

  3. GitLab CI 服务器使用 ssh 连接到测试服务器(在 Docker 中)

  4. 使用 scp 进行服务器之间的文件传输

  5. 使用 supervisor 进行站点程序的进程管理

我花了很长时间配置第三步,其实最后解决也很简单,当然都是马后炮的结论,下面我们分别来进行操作。


创建 ASP.NET Core 2.0 示例程序


我自己创建示例程序:http://40.125.206.47/team/hwapp

注:服务器快过期了,大家可以随便搞。

自己创建的话,也很简单,官方教程:https://www.microsoft.com/net/core#linuxubuntu

我再搬运下命令(安装 .NET Core 2.0,并创建 ASP.NET Core 2.0 示例程序):

$ curl https://packages.microsoft.com/keys/microsoft.asc | gpg --dearmor > microsoft.gpg $ sudo mv microsoft.gpg /etc/apt/trusted.gpg.d/microsoft.gpg $ sudo sh -c 'echo "deb [arch=amd64] https://packages.microsoft.com/repos/microsoft-ubuntu-xenial-prod xenial main" > /etc/apt/sources.list.d/dotnetdev.list' $ sudo apt-get update $ sudo apt-get install dotnet-sdk-2.0.0 $ dotnet new webapi -o hwapp $ cd hwapp


最后,绑定下 ASP.NET Core 2.0 程序端口:

public class Program {    public static void Main(string[] args)    {        BuildWebHost(args).Run();    }    public static IWebHost BuildWebHost(string[] args) =>        WebHost.CreateDefaultBuilder(args)            .UseKestrel() //add code            .UseUrls($"http://*:8088") //add code            .UseStartup<hwapp.Startup>()            .Build(); }


.gitlab-ci.yml 文件配置


我的 .gitlab-ci.yml 文件配置 (http://40.125.206.47/team/hwapp/blob/master/.gitlab-ci.yml
image: microsoft/aspnetcore-build stages:  - build  - deploy_dev before_script:  # Install ssh-agent if not already installed,it is required by Docker.  # (change apt-get to yum if you use a CentOS-based image)  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'  # Run ssh-agent (inside the build environment)  - eval $(ssh-agent -s)  # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store  # error: https://gitlab.com/gitlab-examples/ssh-private-key/issues/1  # - echo "$SSH_PRIVATE_KEY_DEV"  - ssh-add <(echo "$SSH_PRIVATE_KEY_DEV")  # For Docker builds disable host key checking. Be aware that by adding that  # you are suspectible to man-in-the-middle attacks.  # WARNING: Use this only with the Docker executor,if you use it with shell  # you will overwrite your user's SSH config.  - mkdir -p ~/.ssh  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config' build_job:  stage: build  only:    - master  script:    - dotnet restore    - dotnet build deploy_dev_job:  stage: deploy_dev  environment:    name: development  only:    - master  script:    # 发布程序并部署运行    - dotnet publish -c Release --output bin/publish    - scp -r bin/publish root@$DEPLOY_SERVER_DEV:/home/xishuai/wwwroot/hwapp    - ssh root@$DEPLOY_SERVER_DEV "supervisorctl restart hwapp && curl http://localhost:8088/api/values"

上面是我最终调试成功后的 .gitlab-ci.yml 文件配置,其实整个的构建和发布流程,从上面的配置中都可以看出。

这里记录下一些东西:

配置一开始的 image,设置的是我们用于构建的镜像(也就是说后面所有的脚本执行,都是在基于这个镜像创建的容器中),如果不设置的话,默认使用的是我们一开始配置 GitLab CI 填写的 Docker Image,也可以手动编辑 vim /srv/gitlab-runner/config/config.toml 进行修改,我这里使用的是  microsoft/aspnetcore-build 镜像,只用于 ASP.NET Core 应用程序的编译和构建。

stage 可以理解为台阶,每走一步相当于 job,当然,这里的台阶可以走很多步,需要注意的是,每上一个台阶或者每走一步,都必须基于上一个台阶或上一步执行成功,before_script 执行在这些步骤之前,可以理解为准备工作。

 environment 将执行的 job 归纳为哪一种执行环境,你可以设置开发环境和正式环境,我们可以通过通过后台进行查看:

@H_527_301@


GitLab CI 鼓舞器使用 SSH 连接到测试服务器


什么意思呢?就是我们需要在 GitLab CI 构建环境中,使用 SSH 连接到测试服务器,这样我们才可以做接下来的一些操作。

官方配置:SSH keys when using the Docker executor(https://docs.gitlab.com/ce/ci/ssh_keys/README.html#ssh-keys-when-using-the-docker-executor

.gitlab-ci.yml示例配置:

before_script:  # Install ssh-agent if not already installed,it is required by Docker.  # (change apt-get to yum if you use a CentOS-based image)  - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'  # Run ssh-agent (inside the build environment)  - eval $(ssh-agent -s)  # Add the SSH key stored in SSH_PRIVATE_KEY_DEV variable to the agent store  - ssh-add <(echo "$SSH_PRIVATE_KEY_DEV")  # For Docker builds disable host key checking. Be aware that by adding that  # you are suspectible to man-in-the-middle attacks.  # WARNING: Use this only with the Docker executor,if you use it with shell  # you will overwrite your user's SSH config.  - mkdir -p ~/.ssh  - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'  # In order to properly check the server's host key,assuming you created the  # SSH_SERVER_HOSTKEYS variable prevIoUsly,uncomment the following two lines  # instead.  # - mkdir -p ~/.ssh  # - '[[ -f /.dockerenv ]] && echo "$SSH_SERVER_HOSTKEYS" > ~/.ssh/known_hosts'

在进行配置之前,我们需要整理一下这个步骤,要不然思路容易混乱,会造成一些问题,可以参考这篇文章Fixing 'Enter passphrase for /dev/fd/63' in a Gitlab CI job(https://techsparx.com/software-development/gitlab-ci-enter-passphrase.html

需要强调一点:别在 GitLab CI 容器中进行 SSH 配置,因为 CI 构建脚本执行在另外的容器中,并且这个容器是动态进行创建的,也没办法在这个动态容器中进行配置(指的是手动生成 RSA 密钥)。

所以,我们只能手动生成 RSA 密钥,然后强制添加到容器中的 SSH 配置中(通过 RSA 密钥内容)。

配置步骤:

首先,在任何一台服务器上,创建 RSA 无密码的密钥

$ ssh-keygen -t rsa -P '' $ cat /root/.ssh/id_rsa


然后复制 RSA 密钥内容添加/Project/Settings/PipelinesSecret variables 配置中(命名为 SSH_ORUVATE_KEY_DEV):


这里需要特别注意,复制内容为(包含开头和结尾的注释信息):

-----BEGIN RSA PRIVATE KEY----- xxxxxxx -----END RSA PRIVATE KEY-----


我一开始复制没有包含注释信息,然后就一直报下面的错误


错误代码

$ ssh-add <(echo "$SSH_PRIVATE_KEY_DEV") Enter passphrase for /dev/fd/63: ERROR: Job Failed: exit code 1

这里的 $SSH_PRIVATE_KEY_DEV,就是上面我们在 Secret variavles 中,添加RSA 密钥内容

错误信息就是说需要输入 RSA 密钥的密码,但我创建的确实是无密码的 RSA 密钥,也就是说这个密钥是无效的,我被这个问题折磨了好几天,其他人的记录:

  • "Enter passphrase for /dev/fd/63" errorhttps://gitlab.com/gitlab-examples/ssh-private-key/issues/1)(有我的回复

  • "Enter passphrase for /dev/fd/63" errorhttps://gitlab.com/gitlab-org/gitlab-ce/issues/14434

  • https://gitlab.com/gitlab-examples/ssh-private-key/-/jobs/376082受这个兄弟的启发)

配置好这一步之后,然后重新测试下,我们就可以看到下面的执行信息了:

$ ssh-add <(echo "$SSH_PRIVATE_KEY_DEV") Identity added: /dev/fd/63 (/dev/fd/63)


接着我们需要将这个 RSA 密钥对应的公钥,上传需要连接到的服务器(也就是我们的测试服务器),命令如下:

$ ssh-copy-id root@40.125.201.75


到此,GitLab CI 中 SSH 的配置基本上完成了,你可以在.gitlab-ci.yml添加连接脚本,进行测试:

- ssh root@$DEPLOY_SERVER_DEV "ls && cd /"


一开始,我们说到使用scp进行服务器之间的文件传输,因为scp可以基于 SSH 连接进行传输文件,所以我们直接进行文件传输了,示例代码

- scp -r bin/publish root@$DEPLOY_SERVER_DEV:/home/xishuai/wwwroot/hwapp

scp 命令参考:http://www.runoob.com/linux/linux-comm-scp.html


使用 Supervisor 进行站点程序的进程管理


可以参考之前的文章Ubuntu 安装和使用 Supervisor(进程管理)

这里贴一下,supervisorctl 的常用命令:

命令 说明
supervisorctl stop program_name 停止某个进程
supervisorctl start program_name 启动某个进程
supervisorctl restart program_name 重启某个进程
supervisorctl stop all 停止全部进程
supervisorctl reload 载入最新的配置文件,停止原有进程并按新的配置启动、管理所有进程
supervisorctl update 根据最新的配置文件,启动新配置或有改动的进程,配置没有改动的进程不会受影响而重启


最终效果


Pipelines 管道(地址:http://40.125.206.47/team/hwapp/pipelines)


Build_Job 构建任务(地址:http://40.125.206.47/team/hwapp/-/jobs/113


Deploy_Dev_Job 发布和部署任务(地址:http://40.125.206.47/team/hwapp/-/jobs/115


结语


  • GitLab CI & ASP.NET Core 2.0 发布和部署(完成):使用 CI 脚本编译程序,然后将编译后的文件传输到服务器上,最后运行程序,完成发布和部署。

  • GitLab CI & ASP.NET Core 2.0 & Docker 发布和部署(下篇):项目中添加 Dockerfile 文件,使用 CI 脚本构建自定义镜像,然后在服务器上拉取并创建相应容器,最后启动容器,完成发布和部署。


作者:田园里的蟋蟀 
出处:http://www.cnblogs.com/xishuai/ 
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接。


技术交流QQ群:238757010


微信后台回复 python、自动化即可获得相应课程的试听资料




运维自动化班 6 期报名开始


课程概述:


理论结合实战,使学员既可掌握快速从零构建一套实用、完整、可扩展的运维自动化平台。



1


深度结合使用流行的 Zabbix、Ansible、Git、Docker、Rancher、ELK 等开源框架与工具, 以为应用最广泛的 Django 框架为基础,构建一站式运维自动化平台。



2


通过深度剖析与二次开发定制,结合 REST API、运维流程化、运维可视化、运维平台化 思想来构造企业级的运维自动解决方案。



3


在老师带领下大战 Zabbix、CMDB、集群自动化部署上线、ELK 日志大数据分析、Docker 容器管理平台等多个最新实战,天天实战,招招实用。



上课模式:网络直播班    线下面授班


咨询报名联系:

QQ(1):979950755    小月   

QQ(2):279312229    ada   

WeChat : 1902433859   小月

WeChat : 1251743084   小单


开课时间12月17日(周日)

课程大纲http://51reboot.com/course/devops/

(阅读原文,即可跳转

猜你在找的Ubuntu相关文章