CI/CD - 环境搭建(7) - 集成Terraform

Goal

实现Jenkins + Terraform在AWS上创建基本环境。

Prerequisites

Terraform Config

创建main.tf,并push到Github上。

1
2
3
4
5
6
7
8
9
10
11
provider "aws" {
region = "us-east-1"
}

resource "aws_instance" "yongfeiuall" {
ami = "ami-1853ac65"
instance_type = "t2.micro"
tags{
Name = "integration with jenkins"
}
}

Jenkins Config

创建Pipeline Job:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
node {

stage('Preparation') { // for display purposes
// Get some code from a GitHub repository
git credentialsId: 'ee7a726b-e672-424d-9b78-e8015668d55a', url: 'git@github.com:yongfeiuall/pipeline-terraform-example.git'
}

stage('variable file') {
sh 'sudo cp ~/variables.tf ~/.jenkins/workspace/ci-terraform'
}

stage('init'){
sh "terraform --version"
sh "terraform init"
}

stage('plan'){
sh "terraform plan"
}

stage('apply'){
sh "terraform apply -auto-approve"
}
}

说明:

  • Terraform预先安装在了Server上,没有用Jenkins安装
  • AWS access key和secret key不能公开,所以加了stage('variable file),提前放到Server上去
  • terraform apply加上-auto-approve

运行

唐胡璐 wechat
欢迎您扫一扫上面的微信公众号,订阅我的博客!
分享创造价值,您的支持将鼓励我继续前行!