Jenkinsfile to automatically deploy to EKS

6/3/2019

How do I pass my aws credentials when I am running a Jenkinsjob taking this as an example https://github.com/PaulMaddox/amazon-eks-kubectl

$ docker run -v ~/.aws:/home/kubectl/.aws -e CLUSTER=demo    maddox/kubectl get services

The above works on my laptop , but I want to pass aws credentials on the file.I have aws configured in my Jenkins-->credentials .I also have a bitbucket repo which contains a Jenkinsfile and a yam file for "service" and "deployment"

the way I do it now is run the kubectl create -f filename.yaml and it deploys to eks .. just want to do the same thing but automate it with a Jenkinsfile , suggestions on how to do it either with kubectl or with helm

-- user17970
amazon-eks
amazon-iam
amazon-web-services
kubectl
kubernetes-helm

1 Answer

6/5/2019

In your Jenkinsfile you should include similar section:

 stage('Deploy on Dev') {
    node('master'){
        withEnv(["KUBECONFIG=${JENKINS_HOME}/.kube/dev-config","IMAGE=${ACCOUNT}.dkr.ecr.us-east-1.amazonaws.com/${ECR_REPO_NAME}:${IMAGETAG}"]){
            sh "sed -i 's|IMAGE|${IMAGE}|g' k8s/deployment.yaml"
            sh "sed -i 's|ACCOUNT|${ACCOUNT}|g' k8s/service.yaml"
            sh "sed -i 's|ENVIRONMENT|dev|g' k8s/*.yaml"
            sh "sed -i 's|BUILD_NUMBER|01|g' k8s/*.yaml"
            sh "kubectl apply -f k8s"
            DEPLOYMENT = sh (
                script: 'cat k8s/deployment.yaml | yq -r .metadata.name',
                returnStdout: true
            ).trim()
            echo "Creating k8s resources..."
            sleep 180
            DESIRED= sh (
                script: "kubectl get deployment/$DEPLOYMENT | awk '{print \$2}' | grep -v DESIRED",
                returnStdout: true
            ).trim()
            CURRENT= sh (
                script: "kubectl get deployment/$DEPLOYMENT | awk '{print \$3}' | grep -v CURRENT",
                returnStdout: true
            ).trim()
            if (DESIRED.equals(CURRENT)) {
                currentBuild.result = "SUCCESS"
                return
            } else {
                error("Deployment Unsuccessful.")
                currentBuild.result = "FAILURE"
                return
            }
        }
    }
  }
}

which will be responsible for automating deployment proccess.

I hope it helps.

-- MaggieO
Source: StackOverflow