Gitlab CI/CD Runner

Install Gitlab Runner from docker


https://docs.gitlab.com/runner/install/
https://docs.gitlab.com/runner/install/docker.html

docker pull gitlab/gitlab-runner:latest

docker run --rm -t -i gitlab/gitlab-runner --help


 docker run -d --name gitlab-runner --restart always \
   -v /srv/gitlab-runner/config:/etc/gitlab-runner \
   -v /var/run/docker.sock:/var/run/docker.sock \
   gitlab/gitlab-runner:latest



Docker register runner

 

docker run --rm -t -i -v /srv/gitlab-runner/config:/etc/gitlab-runner gitlab/gitlab-runner register
Runtime platform                                    arch=amd64 os=linux pid=6 revision=6946bae7 version=12.0.0
Running in system-mode.                            
                                                   
Please enter the gitlab-ci coordinator URL (e.g. https://gitlab.com/):
https://gitlab.hydrasrv.com/

Please enter the gitlab-ci token for this runner:
xxx-xxx-xxxx

Please enter the gitlab-ci description for this runner:
[1ef5c4135b38]: docker runner for k8s deploy

Please enter the gitlab-ci tags for this runner (comma separated):
Docker Runner K8s Deploy

Registering runner... succeeded                     runner=79epxeRm

Please enter the executor: docker, docker-ssh, parallels, shell, kubernetes, ssh, virtualbox, docker+machine, docker-ssh+machine:
docker

Please enter the default Docker image (e.g. ruby:2.6):
gitlab/gitlab-runner:latest

Runner registered successfully. Feel free to start it, but if it's running already the config should be automatically reloaded! 

 


gitlab-ci.yml

stages:
  - build_image
  - push_image_tag_job
  - push_image
  - update_k8s_image

variables:
  DOCKER_DRIVER: overlay2
  GIT_SUBMODULE_STRATEGY: recursive
  IMAGE_NAME: $REGISTRY_ADDRESS/$REGISTRY_NAMESPACE/$CI_PROJECT_NAME
  

build_image_job:
  stage: build_image
  tags:
    - build-images
  script:
    - CI_COMMIT_SHORT=`echo $CI_COMMIT_SHA|head -c 8`
    - ls -AF|grep '^\.' > .dockerignore
    - docker build -t $IMAGE_NAME:$CI_COMMIT_SHORT .
  only:
    - staging
    - tags

push_image_tag_job:
  stage: push_image_tag_job
  tags:
    - build-images
  script:
    - CI_COMMIT_SHORT=`echo $CI_COMMIT_SHA|head -c 8`
    - docker tag  $IMAGE_NAME:$CI_COMMIT_SHORT $IMAGE_NAME:$CI_BUILD_REF_NAME
    - docker login -u "$REGISTRY_USERNAME" -p "$REGISTRY_PASSWORD" $REGISTRY_ADDRESS
    - docker push $IMAGE_NAME:$CI_BUILD_REF_NAME
    - docker rmi $IMAGE_NAME:$CI_BUILD_REF_NAME
  only:
    - tags

push_image_job:
  stage: push_image
  tags:
    - build-images
  script:
    - CI_COMMIT_SHORT=`echo $CI_COMMIT_SHA|head -c 8`
    - docker login -u "$REGISTRY_USERNAME" -p "$REGISTRY_PASSWORD" $REGISTRY_ADDRESS
    - docker push $IMAGE_NAME:$CI_COMMIT_SHORT
    - docker rmi $IMAGE_NAME:$CI_COMMIT_SHORT
  only:
    - staging

update_k8s_image_job:
  stage: update_k8s_image
  tags:
    - kubeapi-runner
  script:
    - CI_COMMIT_SHORT=`echo $CI_COMMIT_SHA|head -c 8`
    - kubectl -n bigtwo set image statefulset -l app=webonline webonline=$IMAGE_NAME:$CI_COMMIT_SHORT
  only:
    - staging

你可能感兴趣的