Gitlab自动化构建docker镜像 gitlab-ci.yml gitlab-runner

High level workflow

gitlab-ci high level workflow diagram.png

Dockerfile

创建你项目的dockerfile,安装所有的依赖项。

Contaniner_Registry

在你的gitlab中打开package - container_registry,这将用于存储你应用的镜像。

.gitlab-ci.yml

gitlab-ci.yml定义了你在持续集成过程中要去完成的事,在下面的例子中主要是完成docker镜像的构建

before_script:
  - docker info

stages:
  - build

client_job:
  stage: build
  script:
    - cd client
    - docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
    - docker build --build-arg git_username=$CI_DEPLOY_USER --build-arg git_access_token=$CI_DEPLOY_PASSWORD -t $CI_REGISTRY/gitproject/project/app-$CI_COMMIT_REF_NAME:$CI_COMMIT_SHA .
    - docker tag $CI_REGISTRY/gitproject/project/app-$CI_COMMIT_REF_NAME:$CI_COMMIT_SHA $CI_REGISTRY/gitproject/project/app-$CI_COMMIT_REF_NAME:latest
    - docker push $CI_REGISTRYgitproject/project/app-$CI_COMMIT_REF_NAME
  only:
    - develop
  tags:
    - odsplants.tag

server_job:
  stage: build
  script:
    - cd server
    - docker login -u $CI_DEPLOY_USER -p $CI_DEPLOY_PASSWORD $CI_REGISTRY
    - docker build --build-arg git_username=$CI_DEPLOY_USER --build-arg git_access_token=$CI_DEPLOY_PASSWORD -t $CI_REGISTRY/gitproject/project/app-$CI_COMMIT_REF_NAME:$CI_COMMIT_SHA .
    - docker tag $CI_REGISTRY/gitproject/project/appr-$CI_COMMIT_REF_NAME:$CI_COMMIT_SHA $CI_REGISTRY/devops-offshore/gitproject/project/app-$CI_COMMIT_REF_NAME:latest
    - docker push $CI_REGISTRY/gitproject/project/app-$CI_COMMIT_REF_NAME
  only:
    - develop
  tags:
    - odsplants.tag

Variables

  • $CI_REGISTRY - The URL of the docker registry. This is stored in the client project's group CI Variables.
  • $CI_DEPLOY_USER - A special Gitlab user passed into the CI runner to allow access back to the client repository. This is needed to checkout the code into the Docker image to be able to be run.
  • $CI_DEPLOY_PASSWORD - The password for the special Gitlab user.
  • $CI_COMMIT_SHA - Gitlab's CI runner has the Git commit hash which triggered this CI build, which is used in the Docker image to checkout that commit.
  • $CI_COMMIT_REF_NAME - The branch which triggered the Gitlab's CI, available to the runner, in this case, to help name the docker image.

Gitlab-Runner

您需要在具备docker命名的环境中安装gitlab-runner,当你提交代码出发gitlab ci/cd的pipeline时,会使用该gitlab-runner中的docker构建镜像,并推送镜像到container_registry中。
安装gitlab-runner并将2其注册到你的gitlab中:

wget -O /usr/local/bin/gitlab-runner https://gitlab-ci-multi-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-ci-multi-runner-linux-amd64
sudo chmod +x /usr/local/bin/gitlab-runner
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash
add /usr/local/bin into secure_path in /etc/sudoers # enable the sudo permission for gitlab-runner # $ Defaults    secure_path = /sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin
image.png
# Download the binary for your system
sudo curl -L --output /usr/local/bin/gitlab-runner https://gitlab-runner-downloads.s3.amazonaws.com/latest/binaries/gitlab-runner-linux-amd64

# Give it permissions to execute
sudo chmod +x /usr/local/bin/gitlab-runner

# Create a GitLab CI user
sudo useradd --comment 'GitLab Runner' --create-home gitlab-runner --shell /bin/bash

# Install and run as service
sudo gitlab-runner install --user=gitlab-runner --working-directory=/home/gitlab-runner
sudo gitlab-runner start

sudo gitlab-runner register --url http://gitlab.example.com/ --registration-token $REGISTRATION_TOKEN

Install Docker

  • 在你的安装gitlab-runner的服务器中安装docker。

你可能感兴趣的:(Gitlab自动化构建docker镜像 gitlab-ci.yml gitlab-runner)