From a16ccb0d9a747a3ed73f23b847a78ac425ae540f Mon Sep 17 00:00:00 2001 From: Edward Viaene Date: Fri, 3 Aug 2018 13:36:02 +0000 Subject: [PATCH] helm + jenkins --- helm/jenkins/Jenkinsfile.build | 34 ++++++++++++++++++++++++ helm/jenkins/Jenkinsfile.deploy | 46 +++++++++++++++++++++++++++++++++ helm/jenkins/README.md | 4 +++ helm/put-bucket-policy.sh | 32 +++++++++++++++++++++++ helm/setup-s3-helm-repo.sh | 11 +++++++- 5 files changed, 126 insertions(+), 1 deletion(-) create mode 100644 helm/jenkins/Jenkinsfile.build create mode 100644 helm/jenkins/Jenkinsfile.deploy create mode 100644 helm/jenkins/README.md create mode 100755 helm/put-bucket-policy.sh diff --git a/helm/jenkins/Jenkinsfile.build b/helm/jenkins/Jenkinsfile.build new file mode 100644 index 0000000..daf4db9 --- /dev/null +++ b/helm/jenkins/Jenkinsfile.build @@ -0,0 +1,34 @@ +pipeline { + agent { + kubernetes { + label 'helm-pod' + containerTemplate { + name 'helm' + image 'wardviaene/helm-s3' + ttyEnabled true + command 'cat' + } + } + } + stages { + stage('Run helm') { + steps { + container('helm') { + git url: 'git://github.com/wardviaene/kubernetes-course.git', branch: 'master' + sh ''' + HELM_BUCKET=helm-rytcufor + PACKAGE=demo-chart + export AWS_REGION=eu-west-1 + + cp -r /home/helm/.helm ~ + helm repo add my-charts s3://${HELM_BUCKET}/charts + cd helm/${PACKAGE} + helm dependency update + helm package . + helm s3 push --force ${PACKAGE}-*.tgz my-charts + ''' + } + } + } + } +} diff --git a/helm/jenkins/Jenkinsfile.deploy b/helm/jenkins/Jenkinsfile.deploy new file mode 100644 index 0000000..d29548d --- /dev/null +++ b/helm/jenkins/Jenkinsfile.deploy @@ -0,0 +1,46 @@ +pipeline { + agent { + kubernetes { + label 'helm-pod' + defaultContainer 'jnlp' + yaml """ +apiVersion: v1 +kind: Pod +metadata: + labels: + some-label: helm-pod +spec: + serviceAccount: jenkins + containers: + - name: helm-pod + image: wardviaene/helm-s3 + command: + - cat + tty: true +""" + } + } + stages { + stage('Run helm') { + steps { + container('helm-pod') { + git url: 'git://github.com/wardviaene/kubernetes-course.git', branch: 'master' + sh ''' + HELM_BUCKET=helm-rytcufor + PACKAGE=demo-chart + export AWS_REGION=eu-west-1 + + cp -r /home/helm/.helm ~ + helm repo add my-charts s3://${HELM_BUCKET}/charts + DEPLOYED=$(helm list |grep -E "^${PACKAGE}" |grep DEPLOYED |wc -l) + if [ $DEPLOYED == 0 ] ; then + helm install --name ${PACKAGE} my-charts/${PACKAGE} + else + helm upgrade ${PACKAGE} my-charts/${PACKAGE} + fi + ''' + } + } + } + } +} diff --git a/helm/jenkins/README.md b/helm/jenkins/README.md new file mode 100644 index 0000000..47c72af --- /dev/null +++ b/helm/jenkins/README.md @@ -0,0 +1,4 @@ +# install jenkins +``` +helm install --name jenkins --set rbac.install=true,Master.RunAsUser=1000,Master.FsGroup=1000 stable/jenkins +``` diff --git a/helm/put-bucket-policy.sh b/helm/put-bucket-policy.sh new file mode 100755 index 0000000..f6065b3 --- /dev/null +++ b/helm/put-bucket-policy.sh @@ -0,0 +1,32 @@ +#!/bin/bash +DEFAULT_REGION="eu-west-1" +AWS_REGION="${AWS_REGION:-${DEFAULT_REGION}}" + +BUCKET="helm-rytcufor" +NODE_ROLE_ARN="arn:aws:iam::*" + +export AWS_REGION + +aws s3api put-bucket-policy --bucket ${BUCKET} --policy '{ + "Statement": [ + { + "Effect": "Allow", + "Principal": { + "AWS": "'${NODE_ROLE_ARN}'" + }, + "Action": [ + "s3:GetObject", + "s3:PutObject" + ], + "Resource": "arn:aws:s3:::'${BUCKET}'/*" + }, + { + "Effect": "Allow", + "Principal": { + "AWS": "'${NODE_ROLE_ARN}'" + }, + "Action": "s3:ListBucket", + "Resource": "arn:aws:s3:::'${BUCKET}'" + } + ] +}' diff --git a/helm/setup-s3-helm-repo.sh b/helm/setup-s3-helm-repo.sh index 97ace74..1904d18 100755 --- a/helm/setup-s3-helm-repo.sh +++ b/helm/setup-s3-helm-repo.sh @@ -2,18 +2,27 @@ set -e +# create random string RANDOM_STRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 8 | tr '[:upper:]' '[:lower:]' | head -n 1) # it's important to set the AWS_REGION if not set. Change the default DEFAULT_REGION="eu-west-1" AWS_REGION="${AWS_REGION:-${DEFAULT_REGION}}" +export AWS_REGION + +# create s3 bucket if [ "$AWS_REGION" == "us-east-1" ] ; then aws s3api create-bucket --bucket helm-${RANDOM_STRING} else aws s3api create-bucket --bucket helm-${RANDOM_STRING} --region $AWS_REGION --create-bucket-configuration LocationConstraint=${AWS_REGION} fi +# install helm s3 plugin helm plugin install https://github.com/hypnoglow/helm-s3.git + +# initialize s3 bucket helm s3 init s3://helm-${RANDOM_STRING}/charts -helm repo add my-charts s3://helm-lcxqoc24/charts + +# add repository to helm +helm repo add my-charts s3://helm-${RANDOM_STRING}/charts