gitlab runner 实战

本篇文章需要实现:
(1)maven项目的打包
(2)docker镜像打包
(3)配置ssh免密登录,文件拷贝
demo项目地址
git地址 https://github.com/zwyqz/renren_fast.git
git remote remove origin
git remote add origin http://gitlab.example.com/gitlab-instance-a7f43c36/renren-fast_gitlab_runner.git
git push -uf origin master

(1)maven项目的打包
找到项目的CI/CD->编辑器->选择分支(master)->配置流水线->拷贝以及配置文件->提交更改->查看结果:
其中/root/.m2/repository 这个指定为固定目录,在创建docker的时候,目录地址进行了映射,以便jar包重复下载,加快编译速度。(指定是在config.toml中配置了,具体可以看,在注册的时候进行配置;(https://blog.csdn.net/w329636271/article/details/126368266)

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=/root/.m2/repository"
build-job:       # This job runs in the build stage, which runs first.
  stage: build
  image: maven:3.6.3-jdk-8-openj9
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."
    - pwd
    - mvn  clean package -DskipTests=true $MAVEN_OPTS package 
  only:
    # 只对master分支运行job
    - master
  artifacts:
    expire_in: 1 week
    paths:
      # 可以指定到某个文件(这里指定到了jar包),或者整个target/目录 可以用于下载
      - target/renren-fast.jar  

执行结果如下:

(2)docker镜像打包

Dockerfile

FROM java:8
EXPOSE 8090

VOLUME /tmp
ADD target/renren-fast.jar  /app.jar
RUN bash -c 'touch /app.jar'
ENTRYPOINT ["java","-jar","/app.jar"]

.gitlab-ci.yml文件

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=/root/.m2/repository"
  IMAGE_NAME: "appimages"
  CONTAINER_NAME: "app-container"
build-job:       # This job runs in the build stage, which runs first.
  stage: build
  image: maven:3.6.3-jdk-8-openj9
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."
    - pwd
    - mvn  clean package -DskipTests=true $MAVEN_OPTS package 
  only:
    # 只对master分支运行job
    - master
  artifacts:
    expire_in: 1 week
    paths:
      # 可以指定到某个文件(这里指定到了jar包),或者整个target/目录 可以用于下载
      - target/renren-fast.jar  
job_deploy:
    image: docker
    stage: deploy
    script:
      - docker build -t ${IMAGE_NAME} .
      - if [ $(docker ps -aq --filter name=${CONTAINER_NAME}) ]; then docker rm -f ${CONTAINER_NAME};fi
      - docker run -d -p 8080:80 --name ${CONTAINER_NAME} ${IMAGE_NAME}  
      # 删除为none的images    
      - docker rmi $(docker images | awk '/^/ { print $3 }')

(3)配置ssh免登陆
步骤1.配置服务器之间的ssh免登陆,参考(ssh免密登录免提示登录)
步骤2. base64加密id_rsa的内容

cat ~/.ssh/id_rsa |  base64 -w0

在这里插入图片描述
步骤3. 在gitlab中创建环境变量
设置->CI/CD->变量(展开)->添加变量gitlab runner 实战_第1张图片保护变量不勾选。gitlab runner 实战_第2张图片
步骤4.测试模块
修改job_copy中的对应的ip,
以下模块通过免登陆做了(1)输出aa到/tmp/tmp.txt (2)拷贝target的文件内容到/tmp/中

stages:          # List of stages for jobs, and their order of execution
  - build
  - test
  - deploy

variables:
  MAVEN_OPTS: "-Dmaven.repo.local=/root/.m2/repository"
  IMAGE_NAME: "appimages"
  CONTAINER_NAME: "app-container"
build-job:       # This job runs in the build stage, which runs first.
  stage: build
  image: maven:3.6.3-jdk-8-openj9
  script:
    - echo "Compiling the code..."
    - echo "Compile complete."
    - pwd
    - mvn  clean package -DskipTests=true $MAVEN_OPTS package 
  only:
    # 只对master分支运行job
    - master
  artifacts:
    expire_in: 1 week
    paths:
      # 可以指定到某个文件(这里指定到了jar包),或者整个target/目录 可以用于下载
      - target/renren-fast.jar  
job_deploy:
    image: docker
    stage: deploy
    script:
      - docker build -t ${IMAGE_NAME} .
      - if [ $(docker ps -aq --filter name=${CONTAINER_NAME}) ]; then docker rm -f ${CONTAINER_NAME};fi
      - docker run -d -p 8080:80 --name ${CONTAINER_NAME} ${IMAGE_NAME}  
      # 删除为none的images    
      - docker rmi $(docker images | awk '/^/ { print $3 }')
job_copy:
    image: docker
    stage: deploy
    before_script:
        - 'which ssh-agent || ( yum update -y && yum install openssh-client git -y ) '
        - eval $(ssh-agent -s)
        - echo $SSH_PRIVATE_KEY  |  base64 -d
        - echo $SSH_PRIVATE_KEY  |  base64 -d
        - echo $SSH_PRIVATE_KEY  |  base64 -d | ssh-add -
        # - cat id_rsa |  base64 -w0
        - mkdir -p ~/.ssh
        - chmod 700 ~/.ssh
        - ssh-keyscan  -p 28488  45.62.123.30 > ~/.ssh/known_hosts  //45.62.123.30替换为远程服务器的地址
        - chmod 644 ~/.ssh/known_hosts
    script:
       - ssh -p 28488 45.62.123.30  'echo aa >>  /tmp/tmp.txt'
       # - ssh -p 28488 45.62.123.30  mkdir /tmp/deploy1
       - scp -P 28488 -r target/* 45.62.123.30:/tmp/      

参考链接如下:
https://blog.csdn.net/weixin_47656385/article/details/123043020
https://gitlab.com/gitlab-examples
https://www.codenong.com/55223622/
kill -9 $(ps -ax | grep sftp | awk -F " " ‘{print $1 }’)

你可能感兴趣的:(gitlab,java,开发语言)