---
url: /dev/4vp15lv0/index.md
description: 使用 JetBrains TeamCity 搭建 CI/CD 流水线实现 Hexo 博客自动化部署，比 Jenkins 配置更简单直观。
---
## 使用TeamCity实现hexo自动化部署

### 前言

之前使用`Jenkins` 来跑一些自己的项目，例如java程序和博客，我都是使用`Jenkins`配合docker来使用（感兴趣的朋友可以看我之前的文章），但是
`Jenkins`的配置较为繁琐，流水线文件`Jenkinsfile`也需要一定的脚本语言功底，尤其是部署配置到其他机器时，配置还是比较繁琐的，于是，几经搜索，最后选择了
`TeamCity`来做自动化，综合体验下来，确实比`Jenkins` 要香很多！

官网地址奉上：[TeamCity: the Hassle-Free CI/CD Tool by JetBrains](https://www.jetbrains.com/teamcity/)

### 概念

#### CI/CD

首先再来回顾下DevOps和CI/CD......（此处省略1w字）

[DevOps和CI/CD - 知乎 (zhihu.com)](https://zhuanlan.zhihu.com/p/360154972)

[什么是 CI/CD？ (持续集成/持续交付)\_guofangsky 的专栏-CSDN博客\_ci/cd](https://blog.csdn.net/guofangsky/article/details/89684857)

#### 是否收费

`否`

看一下官网的价格

![image-20221107211812513](https://bed.piemoe.com/images/2022/11/07/202211072118097.png)

可以看到，如果我们的构建代理只有3个及以下，我们完全可以免费用于即使是商业用途的情况。

### 前提

1. 云服务器/vps，保证内存达到2G及以上（1GB体验很差）
2. 版本控制，如Github/Gitee/Gitlab等
3. 项目需要的构建环境的，如JDK、node等等

### 正文

本次实现`hexo`自动化部署

#### 部署TeamCity

`TeamCity` 部署需要2个步骤，分别是 `server` 端和`agent`端

注意，`agent`端最好在`server`端后启动。

##### server 端

提供一个可视化的面板，使我们可以定制配置构建内容。

###### 使用docker compose部署

```yaml
version: "3"
services:
  teamcity-server:
    user: root
    privileged: true
    container_name: teamcity-server
    image: jetbrains/teamcity-server
    volumes:
      - /data/teamCity/data:/data/teamcity_server/datadir
      - /data/teamCity/log:/opt/teamcity/logs
    ports:
      - "8111:8111"
```

当看到一下内容时，代表服务端启动正常

```shell
Startup confirmation is required. Open TeamCity web page in the browser. Server is running at http://localhost:8111
```

##### agent端

放置于项目构建的位置，主要用于构建项目，和`server`端可以不在同一服务器

> \[!warning]
> `agent` 端也可以用docker部署，但会发生很多意外！且低配服务器由于内存小等原因，容易出现oom，所以建议在`server`端中直接push
> `agent`端，详细请看`配置`。

###### 使用docker compose部署（不推荐docker部署）

```yaml
version: "3"
services:
  teamcity-agent-01:
    user: root # 使用root账号 便于创建docker镜像
    container_name: teamcity-agent01
    image: jetbrains/teamcity-agent:2022.10-linux-sudo
    privileged: true # 赋予真正的权限
    environment:
      - SERVER_URL=http://ip:8111 # teamcity-server 地址
      - AGENT_NAME=teamcity-agent01 # agent端名称
      - DOCKER_IN_DOCKER=start # 允许构建docker程序
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock # 允许构建docker程序
      - /data/teamCity/agent/work:/opt/buildagent/work
      - /data/teamCity/agent/temp:/opt/buildagent/temp
      - /data/teamCity/agent/tools:/buildagent/tools
      - /data/teamCity/agent/plugins:/opt/buildagent/plugins
      - /data/teamCity/agent/system:/opt/buildagent/system
```

#### 单机版部署

```yaml :collapsed:lines
version: "3"
networks:
  env_default:
    external: true

services:
  teamcity-server:
    user: root
    privileged: true
    container_name: teamcity-server
    image: jetbrains/teamcity-server
    volumes:
      - /data/teamCity/data:/data/teamcity_server/datadir
      - /data/teamCity/log:/opt/teamcity/logs
    ports:
      - "8111:8111"
    networks:
      - env_default
  teamcity-agent01:
    user: root
    container_name: teamcity-agent01
    image: jetbrains/teamcity-agent:2022.10-linux-sudo
    privileged: true
    environment:
      - SERVER_URL=http://db:8111
      - AGENT_NAME=teamcity-agent01
      - DOCKER_IN_DOCKER=start
    volumes:
      - /var/run/docker.sock:/var/run/docker.sock
      - /data/teamCity/agent/work:/opt/buildagent/work
      - /data/teamCity/agent/temp:/opt/buildagent/temp
      - /data/teamCity/agent/tools:/buildagent/tools
      - /data/teamCity/agent/plugins:/opt/buildagent/plugins
      - /data/teamCity/agent/system:/opt/buildagent/system
    depends_on:
      - teamcity-server
    external_links:
      - teamcity-server:db #可以用db这个域名访问mysql服务
    networks:
      - env_default
```

### 配置

#### 访问服务端

```http
http://ip:8111
```

#### 配置数据库

![image-20221109150800613](https://bed.piemoe.com/images/2022/11/09/202211091508033.png)

如果你是之前使用过的，选择`Restore from backup`，如果是初次，点击`Proceed`。

![image-20221109150942366](https://bed.piemoe.com/images/2022/11/09/202211091509974.png)

这里选择存储方式，建议使用`Mysql`方便迁移数据，如果只是单纯使用，可以选择第一个内建。

如果使用`Mysql`，需要下载 `JDBC Driver`，点击`Download JDBC drivcer`等待下载完成即可。

完成配置后点击`Proceed`，等待初始化完成，这里等待时间较长。

#### 配置用户

初始化完成后，会提示创建一个admin管理账号，创建完成后进入到server端的管理面板。

![image-20221109152935083](https://bed.piemoe.com/images/2022/11/09/202211091529355.png)

#### 服务端push客户端

之前提到，`docker`直接部署`agent`端消耗资源较大，并且不能更好的直接利用本机的开发环境，并且会产生很多莫名其妙的问题，所以这里还是通过
`server`端来自动化`push`一个`agent`端。

##### push客户端

点击`Agents`

![image-20221109153042157](https://bed.piemoe.com/images/2022/11/09/202211091530417.png)

选择`Agent Push`

![image-20221109153203340](https://bed.piemoe.com/images/2022/11/09/202211091532968.png)

点击`install agent`

![image-20221109153916306](https://bed.piemoe.com/images/2022/11/09/202211091539954.png)

`host`: agent端服务器的ip地址

`Preset`: 按上图

`Platform`: 按上图

`Install agent to`: 客户端安装位置，建议默认

`SSH port`: `agent`端`ssh`端口

`Authentication method`:  `ssh`连接方式，建议使用`Private-key`

`Username`: `agent`端服务器的用户，我这里直接使用了`root`，其他用户请确保拥有创建目录的权限

`Password`: `agent`端服务器的用户密码

最后2个 `Username`和`Password`可以不用填，除非以别的用户来构建项目。

最后点击`Install agent`即可自动推送一个`agent`端到客户端服务器上

##### 授权

`agent`端第一次链接到`server`端时，需要进行一次授权允许

![202211091546075](https://bed.piemoe.com/images/2022/11/12/202211121859179.png)

点击`Authorize...`授权，至此`agent`端配置完成。

### 项目构建

#### 创建项目

![image-20221109155426098](https://bed.piemoe.com/images/2022/11/09/202211091554620.png)

点击`+`号，创建项目

![image-20221109155503067](https://bed.piemoe.com/images/2022/11/09/202211091555059.png)

如果是`github`项目，可以点击图标，快速进行配置，这里使用码云来配置。

`Repository URL`: 项目`git`地址

`Username`:  `git`的登录名

`Password/ access token`: `git`的`密码`或`access token`，推荐使用`access token`

连接成功后进入详细配置

![image-20221109160058469](https://bed.piemoe.com/images/2022/11/09/202211091600948.png)

这里全部默认即可

#### git设置

点击`Version Control Settings`，可以对检出`git`进行设置

![image-20221109160408436](https://bed.piemoe.com/images/2022/11/09/202211091604800.png)

#### 构建步骤

点击`Build Steps` 进入构建步骤编辑

![image-20221109160513941](https://bed.piemoe.com/images/2022/11/09/202211091605104.png)

这里是我的构建步骤，以供参考，点击`Edit`可以进入编辑

点击`Add build step`新增构建步骤

![image-20221109160916126](https://bed.piemoe.com/images/2022/11/09/202211091609754.png)

选择程序进行构建，这里我选择`Command Line`，执行自定义脚本。

![image-20221109160825983](https://bed.piemoe.com/images/2022/11/09/202211091608010.png)

完成所有流水线构建后，就可以执行构建了

#### 构建

![image-20221109161150879](https://bed.piemoe.com/images/2022/11/09/202211091611454.png)

点击`run`进行自动化构建或者`提交git`内容进行自动化构建。

![image-20221109164412342](https://bed.piemoe.com/images/2022/11/09/202211091644876.png)

![image-20221109165134681](https://bed.piemoe.com/images/2022/11/09/202211091651829.png)

构建完成，看下网站

![image-20221109165228905](https://bed.piemoe.com/images/2022/11/09/202211091652232.png)
