From d5b40fc924749d81ee0f9aabf00a57f8c0c769c7 Mon Sep 17 00:00:00 2001 From: Sciocatti Date: Wed, 11 Jan 2023 23:32:03 +0200 Subject: [PATCH] Initial Commit --- Dockerfile | 1 + README.md | 28 +++++++++++++++++++++++++- config.conf | 3 +++ template/Jenkinsfile | 47 ++++++++++++++++++++++++++++++++++++++++++++ template/setup.sh | 27 +++++++++++++++++++++++++ 5 files changed, 105 insertions(+), 1 deletion(-) create mode 100644 Dockerfile create mode 100644 config.conf create mode 100644 template/Jenkinsfile create mode 100755 template/setup.sh diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6e600c7 --- /dev/null +++ b/Dockerfile @@ -0,0 +1 @@ +FROM nginx:latest \ No newline at end of file diff --git a/README.md b/README.md index ac1f97a..422b6fd 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,29 @@ # Utility-Image-Builder -A template repo for maintaining base Docker images in my private registry. \ No newline at end of file +A template repo for maintaining base Docker images in my private registry. + +> NOTE: The steps below are not for this repo, but for those using it as a template. + +## Bootstrapping +> If needed, steps starting with `[sudo]` require elevated permissions. + +> Go through the entire process before committing to the main branch. + +1. Create a new repository by clicking the "Use this template" button on the template repo page. +1. Configure the new repo settings. At the bottom, select `Template Items` -> `Git Content`. +1. Create the new repo. +1. Pull new repo to local PC. +1. [sudo] Give the setup script permission to execute: + ```bash + chmod +x template/setup.sh + ``` +1. [sudo] Run the start script and fill in the prompts: + ```bash + ./template/setup.sh + ``` +1. Change the Dockerfile as desired. NOTE: Committing before doing this will still build an image... +1. Populate the `config.json`. This is to make it easy to do version management +1. Commit to main. If you are committing to an organization linked to the Jenkins server the image will automatically be build for +the architecture you specified. + +> Note that the bootstrapped repo has to be in a organization where the Jenkins user has visibility, and the organization must be configured and linked to the Jenkins server. \ No newline at end of file diff --git a/config.conf b/config.conf new file mode 100644 index 0000000..5d50dbb --- /dev/null +++ b/config.conf @@ -0,0 +1,3 @@ +destinationRegistry=192.168.195.195:30000 +imageName=nginx +imageTag=latest \ No newline at end of file diff --git a/template/Jenkinsfile b/template/Jenkinsfile new file mode 100644 index 0000000..ef796ae --- /dev/null +++ b/template/Jenkinsfile @@ -0,0 +1,47 @@ +#!groovy + +pipeline { + agent { + kubernetes { + defaultContainer 'jnlp' + yaml """ +apiVersion: v1 +kind: Pod +metadata: + name: {{k3sApp}}-image-build +spec: + serviceAccountName: jenkins-admin + nodeSelector: + kubernetes.io/arch: {{k3sArch}} + containers: + - name: kaniko + image: gcr.io/kaniko-project/executor:debug + command: ["tail"] + args: ["-f", "/dev/null"] + volumeMounts: + - name: kaniko-secret + mountPath: /kaniko/.docker + restartPolicy: Never + volumes: + - name: kaniko-secret + secret: + secretName: dockercred + items: + - key: .dockerconfigjson + path: config.json + """ + } + } + stages { + stage('Docker Build and Push') { + steps { + container("kaniko") { + sh 'destinationRegistry=`sed -n \'s/^destinationRegistry=\(.*\)/\1/p\' < config.conf`' + sh 'imageName=`sed -n \'s/^imageName=\(.*\)/\1/p\' < config.conf`' + sh 'imageTag=`sed -n \'s/^imageTag=\(.*\)/\1/p\' < config.conf`' + sh '/kaniko/executor --dockerfile `pwd`/Dockerfile --context `pwd` --destination ${destinationRegistry}/${imageName}:${imageTag}' + } + } + } + } +} \ No newline at end of file diff --git a/template/setup.sh b/template/setup.sh new file mode 100755 index 0000000..dd88cc1 --- /dev/null +++ b/template/setup.sh @@ -0,0 +1,27 @@ +#!/bin/bash + +echo "Hello, lets get your project set up. Please follow the prompts attentively." + +echo "" +read -p " K3S Build Pod Name: " k3sApp +read -p " Build Architecture: [amd64 | arm64] " k3sArch + +echo "" +echo "Creating Folders" + echo " Creating cicd/" + mkdir cicd + +echo "" +echo "Loading files" +echo " Loading cicd/Jenkinsfile" +cp template/Jenkinsfile cicd/Jenkinsfile +sed -ie "s|{{k3sApp}}|$k3sApp|g" cicd/Jenkinsfile +sed -ie "s|{{k3sArch}}|$k3sArch|g" cicd/Jenkinsfile +rm cicd/Jenkinsfilee + +echo "" +echo "Removing template folder" +# rm -rf template + +echo "" +echo "Thank you! Happy building..."