简体   繁体   English

使用 docker 创建 CI 服务器代理

[英]Using docker to create CI server agents

I'm trying to set up a local GoCD CI server using docker for both the base server and agents.我正在尝试使用 docker 为基本服务器和代理设置本地 GoCD CI 服务器。 I can get everything running fine, but issues spring up when I try make sure the agent containers have everything installed in them that I need to build my projects.我可以让一切正常运行,但是当我尝试确保代理容器中安装了我构建项目所需的所有东西时,问题就会出现。

I want to preface this with I'm aware that I might not be using these technologies correctly, but I don't know much better atm.我想以此作为序言,我知道我可能没有正确使用这些技术,但我不知道更好的 atm。 If there are better ways of doing things, I'd love to learn.如果有更好的做事方法,我很乐意学习。

To start, I'm using the official GoCD docker image and that works just fine.首先,我使用的是官方的 GoCD docker 镜像,效果很好。 Creating a blank agent also works just fine.创建一个空白代理也可以正常工作。 However, one of my projects requires node, yarn and webpack to be build (good ol' react site).但是,我的一个项目需要构建节点、纱线和 webpack(好的 ol' react 站点)。 Of course a standard agent container has nothing but the agent installed on it so I've had a shot using a Dockerfile to install all the tech I need to build my projects.当然,标准代理容器除了安装在其上的代理之外什么都没有,所以我尝试使用 Dockerfile 来安装构建项目所需的所有技术。

FROM gocd/gocd-agent-ubuntu-18.04:v19.11.0

SHELL ["/bin/bash", "-c"] 
USER root

RUN apt-get update
RUN apt-get install -y git curl wget build-essential ca-certificates libssl-dev htop openjdk-8-jre python python-pip

RUN curl -sS https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - && \
    echo "deb https://dl.yarnpkg.com/debian/ stable main" | tee /etc/apt/sources.list.d/yarn.list
RUN apt-get update && apt-get install -y yarn

# This user is created in the base agent image
USER go

ENV NVM_DIR /home/go/.nvm
ENV NODE_VERSION 10.17.0

RUN curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.1/install.sh | bash \
    && . $NVM_DIR/nvm.sh \
    && nvm install $NODE_VERSION \
    && nvm alias default $NODE_VERSION \
    && nvm use default \
    && npm install -g webpack webpack-cli

ENV NODE_PATH $NVM_DIR/v$NODE_VERSION/lib/node_modules
ENV PATH      $NVM_DIR/v$NODE_VERSION/bin:$PATH

This is the current version of this file, but I've been through many many iterations of frustrations where an globally installed npm package is never on the path and thus not conveniently available.这是该文件的当前版本,但我已经经历了很多次挫折,其中全局安装的 npm 包永远不会出现在路径上,因此不方便使用。

The docker build works fine, its just that in this iteration of the Dockerfile, webpack is not found when the agent tries running a build. docker build 工作正常,只是在 Dockerfile 的这个迭代中,当代理尝试运行构建时没有找到 webpack。

My question is:我的问题是:

  1. Is a Dockerfile the right place to do things like install yarn, node, webpack etc... ? Dockerfile 是安装纱线、节点、webpack 等的正确位置吗?

  2. If so, how can I ensure everything I install through npm is actually available?如果是这样,我如何确保通过 npm 安装的所有内容实际上都可用?

  3. If not, what are the current best practices about this?如果不是,那么当前的最佳做法是什么?

Any help, thoughts and anecdotes are fully welcomed and appreciated!任何帮助、想法和轶事都受到欢迎和赞赏!

Cheers~!干杯~!

You should separate gocd-server and gocd-agent to various containers.您应该将 gocd-server 和 gocd-agent 分开到各种容器中。

Pull images:拉取镜像:

  • docker pull gocd/gocd-server:v18.10.0 docker pull docker pull gocd/gocd-server:v18.10.0 docker pull
  • gocd/gocd-agent-alpine-3.8:v18.10.0 gocd/gocd-agent-alpine-3.8:v18.10.0

Build and run them, check if it's ok.构建并运行它们,检查是否正常。 Then connect into bash in agent container然后连接到代理容器中的 bash

docker exec -it gocd-agent bash docker exec -it gocd-agent bash

Install the binaries using the alpine package manager.使用 alpine 包管理器安装二进制文件。

apk add --no-cache nodejs yarn apk 添加 --no-cache nodejs yarn

Then logout and update the container image.然后注销并更新容器映像。 Now you have an image with needed packeges.现在你有一个包含所需包的图像。 Also read this article .另请阅读这篇文章

You have two options with gocd agents. gocd 代理有两种选择。

The first one is the agent use docker, and create other containers, for any purpose that the pipeline needs.第一个是代理使用 docker,并创建其他容器,用于管道需要的任何目的。 So you can have a lot of agents with this option, and the rules or definitions occurs in the pipeline.因此,您可以有很多具有此选项的代理,并且规则或定义出现在管道中。 The agent only execute.代理只执行。

The second one, is an agent with al kind of program installed you needed.第二个是安装了您需要的所有程序的代理。 I use this one.我用这个。 For this case, you use a Dockerfile with all, and generate the image for all the agents.对于这种情况,您将 Dockerfile 与 all 一起使用,并为所有代理生成映像。

For example i have an agent with gcloud, kubectl, sonar scanner and jmeter, who test with sonar before the deploy, then deploy in gcp, and for last step, it test with jmeter after the deploy.例如,我有一个带有 gcloud、kubectl、声纳扫描仪和 jmeter 的代理,他们在部署前用声纳进行测试,然后在 gcp 中部署,最后一步,在部署后用 jmeter 进行测试。

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

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