Goal
实现Jenkins + Terraform在AWS上创建基本环境。
Prerequisites
- Terraform,please refer: Terraform知识
Terraform Config
创建main.tf
,并push到Github上。1
2
3
4
5
6
7
8
9
10
11provider "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
24node {
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