From aeb5a75cf111a77018bb21157c4886d8cc29157c Mon Sep 17 00:00:00 2001 From: Philippe Caseiro Date: Mon, 19 Jun 2023 13:58:25 +0200 Subject: [PATCH] feat(jenkins): adding new default jenkins configuration --- Jenkinsfile | 25 +++++++++++++++++ Makefile | 14 ++++++---- init-app | 16 +++++------ main.mk | 13 +++++---- .../Dockerfile | 0 .../Dockerfile | 0 .../configurations/cnpg-cluster.yaml | 0 .../kustomization.yaml | 12 ++++---- .../myapplication-cnpg-cluster.yaml} | 10 +++---- .../k8s/kustomization/base/kustomization.yaml | 6 ++-- .../MYAPPLICATION-kube/kustomization.yaml | 6 ---- .../myapplication-kube/kustomization.yaml | 6 ++++ .../resources/myapplication-deployment.yaml} | 28 +++++++++---------- .../resources/myapplication-service.yaml} | 8 +++--- .../overlays/dev/kustomization.yaml | 4 +-- .../dev/patches/add-registry-pull-secret.yaml | 4 --- ...aml => myapplication-update-replicas.yaml} | 4 +-- .../overlays/dev/resources/ingress.yaml | 6 ++-- .../overlays/dev/resources/namespace.yaml | 2 +- skaffold.yaml | 4 +-- 20 files changed, 97 insertions(+), 71 deletions(-) create mode 100644 Jenkinsfile rename misc/images/{MYAPPLICATION-kube => myapplication-kube}/Dockerfile (100%) rename misc/images/{MYAPPLICATION-standalone => myapplication-standalone}/Dockerfile (100%) rename misc/k8s/kustomization/base/components/{MYAPPLICATION-cnpg => myapplication-cnpg}/configurations/cnpg-cluster.yaml (100%) rename misc/k8s/kustomization/base/components/{MYAPPLICATION-cnpg => myapplication-cnpg}/kustomization.yaml (66%) rename misc/k8s/kustomization/base/components/{MYAPPLICATION-cnpg/resources/MYAPPLICATION-cnpg-cluster.yaml => myapplication-cnpg/resources/myapplication-cnpg-cluster.yaml} (54%) delete mode 100644 misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/kustomization.yaml create mode 100644 misc/k8s/kustomization/base/resources/myapplication-kube/kustomization.yaml rename misc/k8s/kustomization/base/resources/{MYAPPLICATION-kube/resources/MYAPPLICATION-deployment.yaml => myapplication-kube/resources/myapplication-deployment.yaml} (58%) rename misc/k8s/kustomization/base/resources/{MYAPPLICATION-kube/resources/MYAPPLICATION-service.yaml => myapplication-kube/resources/myapplication-service.yaml} (50%) delete mode 100644 misc/k8s/kustomization/overlays/dev/patches/add-registry-pull-secret.yaml rename misc/k8s/kustomization/overlays/dev/patches/{MYAPPLICATION-update-replicas.yaml => myapplication-update-replicas.yaml} (56%) diff --git a/Jenkinsfile b/Jenkinsfile new file mode 100644 index 0000000..4ca9c9f --- /dev/null +++ b/Jenkinsfile @@ -0,0 +1,25 @@ +// Intégration de la librairie Cadoles (voir https://forge.cadoles.com/Cadoles/Jenkins) +@Library("cadoles") _ + +// Utilisation du pipeline partagé pour les applications Symfony de Cadoles +// Le nom de l'image Docker passée en paramètre vous permet de préciser l'environnement de test +// de votre application Symfony +symfonyAppPipeline('ubuntu:22.04', [ + 'hooks': [ + // Run docker image build, verification and publication stages + 'postSymfonyAppPipeline': { + stage('Build and publish docker images') { + // Retrieve current tag + String currentTag = sh(script: 'git tag --points-at HEAD | head -n 1', returnStdout: true).trim() + // Retrieve default tag + String defaultTag = sh(returnStdout: true, script: 'git describe --always').trim() + kubecontainer.buildAndPublishImage([ + 'imageName': 'reg.cadoles.com/pcaseiro/myapplication-standalone', + 'imageTag': defaultTag, + 'dockerfile': './misc/images/myapplication-standalone/Dockerfile', + 'dryRun': false + ]) + } + } + ] +]) \ No newline at end of file diff --git a/Makefile b/Makefile index fce1e81..73ee55c 100644 --- a/Makefile +++ b/Makefile @@ -1,10 +1,12 @@ -################################ -# Makefile for Cadoles SP -################################ +################################################## +# Makefile for Cadoles Kubernetes Skeletor project +################################################## -IMAGE_REPO := reg.cadoles.com/cadoles -IMAGE_VERSION ?= 0.0.1 +COMMIT_ID ?= $(shell git rev-parse --short --verify HEAD) +CURRENT_BRANCH ?= $(shell git rev-parse --abbrev-ref HEAD | tr -c '[:alnum:]\n\r' '_'| tr '[:upper:]' '[:lower:]') + +IMAGE_REPO ?= reg.cadoles.com/cadoles +IMAGE_VERSION ?= 0.0.1-$(CURRENT_BRANCH) -DAY_SUFFIX_TAG ?= $(shell date +%Y%m%d) include main.mk diff --git a/init-app b/init-app index c19e80b..6cf7095 100755 --- a/init-app +++ b/init-app @@ -79,26 +79,26 @@ init_app() { rm -rf .git echo "Setting Dockerfiles" - dirs=$(find ${DOCKERFILES_ROOT}/* -type d -name 'MYAPPLICATION-*') + dirs=$(find ${DOCKERFILES_ROOT}/* -type d -name 'myapplication-*') for d in ${dirs}; do - mv ${d} $(echo ${d} | sed "s/MYAPPLICATION/${appName}/g") + mv ${d} $(echo ${d} | sed "s/myapplication/${appName}/g") done echo "Setting Kustomization directories" - dirs=$(find ${KUSTOMIZE_DIR}/* -type d -name 'MYAPPLICATION-*') + dirs=$(find ${KUSTOMIZE_DIR}/* -type d -name 'myapplication-*') for d in ${dirs}; do - mv ${d} $(echo ${d} | sed "s/MYAPPLICATION/${appName}/g") + mv ${d} $(echo ${d} | sed "s/myapplication/${appName}/g") done echo "Setting Kustomization files" - dirs=$(find ${KUSTOMIZE_DIR}/* -type f -name 'MYAPPLICATION-*') + dirs=$(find ${KUSTOMIZE_DIR}/* -type f -name 'myapplication-*') for d in ${dirs}; do - mv ${d} $(echo ${d} | sed "s/MYAPPLICATION/${appName}/g") + mv ${d} $(echo ${d} | sed "s/myapplication/${appName}/g") done - grep -rl "MYAPPLICATION" ${KUSTOMIZE_DIR} | xargs sed -i "s/MYAPPLICATION/${appName}/g" + grep -rl "myapplication" ${KUSTOMIZE_DIR} | xargs sed -i "s/myapplication/${appName}/g" echo "Setting Skaffold configuration" - sed -i "s/MYAPPLICATION/${appName}/g" ${appDest}/skaffold.yaml + sed -i "s/myapplication/${appName}/g" ${appDest}/skaffold.yaml echo "Setting docker registry secrets" create_docker_secret ${conf} diff --git a/main.mk b/main.mk index 3327adc..57ed59c 100644 --- a/main.mk +++ b/main.mk @@ -28,12 +28,12 @@ define install_trivy endef define release_image - docker tag $(IMAGE_REPO)/$1:$(IMAGE_VERSION) $(IMAGE_REPO)/$1:$(IMAGE_VERSION)-$(DAY_SUFFIX_TAG) ; \ + docker tag $(IMAGE_REPO)/$1:$(IMAGE_VERSION) $(IMAGE_REPO)/$1:$(IMAGE_VERSION)-$(COMMIT_ID) ; \ docker tag $(IMAGE_REPO)/$1:$(IMAGE_VERSION) $(IMAGE_REPO)/$1:$(IMAGE_VERSION); \ - docker tag $(IMAGE_REPO)/$1:$(IMAGE_VERSION) $(IMAGE_REPO)/$1:latest ; \ - docker push $(IMAGE_REPO)/$1:latest ; \ + docker tag $(IMAGE_REPO)/$1:$(IMAGE_VERSION) $(IMAGE_REPO)/$1:$(IMAGE_VERSION)-latest; \ + docker push $(IMAGE_REPO)/$1:$(IMAGE_VERSION)-latest ; \ docker push $(IMAGE_REPO)/$1:$(IMAGE_VERSION) ; \ - docker push $(IMAGE_REPO)/$1:$(IMAGE_VERSION)-$(DAY_SUFFIX_TAG) + docker push $(IMAGE_REPO)/$1:$(IMAGE_VERSION)-$(COMMIT_ID) endef #list: @@ -57,9 +57,12 @@ release: ${IMAGES_DIR}/* @for name in $(basename $(notdir $^)); do \ $(call release_image,$${name},base); \ done;\ + git tag -f -a $(IMAGE_VERSION) -m "Version $(IMAGE_VERSION) released"; \ + git tag -f -a $(IMAGE_VERSION)-$(COMMIT_ID) -m "Version $(IMAGE_VERSION)-$(COMMIT_ID) released"; \ + git tag -f -a $(IMAGE_VERSION)-latest -m "Version $(IMAGE_VERSION)-latest released"; \ _release: - docker tag $(IMAGE_FULL_NAME):$(IMAGE_TAG) $(IMAGE_FULL_NAME):$(IMAGE_TAG)-$(IMAGE_VERSION)-$(DAY_SUFFIX_TAG) + docker tag $(IMAGE_FULL_NAME):$(IMAGE_TAG) $(IMAGE_FULL_NAME):$(IMAGE_TAG)-$(IMAGE_VERSION)-$(COMMIT_ID) docker tag $(IMAGE_FULL_NAME):$(IMAGE_TAG) $(IMAGE_FULL_NAME):$(IMAGE_TAG)-$(IMAGE_VERSION) docker tag $(IMAGE_FULL_NAME):$(IMAGE_TAG) $(IMAGE_FULL_NAME):$(IMAGE_TAG)-latest docker push $(IMAGE_FULL_NAME):$(IMAGE_TAG)-$(IMAGE_VERSION)-$(DAY_SUFFIX_TAG) diff --git a/misc/images/MYAPPLICATION-kube/Dockerfile b/misc/images/myapplication-kube/Dockerfile similarity index 100% rename from misc/images/MYAPPLICATION-kube/Dockerfile rename to misc/images/myapplication-kube/Dockerfile diff --git a/misc/images/MYAPPLICATION-standalone/Dockerfile b/misc/images/myapplication-standalone/Dockerfile similarity index 100% rename from misc/images/MYAPPLICATION-standalone/Dockerfile rename to misc/images/myapplication-standalone/Dockerfile diff --git a/misc/k8s/kustomization/base/components/MYAPPLICATION-cnpg/configurations/cnpg-cluster.yaml b/misc/k8s/kustomization/base/components/myapplication-cnpg/configurations/cnpg-cluster.yaml similarity index 100% rename from misc/k8s/kustomization/base/components/MYAPPLICATION-cnpg/configurations/cnpg-cluster.yaml rename to misc/k8s/kustomization/base/components/myapplication-cnpg/configurations/cnpg-cluster.yaml diff --git a/misc/k8s/kustomization/base/components/MYAPPLICATION-cnpg/kustomization.yaml b/misc/k8s/kustomization/base/components/myapplication-cnpg/kustomization.yaml similarity index 66% rename from misc/k8s/kustomization/base/components/MYAPPLICATION-cnpg/kustomization.yaml rename to misc/k8s/kustomization/base/components/myapplication-cnpg/kustomization.yaml index f66d9fe..774bbd7 100644 --- a/misc/k8s/kustomization/base/components/MYAPPLICATION-cnpg/kustomization.yaml +++ b/misc/k8s/kustomization/base/components/myapplication-cnpg/kustomization.yaml @@ -8,24 +8,24 @@ configurations: - ./configurations/cnpg-cluster.yaml resources: -- ./resources/MYAPPLICATION-cnpg-cluster.yaml +- ./resources/myapplication-cnpg-cluster.yaml secretgenerator: -- name: MYAPPLICATION-postgres-admin +- name: myapplication-postgres-admin type: secret literals: - username=postgres - password=notsosecret -- name: MYAPPLICATION-postgres-user +- name: myapplication-postgres-user type: Secret literals: - - username=MYAPPLICATION + - username=myapplication - password=NotSoSecretButThisIsBad vars: -- name: MYAPPLICATION_DATABASE_SERVICE_NAME +- name: myapplication_DATABASE_SERVICE_NAME objref: - name: MYAPPLICATION-postgres + name: myapplication-postgres kind: Cluster apiVersion: postgresql.cnpg.io/v1 fieldref: diff --git a/misc/k8s/kustomization/base/components/MYAPPLICATION-cnpg/resources/MYAPPLICATION-cnpg-cluster.yaml b/misc/k8s/kustomization/base/components/myapplication-cnpg/resources/myapplication-cnpg-cluster.yaml similarity index 54% rename from misc/k8s/kustomization/base/components/MYAPPLICATION-cnpg/resources/MYAPPLICATION-cnpg-cluster.yaml rename to misc/k8s/kustomization/base/components/myapplication-cnpg/resources/myapplication-cnpg-cluster.yaml index 94c9e00..4432691 100644 --- a/misc/k8s/kustomization/base/components/MYAPPLICATION-cnpg/resources/MYAPPLICATION-cnpg-cluster.yaml +++ b/misc/k8s/kustomization/base/components/myapplication-cnpg/resources/myapplication-cnpg-cluster.yaml @@ -1,17 +1,17 @@ apiVersion: postgresql.cnpg.io/v1 kind: Cluster metadata: - name: MYAPPLICATION-postgres + name: myapplication-postgres spec: instances: 3 primaryUpdateStrategy: unsupervised superuserSecret: - name: MYAPPLICATION-postgres-admin + name: myapplication-postgres-admin bootstrap: initdb: - database: MYAPPLICATION - owner: MYAPPLICATION + database: myapplication + owner: myapplication secret: - name: MYAPPLICATION-postgres-user + name: myapplication-postgres-user storage: size: 20Gi diff --git a/misc/k8s/kustomization/base/kustomization.yaml b/misc/k8s/kustomization/base/kustomization.yaml index 7c35edb..afc81fb 100644 --- a/misc/k8s/kustomization/base/kustomization.yaml +++ b/misc/k8s/kustomization/base/kustomization.yaml @@ -1,10 +1,10 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization -namePrefix: MYAPPLICATION- +namePrefix: myapplication- components: -- components/MYAPPLICATION-cnpg +- components/myapplication-cnpg resources: -- resources/MYAPPLICATION-kube +- resources/myapplication-kube diff --git a/misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/kustomization.yaml b/misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/kustomization.yaml deleted file mode 100644 index 880026b..0000000 --- a/misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/kustomization.yaml +++ /dev/null @@ -1,6 +0,0 @@ -apiVersion: kustomize.config.k8s.io/v1beta1 -kind: Kustomization - -resources: -- ./resources/MYAPPLICATION-service.yaml -- ./resources/MYAPPLICATION-deployment.yaml diff --git a/misc/k8s/kustomization/base/resources/myapplication-kube/kustomization.yaml b/misc/k8s/kustomization/base/resources/myapplication-kube/kustomization.yaml new file mode 100644 index 0000000..1922147 --- /dev/null +++ b/misc/k8s/kustomization/base/resources/myapplication-kube/kustomization.yaml @@ -0,0 +1,6 @@ +apiVersion: kustomize.config.k8s.io/v1beta1 +kind: Kustomization + +resources: +- ./resources/myapplication-service.yaml +- ./resources/myapplication-deployment.yaml diff --git a/misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/resources/MYAPPLICATION-deployment.yaml b/misc/k8s/kustomization/base/resources/myapplication-kube/resources/myapplication-deployment.yaml similarity index 58% rename from misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/resources/MYAPPLICATION-deployment.yaml rename to misc/k8s/kustomization/base/resources/myapplication-kube/resources/myapplication-deployment.yaml index 3721a59..e5b0497 100644 --- a/misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/resources/MYAPPLICATION-deployment.yaml +++ b/misc/k8s/kustomization/base/resources/myapplication-kube/resources/myapplication-deployment.yaml @@ -2,26 +2,26 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - io.kompose.service: MYAPPLICATION - name: MYAPPLICATION + io.kompose.service: myapplication + name: myapplication spec: replicas: 3 selector: matchLabels: - io.kompose.service: MYAPPLICATION + io.kompose.service: myapplication strategy: type: Recreate template: metadata: labels: - io.kompose.service: MYAPPLICATION + io.kompose.service: myapplication spec: restartPolicy: Always containers: - - image: reg.cadoles.com/cadoles/MYAPPLICATION-kube + - image: reg.cadoles.com/cadoles/myapplication-kube imagePullPolicy: Always - name: MYAPPLICATION-php-fpm + name: myapplication-php-fpm args: ["/usr/sbin/php-fpm81", "-F", "-e"] resources: {} env: @@ -33,22 +33,22 @@ spec: value: 128m - name: PHP_FPM_LOG_LEVEL value: warning - - name: MYAPPLICATION_DATABASE_SERVICE_NAME - value: $(MYAPPLICATION_DATABASE_SERVICE_NAME)-rw - - image: reg.cadoles.com/cadoles/MYAPPLICATION-kube + - name: myapplication_DATABASE_SERVICE_NAME + value: $(myapplication_DATABASE_SERVICE_NAME)-rw + - image: reg.cadoles.com/cadoles/myapplication-kube imagePullPolicy: Always - name: MYAPPLICATION-nginx + name: myapplication-nginx args: ["/usr/sbin/nginx"] env: - - name: NGINX_MYAPPLICATION_UPSTREAM_BACKEND_SERVER + - name: NGINX_myapplication_UPSTREAM_BACKEND_SERVER value: 127.0.0.1:9000 - - name: NGINX_MYAPPLICATION_ROOT + - name: NGINX_myapplication_ROOT value: "/public" - - name: NGINX_MYAPPLICATION_PHP_INDEX + - name: NGINX_myapplication_PHP_INDEX value: "/index.php" - name: NGINX_ERROR_LOG_LEVEL value: "warn" - - name: NGINX_MYAPPLICATION_PHP_NON_FILE_PATTERN + - name: NGINX_myapplication_PHP_NON_FILE_PATTERN value: "^/index\\.php(/|$)" ports: - containerPort: 8080 diff --git a/misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/resources/MYAPPLICATION-service.yaml b/misc/k8s/kustomization/base/resources/myapplication-kube/resources/myapplication-service.yaml similarity index 50% rename from misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/resources/MYAPPLICATION-service.yaml rename to misc/k8s/kustomization/base/resources/myapplication-kube/resources/myapplication-service.yaml index 4887dd7..a7d55e3 100644 --- a/misc/k8s/kustomization/base/resources/MYAPPLICATION-kube/resources/MYAPPLICATION-service.yaml +++ b/misc/k8s/kustomization/base/resources/myapplication-kube/resources/myapplication-service.yaml @@ -2,13 +2,13 @@ apiVersion: v1 kind: Service metadata: labels: - io.kompose.service: MYAPPLICATION - name: MYAPPLICATION + io.kompose.service: myapplication + name: myapplication spec: type: ClusterIP ports: - - name: MYAPPLICATION-http + - name: myapplication-http port: 80 targetPort: 8080 selector: - io.kompose.service: MYAPPLICATION + io.kompose.service: myapplication diff --git a/misc/k8s/kustomization/overlays/dev/kustomization.yaml b/misc/k8s/kustomization/overlays/dev/kustomization.yaml index bf798dc..0f5d3bc 100644 --- a/misc/k8s/kustomization/overlays/dev/kustomization.yaml +++ b/misc/k8s/kustomization/overlays/dev/kustomization.yaml @@ -1,6 +1,6 @@ apiVersion: kustomize.config.k8s.io/v1beta1 kind: Kustomization -namespace: MYAPPLICATION-dev +namespace: myapplication-dev resources: - ../../base @@ -8,7 +8,7 @@ resources: - resources/ingress.yaml patches: -- path: patches/MYAPPLICATION-update-replicas.yaml +- path: patches/myapplication-update-replicas.yaml - path: patches/add-registry-pull-secret.yaml target: kind: Deployment diff --git a/misc/k8s/kustomization/overlays/dev/patches/add-registry-pull-secret.yaml b/misc/k8s/kustomization/overlays/dev/patches/add-registry-pull-secret.yaml deleted file mode 100644 index 1e4b8d2..0000000 --- a/misc/k8s/kustomization/overlays/dev/patches/add-registry-pull-secret.yaml +++ /dev/null @@ -1,4 +0,0 @@ -- op: add - path: "/spec/template/spec/imagePullSecrets" - value: - - name: regcred-dev diff --git a/misc/k8s/kustomization/overlays/dev/patches/MYAPPLICATION-update-replicas.yaml b/misc/k8s/kustomization/overlays/dev/patches/myapplication-update-replicas.yaml similarity index 56% rename from misc/k8s/kustomization/overlays/dev/patches/MYAPPLICATION-update-replicas.yaml rename to misc/k8s/kustomization/overlays/dev/patches/myapplication-update-replicas.yaml index 342f4b5..0627009 100644 --- a/misc/k8s/kustomization/overlays/dev/patches/MYAPPLICATION-update-replicas.yaml +++ b/misc/k8s/kustomization/overlays/dev/patches/myapplication-update-replicas.yaml @@ -2,7 +2,7 @@ apiVersion: apps/v1 kind: Deployment metadata: labels: - io.kompose.service: MYAPPLICATION - name: MYAPPLICATION + io.kompose.service: myapplication + name: myapplication spec: replicas: 1 diff --git a/misc/k8s/kustomization/overlays/dev/resources/ingress.yaml b/misc/k8s/kustomization/overlays/dev/resources/ingress.yaml index 256c5d0..e969878 100644 --- a/misc/k8s/kustomization/overlays/dev/resources/ingress.yaml +++ b/misc/k8s/kustomization/overlays/dev/resources/ingress.yaml @@ -1,7 +1,7 @@ apiVersion: networking.k8s.io/v1 kind: Ingress metadata: - name: MYAPPLICATION + name: myapplication annotations: nginx.ingress.kubernetes.io/proxy-body-size: "138m" nginx.ingress.kubernetes.io/enable-cors: "true" #cf 01 @@ -9,13 +9,13 @@ metadata: spec: ingressClassName: nginx rules: - - host: MYAPPLICATION.dev.local + - host: myapplication.dev.local http: paths: - path: / pathType: Prefix backend: service: - name: MYAPPLICATION + name: myapplication port: number: 8080 diff --git a/misc/k8s/kustomization/overlays/dev/resources/namespace.yaml b/misc/k8s/kustomization/overlays/dev/resources/namespace.yaml index 04c104f..fadee54 100644 --- a/misc/k8s/kustomization/overlays/dev/resources/namespace.yaml +++ b/misc/k8s/kustomization/overlays/dev/resources/namespace.yaml @@ -1,4 +1,4 @@ apiVersion: v1 kind: Namespace metadata: - name: MYAPPLICATION-dev + name: myapplication-dev diff --git a/skaffold.yaml b/skaffold.yaml index 519bf13..101b977 100644 --- a/skaffold.yaml +++ b/skaffold.yaml @@ -29,7 +29,7 @@ build: sha256: {} artifacts: - - image: reg.cadoles.com/cadoles/MYAPPLICATION-kube + - image: reg.cadoles.com/cadoles/myapplication-kube context: . sync: infer: @@ -41,7 +41,7 @@ build: - composer.json - composer.lock kaniko: - dockerfile: misc/images/MYAPPLICATION-kube/Dockerfile + dockerfile: misc/images/myapplication-kube/Dockerfile cache: {} deploy: -- 2.17.1