curl -fsSL https://get.docker.com | bash -s docker --mirror Aliyun
systemctl start docker systemctl enable docker
docker run hello-world
sudo curl -L "https://github.com/docker/compose/releases/download/1.27.4/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose
chmod +x /usr/local/bin/docker-compose
docker-compose --version
总体目录结构以下html
app └── data ├── deploy │ └── jenkins │ ├── config │ ├── docker-composer.yml │ └── Dockerfile └── jenkins ├── certs └── data
# 存放docker-compose相关配置文件 mkdir -p /app/data/deploy/jenkins # 存放Jenkins证书相关文件 mkdir -p /app/data/jenkins/certs # 存放Jenkins数据文件 mkdir -p /app/data/jenkins/data # 修改权限 cd /app/data chmod 775 -R jenkins/ chown -R 1000:1000 jenkins/
cd /app/data/deploy/jenkins/
version: "2" services: jenkins-blueocean: build: context: . container_name: jenkins-blueocean network_mode: bridge ports: - 8088:8080 # - 50000:50000 environment: # - DOCKER_HOST=tcp://docker:2376 # - DOCKER_CERT_PATH=/certs/client # - DOCKER_TLS_VERIFY=1 - TZ=Asia/Shanghai - JENKINS_UC="https://mirrors.tuna.tsinghua.edu.cn/jenkins/" - JENKINS_UC_DOWNLOAD="https://mirrors.tuna.tsinghua.edu.cn/jenkins/" - JENKINS_OPTS="-Dhudson.model.UpdateCenter.updateCenterUrl=https://mirrors.tuna.tsinghua.edu.cn/jenkins/updates/update-center.json" volumes: - /app/data/jenkins/certs:/certs/client:ro - /app/data/jenkins/data:/var/jenkins_home depends_on: - jenkins-docker links: - jenkins-docker:docker jenkins-docker: image: docker:19.03.14-dind container_name: jenkins-docker privileged: true network_mode: bridge command: --config-file /etc/docker/daemon.json ports: - 2376:2376 environment: - DOCKER_TLS_CERTDIR=/certs volumes: - ./config/daemon.json:/etc/docker/daemon.json:ro - /app/data/jenkins/certs:/certs/client - /app/data/jenkins/data:/var/jenkins_home
FROM jenkins/jenkins:2.263.1-lts-slim USER root COPY config/sources.list /etc/apt/sources.list RUN apt-get update && apt-get install -y apt-transport-https \ ca-certificates curl gnupg2 \ software-properties-common \ vim \ rsync \ tree \ default-mysql-client RUN curl -fsSL https://download.docker.com/linux/debian/gpg | apt-key add - RUN apt-key fingerprint 0EBFCD88 RUN add-apt-repository \ "deb [arch=amd64] https://download.docker.com/linux/debian \ $(lsb_release -cs) stable" RUN apt-get update && apt-get install -y docker-ce-cli USER jenkins # RUN jenkins-plugin-cli --plugins blueocean:1.24.3
{ "registry-mirrors": [ # 阿里云镜像加速地址 "https://xxxx.mirror.aliyuncs.com" ], "log-opts": { "max-size": "100m", "max-file": "3" }, "live-restore": true }
deb http://mirrors.aliyun.com/debian/ buster main non-free contrib deb-src http://mirrors.aliyun.com/debian/ buster main non-free contrib deb http://mirrors.aliyun.com/debian-security buster/updates main deb-src http://mirrors.aliyun.com/debian-security buster/updates main deb http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib deb-src http://mirrors.aliyun.com/debian/ buster-updates main non-free contrib deb http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib deb-src http://mirrors.aliyun.com/debian/ buster-backports main non-free contrib
#构建镜像 docker-compose -f docker-composer.yml build #启动服务 docker-compose -f docker-composer.yml up -d #查看日志获取首次登陆密码 docker logs -f jenkins-blueocean
Jenkins彻底启动时会打印一条Jenkins is fully up and running日志,看到此日志就能够开始配置Jenkins了。
node
输入刚才在日志中获得的密码来激活Jenkinsmysql
等待插件自动安装完成,若因网络问题安装失败则重试便可linux
邮箱能够随便填写
git
URL会被用来作Webhook等用途等地址
github
Jenkins自动化部署依赖一系列插件的协做,因此咱们要提早安装好。sql
Manage Jenkins
->Manage Plugins
->高级
docker
#开源镜像 https://mirrors.huaweicloud.com/jenkins/updates/update-center.json #开源地址 https://jenkins-zh.cn/tutorial/management/plugin/update-center/
可选插件
下搜索如下插件并安装apache
Blue Ocean (可选,若是想用Blue Ocean视图的话) SSH Pipeline Steps Pipeline Utility Steps NodeJS ansiColor
插件安装完成的时候重启Jenkinsjson
# ssh进入Jenkins服务器 # 进入Jenkins容器 docker exec -it jenkins-blueocean bash # 进入用户主目录,用户主目录中的内容被挂载到数据盘上,能够把安装程序放心放入其中 cd ~ mkdir -p tools && cd tools # 下载安装包 curl https://mirrors.tuna.tsinghua.edu.cn/apache/maven/maven-3/3.6.3/binaries/apache-maven-3.6.3-bin.zip -o apache-maven-3.6.3-bin.zip # 解压 unzip apache-maven-3.6.3-bin.zip # 删除安装包 rm apache-maven-3.6.3-bin.zip # 配置maven settings.xml cd ~ mkdir -p .m2 # 将项目中maven配置文件settings.xml文件内容写入.m2/settings.xml文件中
在Jenkins Server Web界面上系统管理
->全局工具配置
->Maven
->新增maven安装
自动安装复选框
/var/jenkins_home/tools/apache-maven-3.6.3
保存
。上述安装Jenkins插件过程当中已经安装了NodeJS插件,由于NodeJS有不少版本,因此咱们须要手动配置NodeJS安装,这样才能够在流水线中使用它。
在Jenkins Server Web界面上系统管理
->全局工具配置
->NodeJS
->新增NodeJS安装
NODEJS10
(名称很重要,咱们的流水线配置中须要使用这个名字)NodeJS10.23.0
(能够根据须要选择)保存
。Jenkins提供凭据管理的功能来帮助咱们存放涉密帐号密码、私钥等信息,在配置Pipeline前咱们须要建立好相关凭据。
# ssh进入Jenkins服务器 # 进入Jenkins容器 docker exec -it jenkins-blueocean bash # 进入主目录 cd ~ # 生成RSA密钥对 ssh-keygen -m PEM -t rsa
在Jenkins界面上系统管理->
管理凭据`中建立如下全局ssh密钥对凭据,填写好ID、描述、用户名和私钥信息
为了Jenkins能正常执行部署任务,各个应用服务器运维帐号要信任上述生成的密钥对。
可参考该文章[https://www.cnblogs.com/wangyang0210/archive/2004/01/13/9722922.html]
Jenkins Pipeline支持共享类库来减小冗余代码,咱们经过这种方法来复用代码而且管理部署相关配置。
系统管理
->系统配置
->Global Pipeline Libraries
中点击新增来建立共享类库,其基本信息以下:
系统管理
->全局安全配置
->项目矩阵受权策略
根据本身需求进行受权就行了
这里只是作一个记录,上方的步骤均已经是修改成正确的步骤。
ERROR: Can't find a suitable configuration file in this directory or any parent. Are you in the right directory? Supported filenames: docker-compose.yml, docker-compose.yaml # 解决错误 1. docker服务没有启动 2. 使用-f指定配置文件
[root@localhost jenkins]# docker logs -f jenkins-blueocean touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions? touch: cannot touch '/var/jenkins_home/copy_reference_file.log': Permission denied Can not write to /var/jenkins_home/copy_reference_file.log. Wrong volume permissions? # 解决错误 1. 目录权限错误(chown -R 1000:1000 <本地映射目录>)