Ubuntu 18.04 ä¸ã® GitLab CI 㧠Docker ã¤ã¡ã¼ã¸ããã«ãã㦠GCP ã® Container Registry ã«ç»é²ããã¾ã§
ç°å¢
ãããVPS ã« Ubuntu 18.04 ãOSã«ã¹ã¿ã ã¤ã³ã¹ãã¼ã«ãã¦ãã¦ãGitLab CE ãã¤ã³ã¹ãã¼ã«ãã¾ããGitLab CI Runner ãåãã¹ãã§åããã¾ãã CI 㧠Docker ã¤ã¡ã¼ã¸ããã«ããã¦ãGoogle Cloud Platform (GCP) ã® Container Registry ã«ããã·ã¥ãã¾ãã
GitLab ã®ã¤ã³ã¹ãã¼ã«
ä¸è¨ã®ãã¼ã¸ã®éãã§ããEEã¨CEã®å·®ã¯ã©ã¤ã»ã³ã¹ãé©ç¨ãããã©ããã§å¤ããã¾ããã絶対ã«CEã®ã¾ã¾ã¨ãããªãã¤ã³ã¹ãã¼ã«ã¹ã¯ãªããã®URLãå¤ããã¨ãã¡ãã§ã¤ã³ã¹ãã¼ã«ããã¾ãã
GitLab CI æ©è½ã®è¨å®
Docker CE ãã¤ã³ã¹ãã¼ã«
åã« apt ããã¤ã³ã¹ãã¼ã«ãã§ãã¾ããããã¼ã¸ã§ã³ãå¤ãã¨åµããããã®ã§ææ°ã® Docker CE ãã¤ã³ã¹ãã¼ã«ãã¾ãã
GitLab Runner ã®ã¤ã³ã¹ãã¼ã«
ãã¡ãã¯ãDockerã¸ã®ã¤ã³ã¹ãã¼ã«ã§ã¯ãªãããªãã¸ããªããã¤ã³ã¹ãã¼ã«ãã¾ãããGitLab Runner ã¯ã»ã¼ CI Runner ã®ã³ãã³ã管çãã¼ã«ã§ãå®éã¸ã§ãå¦çãã Runner (ãªãã Executor) ã¯æ¬¡ã§ã¤ã³ã¹ãã¼ã«ã¨ããèªèã§ãã
å®éã® GitLab Runner(Executor) ã¨ãã¦åä½ããDockerã³ã³ãããç»é²
ä¸è¨ã® Building Docker images with GitLab CI/CD ãã¼ã¸å ã® Use docker-in-docker executor* ãå®è¡ãã¾ãã https://docs.gitlab.com/ee/ci/docker/using_docker_build.html#use-docker-in-docker-executor
åãªãã¸ããªã® CI è¨å®
GCP ã«ã¹ãã¬ã¼ã¸ç®¡çè 権éãæã¤ãµã¼ãã¹ã¢ã«ã¦ã³ããä½æããJSONãã¼ãã¡ã¤ã«ãåå¾ãã¾ãã https://cloud.google.com/container-registry/docs/advanced-authentication#json_key_file
Settings -> CI / CD -> Variables
GCLOUD_PROJECT_ID
- GCP ã®ããã¸ã§ã¯ãIDGCLOUD_SERVICE_KEY
- JSONãã¼ãã¡ã¤ã«ã®ä¸èº«ãè²¼ãä»ãã
.gitlab-ci.yml
ä¸è¨ã®ããã«è¨ç½®ãããä¾ã§ã¯ gcr.io/<GCP project ID>/<repository group name>/<repository name>
ã§ã¤ã¡ã¼ã¸ã push ããã¾ãã
image: tilfin/gitlab-ci-to-gcr services: - docker:dind variables: DOCKER_DRIVER: overlay IMAGE_NAME: "$CI_PROJECT_PATH:latest" before_script: - echo $GCLOUD_SERVICE_KEY > ${HOME}/gcr-key.json - gcloud auth activate-service-account --key-file ${HOME}/gcr-key.json - docker login -u _json_key --password-stdin https://gcr.io < ${HOME}/gcr-key.json stages: - publish publish: stage: publish script: - docker build -t $IMAGE_NAME . - docker tag $IMAGE_NAME "gcr.io/$GCLOUD_PROJECT_ID/$IMAGE_NAME" - docker push "gcr.io/$GCLOUD_PROJECT_ID/$IMAGE_NAME" only: - master
dind
㯠Docker IN Docker ã®ç¥ã§ããoverlay
ã¹ãã¬ã¼ã¸ã»ãã©ã¤ã㯠Ubuntu 18.04 ã§ããã°å©ç¨ã§ãã¾ããDocker å ¥ãåã§ãã¹ãã¬ã¼ã¸ã¯ééçã«ãã¹ãã«ã¢ã¯ã»ã¹ãããã¨ã§ã¹ãã¼ããä¸ãããªãããã«ãã¾ãã
è£è¶³) tilfin/gitlab-ci-to-gcr
tilfin/gitlab-ci-to-gcr
ã¤ã¡ã¼ã¸ã¯ãç§ãå
¬å¼ Docker Hub ã«ããã·ã¥ãã¦ã docker:stable
ã« Google Cloud SDK 㨠gcloud ã³ãã³ããã¤ã³ã¹ãã¼ã«ãããã®ã§ãã
https://hub.docker.com/r/tilfin/gitlab-ci-to-gcr/
FROM docker:stable ARG CLOUD_SDK_VERSION=224.0.0 ENV CLOUD_SDK_VERSION=$CLOUD_SDK_VERSION ENV PATH /google-cloud-sdk/bin:$PATH RUN apk --no-cache add \ curl \ python \ py-crcmod \ bash \ libc6-compat \ openssh-client \ git \ gnupg \ && curl -O https://dl.google.com/dl/cloudsdk/channels/rapid/downloads/google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \ tar xzf google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \ rm google-cloud-sdk-${CLOUD_SDK_VERSION}-linux-x86_64.tar.gz && \ ln -s /lib /lib64 && \ gcloud components install kubectl && \ gcloud config set core/disable_usage_reporting true && \ gcloud config set component_manager/disable_update_check true && \ gcloud config set metrics/environment github_docker_image && \ gcloud --version VOLUME ["/root/.config"]
ç·æ¬
ãã¨ã㨠Registry æ©è½èªä½ã GitLab ã«ããã¹ãã¬ã¼ã¸ã ã GCP ãå©ç¨ãããã¨ãã§ãã¾ãããããããã«ããã¡ã¤ã³ã§ã®éç¨ã§ãã¾ããããªãå ´åããã£ãã®ã¨ãå¯ç¨æ§ã®é¢ã§ç´æ¥ GCP ã® Registry ãåç §ããæ¹ãããããããã®ããã«ãã¦ä½¿ã£ã¦ãã¾ãã