复制用户目录下的 .ssh 文件夹下的公钥,将其复制到 GitLab 的
创建工作目录 /usr/lcoal/docker/runner
创建构建目录 /usr/local/docker/runner/enviroment
下载 jdk-8u152-linux-x64.tar.gz 并复制到 /usr/local/docker/runner/environment
Dockerfile在 usr/lcoal/docker/runner/environment 目录下创建 Dockerfile
FROM gitlab/gitlab-runner:v11.0.2 RUN echo 'deb xenial main restricted universe multiverse' > /etc/apt/sources.list && \ echo 'deb xenial-security main restricted universe multiverse' >> /etc/apt/sources.list && \ echo 'deb xenial-updates main restricted universe multiverse' >> /etc/apt/sources.list && \ echo 'deb xenial-backports main restricted universe multiverse' >> /etc/apt/sources.list && \ apt-get update -y && \ apt-get clean RUN apt-get -y install apt-transport-https ca-certificates curl software-properties-common && \ curl -fsSL | apt-key add - && \ add-apt-repository "deb [arch=amd64] $(lsb_release -cs) stable" && \ apt-get update -y && \ apt-get install -y docker-ce COPY daemon.json /etc/docker/daemon.json WORKDIR /usr/local/bin RUN wget https://raw.githubusercontent.com/topsale/resources/master/docker/docker-compose RUN chmod +x docker-compose RUN mkdir -p /usr/local/java WORKDIR /usr/local/java COPY jdk-8u152-linux-x64.tar.gz /usr/local/java RUN tar -zxvf jdk-8u152-linux-x64.tar.gz && \ rm -fr jdk-8u152-linux-x64.tar.gz RUN mkdir -p /usr/local/maven WORKDIR /usr/local/maven RUN wget https://raw.githubusercontent.com/topsale/resources/master/maven/apache-maven-3.5.3-bin.tar.gz RUN tar -zxvf apache-maven-3.5.3-bin.tar.gz && \ rm -fr apache-maven-3.5.3-bin.tar.gz ENV JAVA_HOME /usr/local/java/jdk1.8.0_152 ENV MAVEN_HOME /usr/local/maven/apache-maven-3.5.3 ENV PATH $PATH:$JAVA_HOME/bin:$MAVEN_HOME/bin WORKDIR / daemon.json在 /usr/local/docker/runner/environment 目录下创建 daemon.json,用于配置加速器和仓库地址
{ "registry-mirrors": [ "https://registry.docker-cn.com" ], "insecure-registries": [ "ip:port" ] } 新建 Git 项目新建一个 Git 项目,将其克隆到本地
注册 Runner docker exec -it gitlab-runner gitlab-runner register # 输入 GitLab 地址 Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/): :8080/ # 输入 GitLab Token Please enter the gitlab-ci token for this runner: 1Lxq_f1NRfCfeNbE5WRh # 输入 Runner 的说明 Please enter the gitlab-ci description for this runner: 可以为空 # 设置 Tag,可以用于指定在构建规定的 tag 时触发 ci Please enter the gitlab-ci tags for this runner (comma separated): 可以为空 # 选择 runner 执行器,这里我们选择的是 shell Please enter the executor: virtualbox, docker+machine, parallels, shell, ssh, docker-ssh+machine, kubernetes, docker, docker-ssh: shell以上交互中,GitLab 地址和 Token 令牌可以在 GitLab 的项目的设置中找到:
当上述步骤完成后,刷新当前页面,可以看见当前页面下多出来一个 runner:
完善项目在这里我只简单地做了个小 dome,大概这么简单:
项目中用到的 .gitlab-ci.yml、Dockerfile、docker-compose.yml 如下:
.gitlab-ci.yml
Dockerfile
FROM openjdk:8-jre RUN mkdir /app COPY ci-test-project-1.0.0-SNAPSHOT.jar /app/app.jar ENTRYPOINT ["java", "-Djava.security.egd=file:/dev/./urandom", "-jar", "/app/app.jar"] EXPOSE 8080docker-compose.yml
version: '3.1' services: itoken-config: restart: always image: ci-test-project ports: - 8080:8080 提交项目完成 demo 的之后就将项目提交到 GitLab 上,push 完成之后,点击项目的 CI/CD 可以看见一下页面
再点击进入就可以看见具体的作业了,自行体会
当项目出现所有 jobs 全部通过时,也就是持续集成初步完善了,这时我们可以打开浏览器访问 ip:8080 可以看见浏览器上显示
Hello GitLab