In order to accomplish this we need to have up and running the following:
- kubernetes cluster, docker swarm or single node docker host
- gitlab installed
- gitlab runners deployed to a host (container or VM)
- a valid Dockerfile
- gitlab ci/cd configuration yml
- kubernetes deployment file
Before starting we need to enable Container Registry. Easiest way to do this is by configuring gitlab to do this for us.
Configuration change should be applied to the file /etc/gitlab/gitlab.rb
. Add the following lines to the bottom of file:
registry_external_url 'https://gitlab.0x01.link:5001'
gitlab_rails['registry_enabled'] = true
gitlab_rails['registry_host'] = "gitlab.0x01.link"
gitlab_rails['registry_port'] = "5005"
gitlab_rails['registry_path'] = "/var/opt/gitlab/gitlab-rails/shared/registry"
In order to apply our new configuration change we need to run command gitlab-ctl reconfigure
Right now we are ready to create our Dockerfile required for CI/CD. Paste the following example in a file named Dockerfile in the root of your project:
FROM php:7.4-apache
RUN apt-get update
RUN apt-get install -y \
libfreetype6-dev \
libjpeg62-turbo-dev \
libmcrypt-dev \
libpng-dev \
zlib1g-dev \
libxml2-dev \
libzip-dev \
libonig-dev \
graphviz \
&& docker-php-ext-configure gd \
&& docker-php-ext-install -j$(nproc) gd \
&& docker-php-ext-install pdo_mysql \
&& docker-php-ext-install mysqli \
&& docker-php-ext-install zip \
&& docker-php-source delete
RUN apt-get clean
RUN a2enmod rewrite
RUN curl --silent --show-error https://getcomposer.org/installer | php -- --install-dir=/usr/local/bin --filename=composer
# RUN composer global require "phpoffice/phpspreadsheet"
RUN mkdir -p /myproject/
COPY CI/ /myproject/CI/
COPY https/ /myproject/https/
COPY docker/entrypoint.sh /
ENV APACHE_DOCUMENT_ROOT=/var/www/html/https
EXPOSE 80
CMD ["bash", "/entrypoint.sh"]
In order to create our Gitlab build stage is to create our .gitlab-ci.yml. Paste the following example in your yml:
variables:
CI_REGISTRY: gitlab.0x01.link:5001
CONTAINER_IMAGE: $CI_REGISTRY/0x01/myproject:v${CI_PIPELINE_ID}
CONTAINER_IMAGE_LATEST: $CI_REGISTRY/0x01/myproject:latest
stages:
- build
- deploy
build:
tags:
- DC1
stage: build
image:
name: gcr.io/kaniko-project/executor:debug
entrypoint: [""]
script:
- mkdir -p /kaniko/.docker
- echo "{\"auths\":{\"$CI_REGISTRY\":{\"username\":\"$CI_USERNAME\",\"password\":\"$CI_PASSWORD\"}}}" > /kaniko/.docker/config.json
- /kaniko/executor --context ${CI_PROJECT_DIR}/${KANIKO_CONTEXT:-.} --dockerfile Dockerfile --destination $CONTAINER_IMAGE --destination $CONTAINER_IMAGE_LATEST
only:
- master
While we are near by ready in the latest step we need to create a deployment file for kubernetes cluster. You can use the following deployment.yml as an valid example for your implementation:
apiVersion: apps/v1
kind: Deployment
metadata:
name: myproject
namespace: myproject
labels:
app: myproject
spec:
replicas: 3
selector:
matchLabels:
app: myproject
strategy:
type: RollingUpdate
rollingUpdate:
maxSurge: 1
maxUnavailable: 33%
template:
metadata:
labels:
app: myproject
spec:
containers:
- name: myproject
image: gitlab.0x01.link:5001/0x01/myproject:latest
ports:
- containerPort: 80
imagePullSecrets:
- name: registry-secret
In the final step we need to append the following block of code to our .gitlab-ci.yml.
deploy:
tags:
- DC1
stage: deploy
image:
name: lwolf/helm-kubectl-docker:latest
before_script:
- mkdir -p ~/.kube
- echo ${kube_config} | base64 -d > ~/.kube/config
- sed -i -e "s/:latest/:v${CI_PIPELINE_ID}/g" docker/deployment.yaml
- echo "v${CI_PIPELINE_ID}"
script:
- kubectl apply -f deployment.yaml
With this final step we have our build and deploy stages ready.
0 Comments