Skip to content

Commit c5d46bb

Browse files
authored
Concourse prod (#36)
* add dynamic envs to set_pipeline.bash * add ci_prod and ci_dev * hide secrets * setup releases * check if tag contains HEAD * add cd .. * update file path * remove cd .. * echo tag * test different string * use tag file instead * correct file path * echo tag * version tag in build image file * add x to pipefail * test silly string * update set_pipeline to use hyphen * generalise bash scripts between dev and prod * add podman to assume_role * verify caller identiy * check caller identity on build image * generalise terraform_infra.bash between dev and prod * merge prod and dev into one file * re * fish conditional * Revert "fish conditional" This reverts commit bfb5c13. * fix conditional on concourse-prod * change to .sh * update README
1 parent 0e34a88 commit c5d46bb

File tree

9 files changed

+363
-316
lines changed

9 files changed

+363
-316
lines changed

README.md

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -244,18 +244,20 @@ AWS Secrets Manager so you do not need to set up anything yourself.
244244

245245
To set the pipeline, run the following script:
246246
```bash
247-
chmod u+x ./concourse/scripts/set_pipeline.bash
248-
./concourse/scripts/set_pipeline.bash KEH-TAT-UI
247+
chmod u+x ./concourse/scripts/set_pipeline.sh
248+
./concourse/scripts/set_pipeline.sh KEH-TAT-UI
249249
```
250250
Note that you only have to run chmod the first time running the script in order to give permissions.
251251
This script will set the branch and pipeline name to whatever branch you are currently on. It will also set the image tag on ECR to the current commit hash at the time of setting the pipeline.
252252

253253
The pipeline name itself will usually follow a pattern as follows: `<repo-name>-<branch-name>`
254254
If you wish to set a pipeline for another branch without checking out, you can run the following:
255255
```bash
256-
./concourse/scripts/set_pipeline.bash KEH-TAT-UI <branch_name>
256+
./concourse/scripts/set_pipeline.sh KEH-TAT-UI <branch_name>
257257
```
258258

259+
If the branch you are deploying is "main" or "master", it will trigger a deployment to the sdp-prod environment. To set the ECR image tag, you must draft a Github release pointing to the latest release of the main/master branch that has a tag in the form of vX.Y.Z. Drafting up a release will automatically deploy the latest version of the main/master branch with the associated release tag, but you can also manually trigger a build through the Concourse UI or the terminal prompt.
260+
259261
#### Triggering a pipeline
260262
Once the pipeline has been set, you can manually trigger a build on the Concourse UI, or run the following command:
261263
```bash

concourse/ci.yml

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,24 @@ resources:
33
type: git
44
icon: github
55
source:
6-
uri: https://github.com/ONS-Innovation/keh-tech-audit-tool.git
6+
uri: https://github.com/ONS-Innovation/keh-tech-audit-tool
77
branch: ((branch))
88

9+
- name: release
10+
type: github-release
11+
icon: github
12+
source:
13+
owner: ONS-Innovation
14+
repository: keh-tech-audit-tool
15+
916
jobs:
1017
- name: build-and-push
1118
public: true
1219
plan:
1320
- get: resource-repo
14-
trigger: false
21+
timeout: 5m
22+
- get: release
23+
trigger: true
1524
timeout: 5m
1625
- task: build-image
1726
privileged: true
@@ -23,21 +32,27 @@ jobs:
2332
repository: hashicorp/terraform
2433
inputs:
2534
- name: resource-repo
35+
- name: release
2636
params:
27-
aws_account_id: ((aws_account_sdp_dev))
28-
aws_role_arn: arn:aws:iam::((aws_account_sdp_dev)):role/sdp-concourse-dev
29-
tag: ((tag))
37+
aws_account_id: ((aws_account_sdp_((env))))
38+
aws_role_arn: arn:aws:iam::((aws_account_sdp_((env)))):role/sdp-concourse-((env))
39+
tat_secrets_ui: ((sdp_((env))_secrets_tat_ui))
3040
run: # binary used to build the image
3141
path: sh
3242
args:
3343
- -cx
3444
- |
35-
chmod u+x ./resource-repo/concourse/scripts/assume_role.bash
36-
chmod u+x ./resource-repo/concourse/scripts/build_image.bash
37-
source ./resource-repo/concourse/scripts/assume_role.bash
38-
./resource-repo/concourse/scripts/build_image.bash
45+
if [[ "((env))" == "prod" ]]; then
46+
export tag=v$(cat release/version)
47+
else
48+
export tag=((tag))
49+
fi
50+
git rev-parse --abbrev-ref HEAD
51+
chmod u+x ./resource-repo/concourse/scripts/assume_role.sh
52+
chmod u+x ./resource-repo/concourse/scripts/build_image.sh
53+
source ./resource-repo/concourse/scripts/assume_role.sh
54+
./resource-repo/concourse/scripts/build_image.sh
3955
timeout: 10m
40-
4156
- task: terraform
4257
privileged: true
4358
config:
@@ -47,12 +62,22 @@ jobs:
4762
source: {repository: hashicorp/terraform}
4863
inputs:
4964
- name: resource-repo
65+
- name: release
5066
params:
51-
tat_secrets_ui: ((sdp_dev_secrets_tat_ui))
67+
tat_secrets_ui: ((sdp_((env))_secrets_tat_ui))
5268
github_access_token: ((github_access_token))
53-
tag: ((tag))
5469
run:
5570
path: sh
56-
args: ["./resource-repo/concourse/scripts/terraform_infra.bash"]
71+
args:
72+
- -cx
73+
- |
74+
if [[ "((env))" == "prod" ]]; then
75+
export tag=v$(cat release/version)
76+
else
77+
export tag=((tag))
78+
fi
79+
chmod u+x ./resource-repo/concourse/scripts/terraform_infra.sh
80+
export env=((env))
81+
./resource-repo/concourse/scripts/terraform_infra.sh
5782
timeout: 30m
58-
83+
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
11
set -euo pipefail
22

3-
apk add --no-cache aws-cli podman
3+
apk add --no-cache aws-cli podman jq
44

55
aws sts assume-role --output text \
66
--role-arn "${aws_role_arn}" \
77
--role-session-name concourse-pipeline-run \
88
--query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \
99
| awk -F '\t' '{print $1 > ("AccessKeyId")}{print $2 > ("SecretAccessKey")}{print $3 > ("SessionToken")}'
1010

11+
1112
export AWS_ACCESS_KEY_ID="$(cat AccessKeyId)"
1213
export AWS_SECRET_ACCESS_KEY="$(cat SecretAccessKey)"
1314
export AWS_SESSION_TOKEN="$(cat SessionToken)"

concourse/scripts/build_image.bash

Lines changed: 0 additions & 12 deletions
This file was deleted.

concourse/scripts/build_image.sh

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
set -euo pipefail
2+
3+
export STORAGE_DRIVER=vfs
4+
export PODMAN_SYSTEMD_UNIT=concourse-task
5+
6+
container_image=$(echo "$tat_secrets_ui" | jq -r .container_image)
7+
8+
aws ecr get-login-password --region eu-west-2 | podman --storage-driver=vfs login --username AWS --password-stdin ${aws_account_id}.dkr.ecr.eu-west-2.amazonaws.com
9+
10+
podman build -t ${container_image}:${tag} resource-repo
11+
12+
podman tag ${container_image}:${tag} ${aws_account_id}.dkr.ecr.eu-west-2.amazonaws.com/${container_image}:${tag}
13+
14+
podman push ${aws_account_id}.dkr.ecr.eu-west-2.amazonaws.com/${container_image}:${tag}

concourse/scripts/set_pipeline.bash renamed to concourse/scripts/set_pipeline.sh

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
tag=$(git rev-parse HEAD)
21
repo_name=${1}
32

43
if [[ $# -gt 1 ]]; then
@@ -12,4 +11,17 @@ else
1211
branch=$(git rev-parse --abbrev-ref HEAD)
1312
fi
1413

15-
fly -t aws-sdp set-pipeline -c concourse/ci.yml -p ${repo_name}-${branch} -v branch=${branch} -v tag=${tag}
14+
if [[ ${branch} == "main" || ${branch} == "master" ]]; then
15+
env="prod"
16+
else
17+
env="dev"
18+
fi
19+
20+
if [[ ${env} == "dev" ]]; then
21+
tag=$(git rev-parse HEAD)
22+
else
23+
tag=$(git tag | tail -n 1)
24+
fi
25+
26+
fly -t aws-sdp set-pipeline -c concourse/ci.yml -p ${repo_name}-${branch} -v branch=${branch} -v tag=${tag} -v env=${env}
27+

concourse/scripts/terraform_infra.bash renamed to concourse/scripts/terraform_infra.sh

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ export AWS_SECRET_ACCESS_KEY=$aws_secret_access_key
2424

2525
git config --global url."https://x-access-token:$github_access_token@github.com/".insteadOf "https://github.com/"
2626

27+
if [[ ${env} != "prod" ]]; then
28+
env="dev"
29+
fi
30+
2731
cd resource-repo/terraform/service
28-
terraform init -backend-config=env/dev/backend-dev.tfbackend -reconfigure
32+
terraform init -backend-config=env/${env}/backend-${env}.tfbackend -reconfigure
2933
terraform apply \
3034
-var "aws_account_id=$aws_account_id" \
3135
-var "aws_access_key_id=$aws_access_key_id" \

0 commit comments

Comments
 (0)