Jenkins环境搭建

作者:James Zhu ([email protected])

创建日期:2018-10-26

项目需求

借助Docker启动Jenkins服务非常方便,本例中需要在Jenkins中运行docker命令,还需要安装以下插件(插件当然可以制作完image后手动安装,这里为了表达将用到的插件,因此写在了Dockerfile中):

  • Pipeline
  • Git plugin
  • Build Timestamp Plugin

因此需要通过Dockerfile创建自己的image

操作步骤

  1. 创建Dockerfile

    FROM jenkins/jenkins:alpine
    
    ARG ALPINE_MIRROR=https://mirrors.aliyun.com
    ARG TIMEZONE=Asia/Shanghai
    
    ENV JAVA_OPTS="-Djenkins.install.runSetupWizard=false"
    
    USER root
    
    # Once jenkins is running and configured, run the following command to find the list of plugins installed:
    # curl -sSL "http://$JENKINS_HOST/pluginManager/api/json?depth=1&wrapper=plugins" | jq -r '.plugins[].shortName' | tee plugins.txt
    RUN set -e; \
        if [ -n "$ALPINE_MIRROR" ]; then \
            sed -i 's!http://dl-cdn.alpinelinux.org!'"$ALPINE_MIRROR"'!g' /etc/apk/repositories; \
        fi; \
        apk add --no-cache docker tzdata; \
        if [ -n "$TIMEZONE" ]; then \
            cp "/usr/share/zoneinfo/$TIMEZONE" /etc/localtime; \
            echo "$TIMEZONE" > /etc/timezone; \
        fi; \
        apk del tzdata; \
        install-plugins.sh workflow-aggregator git build-timestamp
    
    USER jenkins
    
  2. 创建image

    docker build -t localhost/jenkins .
    
  3. 启动Jenkins服务

    docker volume create jenkins_data
    docker run --rm -d \
        --name jenkins \
        -p 8080:8080 \
        -p 50000:50000 \
        -v jenkins_data:/var/jenkins_home \
        # Docker Toolbox
        -v $HOME/.docker/machine/machines/default:/usr/share/ca-certificates/docker \
        -e DOCKER_TLS_VERIFY \
        -e DOCKER_HOST \
        -e DOCKER_CERT_PATH=/usr/share/ca-certificates/docker \
        # Docker for Mac
        -v /var/run/docker.sock:/var/run/docker.sock \
        --group-add=$(stat -f %g /var/run/docker.sock) \
        # Docker for Linux
        -v /var/run/docker.sock:/var/run/docker.sock \
        --group-add=$(stat -c %g /var/run/docker.sock) \
        localhost/jenkins
    

    The image needs to run Docker commands, so it assumes that your Docker daemon is listening to /var/run/docker.sock (discussion). This is not Docker-in-Docker; the container only runs the CLI and connects back to the host to start sister containers.

  4. 现在打开浏览器访问http://<docker-machine-ip>/,就可以使用Jenkins了。

    因为设置了JAVA_OPTS="-Djenkins.install.runSetupWizard=false"环境变量,因此不会出现安装向导,无需登录直接使用。

  5. Jenkins设置

    • 系统管理 => 系统设置 => Build Timestamp

      这个用于创建image时传入BUILD_DATE

      Timezone: GMT

      Pattern: yyyy-MM-dd'T'HH:mm:ss'Z'

    • 凭据

      根据实际情况,添加Git服务器登录凭据

  6. 至此,Jenkins已经安装配置完成。接下来就可以创建任务了。

results matching ""

    No results matching ""