1+ resource_types :
2+ - name : key-value
3+ type : registry-image
4+ source :
5+ repository : gstack/keyval-resource
6+
17resources :
28- name : resource-repo
39 type : git
@@ -6,14 +12,112 @@ resources:
612 uri : https://github.com/ONS-Innovation/keh-tech-audit-tool-api
713 branch : ((branch))
814 access_token : ((github_access_token))
15+ - name : github-release
16+ type : github-release
17+ source :
18+ owner : ONS-Innovation
19+ repository : keh-tech-audit-tool-api
20+ access_token : ((github_access_token))
21+ - name : github-release-tag
22+ type : key-value
23+ source :
24+ file : tag-output/tag
25+
26+ # Define the common terraform task as an anchor
27+ terraform-task : &terraform-task
28+ task : terraform-deploy
29+ privileged : true
30+ config :
31+ platform : linux
32+ image_resource :
33+ type : docker-image
34+ source :
35+ repository : hashicorp/terraform
36+ inputs :
37+ - name : resource-repo
38+ - name : github-release-tag
39+ params :
40+ secrets : ((sdp_((env))_secrets_tat_api))
41+ github_access_token : ((github_access_token))
42+ env : ((env))
43+ branch : ((branch))
44+ run :
45+ path : sh
46+ args :
47+ - -cx
48+ - |
49+ echo "DEBUG: Environment is ${env}"
50+ echo "DEBUG: Tag is ${tag}"
51+ export tag=$(cat github-release-tag/tag)
52+ if [[ "$env" == "prod" ]] && [[ "$branch" != "main" && "$branch" != "master" ]]; then
53+ echo "Not on main branch, skipping terraform for prod."
54+ exit 0
55+ fi
56+ apk add --no-cache jq curl
57+ if [[ "$env" == "prod" && ! "$tag" =~ ^v[0-9]+\.[0-9]+\.[0-9]+$ ]]; then
58+ echo "ERROR: Tag '$tag' is not in semantic versioning format (vX.Y.Z)"
59+ exit 1
60+ fi
61+ chmod u+x ./resource-repo/concourse/scripts/terraform_infra.sh
62+ ./resource-repo/concourse/scripts/terraform_infra.sh
63+ timeout : 30m
964
1065jobs :
11- - name : build-and-push
66+ - name : calculate-tag
1267 public : true
1368 plan :
69+ - get : github-release
70+ trigger : true
71+ - get : resource-repo
72+ trigger : false
73+ - task : calculate-tag
74+ config :
75+ platform : linux
76+ image_resource :
77+ type : docker-image
78+ source :
79+ repository : alpine
80+ inputs :
81+ - name : resource-repo
82+ - name : github-release
83+ outputs :
84+ - name : tag-output
85+ params :
86+ branch : ((branch))
87+ run :
88+ path : sh
89+ args :
90+ - -cx
91+ - |
92+ apk add --no-cache git
93+ echo "Calculating tag for branch: ${branch}"
94+ if [[ "$branch" == "main" || "$branch" == "master" ]]; then
95+ # Get the latest tag that matches the format vX.Y.Z
96+ tag=$(git -C resource-repo tag --list 'v[0-9]*.[0-9]*.[0-9]*' | sort -V | tail -n 1)
97+ if [[ -z "$tag" ]]; then
98+ echo "No valid semantic versioning tags (vX.Y.Z) found. Cannot set pipeline."
99+ exit 1
100+ fi
101+ else
102+ # Remove non-alphanumeric characters and take the first 7 characters
103+ tag=$(echo "${branch}" | tr -cd '[:alnum:]' | cut -c1-7)
104+ fi
105+ echo "Calculated tag: ${tag}"
106+ # Write the tag to a file for output
107+ echo "${tag}" > tag-output/tag
108+ - put : github-release-tag
109+ params :
110+ directory : tag-output
111+
112+ - name : deploy-after-github-release-dev
113+ public : true
114+ plan :
115+ - get : github-release-tag
116+ passed : [calculate-tag]
117+ trigger : true
14118 - get : resource-repo
15119 timeout : 5m
16- - task : build-image
120+ - task : deploy-release
17121 privileged : true
18122 config :
19123 platform : linux
@@ -22,56 +126,85 @@ jobs:
22126 source :
23127 repository : hashicorp/terraform
24128 inputs :
25- - name : resource-repo
129+ - name : resource-repo
130+ - name : github-release-tag
26131 params :
27132 aws_account_id : ((aws_account_sdp_((env))))
28133 aws_role_arn : arn:aws:iam::((aws_account_sdp_((env)))):role/sdp-concourse-((env))
29134 secrets : ((sdp_((env))_secrets_tat_api))
135+ env : dev
136+ tag : github-release-tag/tag # Use the release tag from the resource
137+ repo_name : ((repo_name))
30138 run : # binary used to build the image
31139 path : sh
32140 args :
33141 - -cx
34142 - |
35143 apk add --no-cache aws-cli podman jq iptables curl
36-
37- if [[ "((env))" == "prod" ]]; then
38- tag=$(curl "https://api.github.com/repos/ONS-Innovation/keh-tech-audit-tool-api/releases" | jq -r '.[0].tag_name')
39- export tag
40- else
41- export tag=((tag))
42- fi
43- git rev-parse --abbrev-ref HEAD
144+ export repo_name=((repo_name))
145+ export tag=$(cat github-release-tag/tag)
146+ # Write the tag to a file for output
147+ echo "${tag}" > release-tag-output/tag
148+ echo "Using tag: ${tag}"
44149 chmod u+x ./resource-repo/concourse/scripts/assume_role.sh
45150 chmod u+x ./resource-repo/concourse/scripts/build_image.sh
46151 source ./resource-repo/concourse/scripts/assume_role.sh
47152 ./resource-repo/concourse/scripts/build_image.sh
48153 timeout : 10m
49- - task : terraform
154+ - << : *terraform-task
155+ params :
156+ secrets : ((sdp_((env))_secrets_tat_api))
157+ github_access_token : ((github_access_token))
158+ env : dev
159+ - name : release-build-and-push-prod
160+ public : true
161+ plan :
162+ - get : resource-repo
163+ passed : [deploy-after-github-release-dev]
164+ trigger : false # Manual trigger only
165+ timeout : 5m
166+ - get : github-release-tag
167+ passed : [deploy-after-github-release-dev]
168+ - task : build-image
50169 privileged : true
51170 config :
52171 platform : linux
53172 image_resource :
54173 type : docker-image
55- source : {repository: hashicorp/terraform}
174+ source :
175+ repository : hashicorp/terraform
56176 inputs :
57- - name : resource-repo
177+ - name : resource-repo
178+ - name : github-release-tag
58179 params :
59- secrets : ((sdp_((env))_secrets_tat_api))
60- github_access_token : ((github_access_token))
180+ aws_account_id : ((aws_account_sdp_prod))
181+ aws_role_arn : arn:aws:iam::((aws_account_sdp_prod)):role/sdp-concourse-prod
182+ secrets : ((sdp_prod_secrets_tat_api))
183+ env : prod
184+ tag : github-release-tag/tag
185+ repo_name : ((repo_name))
186+ branch : ((branch))
61187 run :
62188 path : sh
63189 args :
64- - -cx
65- - |
66- apk add --no-cache jq curl
67- if [[ "((env))" == "prod" ]]; then
68- tag=$(curl "https://api.github.com/repos/ONS-Innovation/keh-tech-audit-tool-api/releases" | jq -r '.[0].tag_name')
69- export tag
70- else
71- export tag=((tag))
72- fi
73- chmod u+x ./resource-repo/concourse/scripts/terraform_infra.sh
74- export env=((env))
75- ./resource-repo/concourse/scripts/terraform_infra.sh
76- timeout : 30m
77-
190+ - -cx
191+ - |
192+ if [[ "$branch" != "main" ]]; then
193+ echo "Not on main branch, skipping build."
194+ exit 0
195+ fi
196+ apk add --no-cache aws-cli podman jq iptables curl
197+ export repo_name=((repo_name))
198+ export tag=$(cat github-release-tag/tag)
199+ echo "Using release tag: ${tag}"
200+ chmod u+x ./resource-repo/concourse/scripts/assume_role.sh
201+ chmod u+x ./resource-repo/concourse/scripts/build_image.sh
202+ source ./resource-repo/concourse/scripts/assume_role.sh
203+ ./resource-repo/concourse/scripts/build_image.sh
204+ timeout : 10m
205+ - << : *terraform-task
206+ params :
207+ github_access_token : ((github_access_token))
208+ env : prod
209+ secrets : ((sdp_prod_secrets_tat_api))
210+
0 commit comments