Jenkinsfile自动部署到EKS

时间:2019-06-03 18:19:44

标签: amazon-web-services amazon-iam kubernetes-helm kubectl amazon-eks

我在运行Jenkinsjob时如何通过aws凭证 以这个为例https://github.com/PaulMaddox/amazon-eks-kubectl

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

以上内容可在笔记本电脑上使用,但我想在文件上传递aws凭证。我在Jenkins->凭证中配置了aws。我还有一个bitbucket存储库,其中包含Jenkinsfile和yam文件,用于“服务” ”和“部署”

我现在执行的方式是运行kubectl create -f filename.yaml并将其部署到eks ..只是想做同样的事情,但要用Jenkinsfile使其自动化,建议如何使用kubectl或掌上

1 个答案:

答案 0 :(得分:0)

在您的Jenkins文件中,应包括类似的部分:

 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
            }
        }
    }
  }
}

将负责自动化部署过程。

希望对您有帮助。