简体   繁体   English

将jenkins与terrain集成在terraform上

[英]integrate jenkins on terraform with bitbucket

I have created terraform script for aws architecture which includes ec2 instance and jenkins on ec2. 我为aw体系结构创建了terraform脚本,其中包括ec2实例和ec2上的jenkins。 I am new to jenkins and trying to figure how to integrate it with bitbucket using the existing terraform script. 我是jenkins的新手,并试图弄清楚如何使用现有的terraform脚本将其与bitbucket集成。 Any help would be greatly appreciated. 任何帮助将不胜感激。

CI/CD pipeline workflow for applying changes to the infrastructure using terraform: CI / CD管道工作流程,用于使用terraform将更改应用于基础架构:

  1. Developer or Operations Engineer change the terraform configuration file in his local machine and commit the code to BitBucket. 开发人员或运营工程师在其本地计算机上更改terraform配置文件,并将代码提交给BitBucket。
  2. Gitbucket webhook triggers a continuous integration job to jenkins. Gitbucket Webhook触发了jenkins的持续集成工作。
  3. Jenkins will pull the latest code from the configured repo which contains terraform files to its workspace. Jenkins将从配置的仓库中提取最新代码,该仓库包含terraform文件到其工作空间。
  4. It reads the terraform configuration then initialize the remote consul backend. 它读取terraform配置,然后初始化远程领事后端。
  5. Terraform generates a plan about the changes that has to be applied on the infrastructure Terraform生成有关必须在基础架构上应用的更改的计划
  6. Jenkins send notification to a slack channel about the changes for manual approval. 詹金斯(Jenkins)将闲置变更的通知发送到闲置渠道,以供人工批准。
  7. Here, the user can approve or disapprove the terraform plan. 在这里,用户可以批准或不批准地形计划。
  8. The user input is sent to jenkins server for proceeding with the further action. 用户输入将发送到jenkins服务器以继续执行进一步的操作。
  9. Once the changes are approved by an operator, jenkins will execute terraform apply command to reflect the changes to the infrastructure. 一旦更改得到操作员的批准,詹金斯将执行terraform apply命令以将更改反映到基础架构。
  10. Terraform will create an report about the resources and its dependency created while executing the plan. Terraform将创建有关在执行计划时创建的资源及其依赖性的报告。
  11. Terraform will provision the resources in the provider environment. Terraform将在提供程序环境中配置资源。
  12. Jenkins will again send a notification to the slack channel about the status of the infrastructure after the applying changes on it. 在对基础结构应用更改后,Jenkins将再次向松弛通道发送有关基础结构状态的通知。 Once the job is executed, Jenkin pipeline job is configured to clean up the workspace created by the job. 执行作业后,将配置Jenkin管道作业以清理作业创建的工作空间。

How to setup the deployment environment? 如何设置部署环境?

  1. Create a repo in scm tools like gitlab or bitbucket and commit the terraform configuration and its dependency module to the repo. 在gitlab或bitbucket之类的scm工具中创建一个仓库,然后将terraform配置及其依赖模块提交给仓库。 If you are using any third party remote module as a dependency, it will be automatically downloaded while execution. 如果您使用任何第三方远程模块作为依赖项,则它将在执行时自动下载。
  2. If you do not have Jenkins server, then just pull a jenkins docker image and run it in your local machine. 如果您没有Jenkins服务器,则只需提取一个jenkins docker映像并在本地计算机上运行它即可。 If you are setting it up in cloud environment, check the jenkins virtual machine image from marketplace to set up the environment and configure the required plugins. 如果要在云环境中进行设置,请从市场上检查jenkins虚拟机映像以设置环境并配置所需的插件。
  3. Create a webhook in your bitbucket repo settings to invoke a http call to your jenkins call back url for triggering continuous integration job. 在bitbucket存储库设置中创建一个webhook,以调用对jenkins回调URL的http调用,以触发持续集成作业。
  4. If you have an existing jenkins server, ensure pipeline plugin is installed in the jenkins server. 如果您有现有的jenkins服务器,请确保在jenkins服务器中安装了管道插件。 Otherwise goto "Manage plugins" and install pipeline plugin. 否则,转到“管理插件”并安装管道插件。
  5. In this project, we are using consul as a remote backend for state storing and state locking. 在这个项目中,我们使用领事作为状态存储和状态锁定的远程后端。 It is not recommended to use local state for the case where multiple people involved in the project and for production deployments. 在项目涉及多个人和生产部署的情况下,建议不要使用本地状态。 It is good to use remote backend which provides highly available storage with state lock functionalities to avoid writing the state by multiple users at a time. 最好使用远程后端,该后端可提供具有状态锁定功能的高可用性存储,以避免一次由多个用户写入状态。
  6. If you do not have consul key-value store in your environment, just pull consul docker image and setup a single node cluster. 如果您的环境中没有领事键值存储,只需拉出领事docker映像并设置单个节点集群即可。 If it is production deployment, setup a distributed key-value store. 如果是生产部署,请设置分布式键值存储。
  7. Create an application in slack and note down the slack integration details for configuring it in Jenkinsfile. 在Slack中创建一个应用程序,并记下在Jenkinsfile中配置它的Slack集成详细信息。
  8. Configure your provider details and backend details in main terraform configuration file either by environment variable or persisting in a repo. 通过环境变量或持久化在回购中,在主要terraform配置文件中配置提供者详细信息和后端详细信息。 In my case, I am going to provision a resource in AWS and my CI server is hosted in AWS. 就我而言,我将在AWS中配置资源,而CI服务器托管在AWS中。 So I am assigning an IAM role to my server with sufficient privileges. 因此,我正在使用足够的权限向服务器分配IAM角色。
  9. Create a new project in Jenkins by using pipeline plugin. 使用管道插件在Jenkins中创建一个新项目。
  10. Add the Jenkinsfile where the pipeline stages are defined. 在定义管道阶段的位置添加Jenkinsfile。 Save the job and trigger it manually for testing. 保存作业并手动触发以进行测试。 Then apply changes to the configuration and commit the changes to the bitbucket and ensure the job is automatically triggered. 然后将更改应用于配置,并将更改提交到bitbucket,并确保自动触发作业。 Check Jenkins log for more details about the job. 查看Jenkins日志以获取有关该作业的更多详细信息。

在此处输入图片说明

 ###Jenkinsfile### import groovy.json.JsonOutput //git env vars env.git_url = 'https://user@bitbucket.org/user/terraform-ci.git' env.git_branch = 'master' env.credentials_id = '1' //slack env vars env.slack_url = 'https://hooks.slack.com/services/SDKJSDKS/SDSDJSDK/SDKJSDKDS23434SDSDLCMLC' env.notification_channel = 'my-slack-channel' //jenkins env vars env.jenkins_server_url = 'https://52.79.46.98' env.jenkins_node_custom_workspace_path = "/opt/bitnami/apps/jenkins/jenkins_home/${JOB_NAME}/workspace" env.jenkins_node_label = 'master' env.terraform_version = '0.11.10' def notifySlack(text, channel, attachments) { def payload = JsonOutput.toJson([text: text, channel: channel, username: "Jenkins", attachments: attachments ]) sh "export PATH=/opt/bitnami/common/bin:$PATH && curl -X POST --data-urlencode \\'payload=${payload}\\' ${slack_url}" } pipeline { agent { node { customWorkspace "$jenkins_node_custom_workspace_path" label "$jenkins_node_label" } } stages { stage('fetch_latest_code') { steps { git branch: "$git_branch" , credentialsId: "$credentials_id" , url: "$git_url" } } stage('install_deps') { steps { sh "sudo apt install wget zip python-pip -y" sh "cd /tmp" sh "curl -o terraform.zip https://releases.hashicorp.com/terraform/'$terraform_version'/terraform_'$terraform_version'_linux_amd64.zip" sh "unzip terraform.zip" sh "sudo mv terraform /usr/bin" sh "rm -rf terraform.zip" } } stage('init_and_plan') { steps { sh "sudo terraform init $jenkins_node_custom_workspace_path/workspace" sh "sudo terraform plan $jenkins_node_custom_workspace_path/workspace" notifySlack("Build completed! Build logs from jenkins server $jenkins_server_url/jenkins/job/$JOB_NAME/$BUILD_NUMBER/console", notification_channel, []) } } stage('approve') { steps { notifySlack("Do you approve deployment? $jenkins_server_url/jenkins/job/$JOB_NAME", notification_channel, []) input 'Do you approve deployment?' } } stage('apply_changes') { steps { sh "echo 'yes' | sudo terraform apply $jenkins_node_custom_workspace_path/workspace" notifySlack("Deployment logs from jenkins server $jenkins_server_url/jenkins/job/$JOB_NAME/$BUILD_NUMBER/console", notification_channel, []) } } } post { always { cleanWs() } } } ###Code Completed### 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 bitbucket 中找不到 terraform - terraform not found in bitbucket Terraform Output 通过 Jenkins - Terraform Output via Jenkins 如何将terraform与atlassian / localstack集成? - How to integrate terraform with atlassian/localstack? 如何使用jenkins将bitbucket中的repo同步到S3 - How to sync repo in bitbucket to S3 with jenkins 使用 bitbucket 和 terraform 部署后无法连接到 EKS 集群 - Cannot connect to the EKS cluster after deploying with bitbucket and terraform 使用 bitbucket 作为源时,Terraform 无法从模块中获取 arguments - Terraform not able to fetch arguments from modules when using bitbucket as source 在 Jenkins 管道中出现错误“terraform: command not found” - In Jenkins pipeline getting error “terraform: command not found” 是否可以以这种方式集成 AWS Codepipeline 和 Jenkins? - Is it possible to integrate AWS Codepipeline and Jenkins in such a way? 如何使用Bitbucket,Jenkins和ElasticBeanstalk构建部署管道 - How to build a deployment Pipeline Using Bitbucket, Jenkins and ElasticBeanstalk 使用 Terraform 将 AWS API 网关方法与多个 Lambda 集成 - Integrate AWS API Gateway methods with multiple Lambdas using Terraform
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM