Skip to content

Commit 64a226a

Browse files
committed
add multi-branch workflow example
Signed-off-by: Aidan Oldershaw <aoldershaw@pivotal.io>
1 parent cf1c904 commit 64a226a

File tree

5 files changed

+225
-0
lines changed

5 files changed

+225
-0
lines changed

pipelines/multi-branch/template.yml

+81
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
resource_types:
2+
- name: terraform
3+
type: registry-image
4+
source:
5+
repository: ljfranklin/terraform-resource
6+
7+
- name: gcs
8+
type: registry-image
9+
source:
10+
repository: frodenas/gcs-resource
11+
12+
resources:
13+
- name: branch
14+
type: git
15+
source:
16+
uri: https://github.com/concourse/examples
17+
branch: ((branch))
18+
19+
- name: examples
20+
type: git
21+
source:
22+
uri: https://github.com/concourse/examples
23+
24+
- name: build-artifact
25+
type: gcs
26+
source:
27+
bucket: concourse-examples
28+
json_key: ((concourse_artifacts_json_key))
29+
regexp: multi-branch/features/((feature))/my-app-(.+)\.tgz
30+
31+
- name: staging-env
32+
type: terraform
33+
source:
34+
env_name: ((feature))
35+
backend_type: gcs
36+
backend_config:
37+
bucket: concourse-examples
38+
prefix: multi-branch/terraform
39+
credentials: ((concourse_artifacts_json_key))
40+
41+
jobs:
42+
- name: test
43+
plan:
44+
- in_parallel:
45+
- get: branch
46+
trigger: true
47+
- get: examples
48+
- task: unit
49+
file: examples/tasks/go-test.yml
50+
input_mapping: {repo: branch}
51+
params: {MODULE: apps/golang}
52+
53+
- name: build
54+
plan:
55+
- in_parallel:
56+
- get: branch
57+
passed: [test]
58+
trigger: true
59+
- get: examples
60+
- task: build
61+
file: examples/tasks/go-build.yml
62+
params:
63+
MODULE: apps/golang
64+
BINARY_NAME: my-app
65+
input_mapping: {repo: branch}
66+
- put: build-artifact
67+
params: {file: "binary/my-app-*.tgz"}
68+
69+
- name: deploy
70+
plan:
71+
- in_parallel:
72+
- get: build-artifact
73+
passed: [build]
74+
trigger: true
75+
- get: examples
76+
- load_var: bundle_url
77+
file: build-artifact/url
78+
- put: staging-env
79+
params:
80+
terraform_source: examples/terraform/staging
81+
vars: {bundle_url: ((.:bundle_url))}

pipelines/multi-branch/tracker.yml

+83
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
resource_types:
2+
- name: git-branches
3+
type: registry-image
4+
source:
5+
repository: aoldershaw/git-branches-resource
6+
7+
resources:
8+
- name: feature-branches
9+
type: git-branches
10+
source:
11+
uri: https://github.com/concourse/examples
12+
branch_regex: 'feature/(?P<feature>.*)'
13+
14+
- name: examples
15+
type: git
16+
source:
17+
uri: https://github.com/concourse/examples
18+
19+
jobs:
20+
- name: set-feature-pipelines
21+
plan:
22+
- in_parallel:
23+
- get: feature-branches
24+
trigger: true
25+
- get: examples
26+
- load_var: branches
27+
file: feature-branches/branches.json
28+
- across:
29+
- var: branch
30+
values: ((.:branches))
31+
set_pipeline: dev
32+
file: examples/pipelines/multi-branch/template.yml
33+
instance_vars: {feature: ((.:branch.groups.feature))}
34+
vars: {branch: ((.:branch.name))}
35+
36+
- name: cleanup-inactive-workspaces
37+
plan:
38+
- get: feature-branches
39+
passed: [set-feature-pipelines]
40+
trigger: true
41+
- load_var: active_branches
42+
file: feature-branches/branches.json
43+
- task: cleanup
44+
config:
45+
platform: linux
46+
image_resource:
47+
type: registry-image
48+
source: {repository: hashicorp/terraform}
49+
params:
50+
ACTIVE_BRANCHES: ((.:active_branches))
51+
TERRAFORM_BACKEND_CONFIG:
52+
terraform:
53+
backend:
54+
gcs:
55+
bucket: concourse-examples
56+
prefix: multi-branch/terraform
57+
credentials: ((concourse_artifacts_json_key))
58+
run:
59+
path: sh
60+
args:
61+
- -c
62+
- |
63+
set -euo pipefail
64+
65+
apk add jq
66+
67+
active_features="$(echo "$ACTIVE_BRANCHES" | jq '[.[].groups.feature]')"
68+
69+
echo "$TERRAFORM_BACKEND_CONFIG" > backend.tf.json
70+
terraform init
71+
active_workspaces="$(terraform workspace list | grep -v '^[*]' | tr -d ' ' | jq --raw-input --slurp 'split("\n") | map(select(. != ""))')"
72+
73+
jq -nr "$active_workspaces - $active_features | .[]" | while read extra_workspace
74+
do
75+
echo "deleting workspace $extra_workspace"
76+
terraform workspace select "$extra_workspace"
77+
terraform init
78+
79+
terraform destroy -auto-approve
80+
81+
terraform workspace select default
82+
terraform workspace delete "$extra_workspace"
83+
done

tasks/go-build.yml

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
platform: linux
2+
3+
image_resource:
4+
type: registry-image
5+
source: { repository: golang }
6+
7+
inputs:
8+
- name: repo
9+
path: .
10+
11+
outputs:
12+
- name: binary
13+
14+
params:
15+
MODULE:
16+
BINARY_NAME:
17+
18+
run:
19+
path: sh
20+
args:
21+
- -ce
22+
- |
23+
root=$(pwd)
24+
cd "$MODULE"
25+
go build -o "/tmp/$BINARY_NAME"
26+
27+
timestamp=$(date '+%Y%m%d%H%M%S')
28+
tar czf "$root/binary/$BINARY_NAME-$timestamp.tgz" -C /tmp "$BINARY_NAME"

tasks/go-test.yml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
platform: linux
2+
3+
image_resource:
4+
type: registry-image
5+
source: { repository: golang }
6+
7+
inputs:
8+
- name: repo
9+
path: .
10+
11+
params:
12+
MODULE:
13+
14+
run:
15+
path: sh
16+
args:
17+
- -ce
18+
- |
19+
cd "$MODULE"
20+
go test -v ./...

terraform/staging/main.tf

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
variable "bundle_url" {
2+
type = string
3+
}
4+
5+
resource "null_resource" "fake_deployment" {
6+
triggers = {
7+
bundle_url = var.bundle_url
8+
}
9+
10+
provisioner "local-exec" {
11+
command = "echo \"pretending to deploy from ${var.bundle_url}\"..."
12+
}
13+
}

0 commit comments

Comments
 (0)