From 5d06e769fbc23ef42567f08db182635982b48a7e Mon Sep 17 00:00:00 2001 From: Sciocatti Date: Mon, 9 Jan 2023 07:33:28 +0200 Subject: [PATCH] Initial Commit. --- .gitignore | 4 ++- template/Dockerfile | 2 ++ template/Jenkinsfile | 35 ++++++++++++++++++++ template/deployment.yaml | 51 +++++++++++++++++++++++++++++ template/setup.sh | 71 ++++++++++++++++++++++++++++++++++++++++ template/start.sh | 6 ++++ 6 files changed, 168 insertions(+), 1 deletion(-) create mode 100644 template/Dockerfile create mode 100644 template/Jenkinsfile create mode 100644 template/deployment.yaml create mode 100755 template/setup.sh create mode 100644 template/start.sh diff --git a/.gitignore b/.gitignore index d4777d2..4c4c565 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,8 @@ .LSOverride # Icon must end with two \r -Icon +Icon + # Thumbnails ._* @@ -26,3 +27,4 @@ Network Trash Folder Temporary Items .apdisk +data/ diff --git a/template/Dockerfile b/template/Dockerfile new file mode 100644 index 0000000..61da48a --- /dev/null +++ b/template/Dockerfile @@ -0,0 +1,2 @@ +FROM {{k3sContainerBase}} + diff --git a/template/Jenkinsfile b/template/Jenkinsfile new file mode 100644 index 0000000..a783476 --- /dev/null +++ b/template/Jenkinsfile @@ -0,0 +1,35 @@ +#!groovy + +pipeline { + agent { + kubernetes { + defaultContainer 'jnlp' + yaml """ +apiVersion: v1 +kind: Pod +metadata: + name: {{k3sApp}}-"""+env.BRANCH_NAME+"""-build +spec: + serviceAccountName: jenkins-admin + containers: + - name: deploy + image: 192.168.195.195:30000/nginx:latest + command: ["tail"] + args: ["-f", "/dev/null"] + restartPolicy: Never + """ + } + } + stages { + stage('Kubernetes Deploy') { + steps { + container("deploy") { + sh 'curl -LO "https://storage.googleapis.com/kubernetes-release/release/v1.24.3/bin/linux/arm64/kubectl"' + sh 'chmod u+x ./kubectl' + sh 'sed -ie "s/{{replaceMe}}/`date +%s`/g" `pwd`/cicd/'+env.BRANCH_NAME+'/deployment.yaml' + sh './kubectl replace -f `pwd`/cicd/'+env.BRANCH_NAME+'/deployment.yaml' + } + } + } + } +} \ No newline at end of file diff --git a/template/deployment.yaml b/template/deployment.yaml new file mode 100644 index 0000000..1c13b79 --- /dev/null +++ b/template/deployment.yaml @@ -0,0 +1,51 @@ +apiVersion: v1 +kind: Service +metadata: + name: {{k3sApp}}-{{k3sEnv}}-svc + namespace: {{k3sNamespace}} +spec: + type: NodePort + selector: + app: {{k3sApp}}-{{k3sEnv}} + ports: + - name: http + nodePort: {{k3sport}} + port: 5432 + targetPort: 5432 + +--- +apiVersion: apps/v1 +kind: Deployment +metadata: + name: {{k3sApp}}-{{k3sEnv}} + namespace: {{k3sNamespace}} +spec: + replicas: 1 + strategy: + rollingUpdate: + maxSurge: 1 + maxUnavailable: 1 + type: RollingUpdate + selector: + matchLabels: + app: {{k3sApp}}-{{k3sEnv}} + template: + metadata: + labels: + app: {{k3sApp}}-{{k3sEnv}} + delpoymentDate: "{{replaceMe}}" + spec: + containers: + - name: {{k3sApp}}-{{k3sEnv}} + image: 192.168.195.195:30000/{{k3sApp}}:{{k3sEnv}} + imagePullPolicy: Always + ports: + - name: http + containerPort: 5432 + readinessProbe: + httpGet: + path: / + port: 5432 + initialDelaySeconds: 5 + periodSeconds: 5 + successThreshold: 1 \ No newline at end of file diff --git a/template/setup.sh b/template/setup.sh new file mode 100755 index 0000000..81bf9a9 --- /dev/null +++ b/template/setup.sh @@ -0,0 +1,71 @@ +#!/bin/bash + +#./setup.sh 3000 192.168.195.195:30000/nginx:latest cicd/Jenkinsfile + +folderName=${PWD##*/} +folderName=${folderName:-/} + +echo "Hello, lets get your project set up. Please follow the prompts attentively." + +read -p "Do you want to run this project locally? [Yn] " localProject + +if [ $localProject == 'Y' ] || [ $localProject == 'y' ] +then + echo "Configuring local settings" + read -p " Local port to expose DB on? [eg 5432] " localPort + read -p " Postgres container to use? [eg postgis/postgis:latest] " localContainer + + echo " Creating start script" + cp template/start.sh start.sh + sed -ie "s|{{localPort}}|$localPort|g" start.sh + sed -ie "s|{{localContainer}}|$localContainer|g" start.sh + chmod +x start.sh + rm start.she +fi + +read -p "Are you deploying this with the Sciocatti Jenkins pipeline? [Yn] " useJenkins + +if [ $useJenkins == 'Y' ] || [ $useJenkins == 'y' ] +then + echo "Configuring deployment settings. See the Readme for info on deployable branches." + read -p " K3S App name: " k3sApp + read -p " K3S namespace: [eg utility] " k3sNamespace + read -p " K3S base container: [eg postgis/postgis:latest] " k3sContainerBase + read -p " K3S NodePort: " k3sport + + k3sportMain=$((k3sport+1)) + k3sportStage=$((k3sport+2)) + + echo "Creating Folders" + echo " Creating cicd/" + mkdir cicd + echo " Creating cicd/main" + mkdir cicd/main + + echo "Loading files" + echo " Loading cicd/Jenkinsfile" + cp template/Jenkinsfile cicd/Jenkinsfile + sed -ie "s|{{k3sApp}}|$k3sApp|g" cicd/Jenkinsfile + rm cicd/Jenkinsfilee + + echo " Loading main" + k3sEnv=main + echo " Loading cicd/main/Dockerfile" + cp template/Dockerfile cicd/main/Dockerfile + sed -ie "s|{{k3sContainerBase}}|$k3sContainerBase|g" cicd/main/Dockerfile + rm cicd/main/Dockerfilee + + echo " Loading cicd/main/deployment.yaml" + cp template/deployment.yaml cicd/main/deployment.yaml + sed -ie "s|{{k3sApp}}|$k3sApp|g" cicd/main/deployment.yaml + sed -ie "s|{{k3sport}}|$k3sportMain|g" cicd/main/deployment.yaml + sed -ie "s|{{k3sNamespace}}|$k3sNamespace|g" cicd/main/deployment.yaml + sed -ie "s|{{k3sEnv}}|$k3sEnv|g" cicd/main/deployment.yaml + rm cicd/main/deployment.yamle + +fi + +echo "Removing template folder" +rm -rf template + +echo "Thank you! Happy building..." \ No newline at end of file diff --git a/template/start.sh b/template/start.sh new file mode 100644 index 0000000..5575e2c --- /dev/null +++ b/template/start.sh @@ -0,0 +1,6 @@ +#!/bin/bash +echo "" +echo "Starting an Postgres container listening on http://localhost:{{localPort}}" +echo "" +echo "" +docker run --rm -v $(pwd)/data/:/var/lib/postgresql/data/ -p {{localPort}}:80 {{localContainer}} \ No newline at end of file