Skip to content

Commit eff41ea

Browse files
authored
Merge pull request #53 from ONS-Innovation/concourse
Concourse pipeline
2 parents cf389bb + 9a8db91 commit eff41ea

File tree

7 files changed

+217
-2
lines changed

7 files changed

+217
-2
lines changed

concourse/ci.yml

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
resources:
2+
- name: resource-repo
3+
type: git
4+
icon: github
5+
source:
6+
uri: https://github.com/ONS-Innovation/github-copilot-usage-dashboard
7+
username: ((github_access_token))
8+
password: x-oauth-basic
9+
branch: ((branch))
10+
11+
jobs:
12+
- name: build-and-push
13+
public: true
14+
plan:
15+
- get: resource-repo
16+
timeout: 5m
17+
trigger: true
18+
timeout: 5m
19+
- task: build-image
20+
privileged: true
21+
config:
22+
platform: linux
23+
image_resource:
24+
type: docker-image
25+
source:
26+
repository: hashicorp/terraform
27+
inputs:
28+
- name: resource-repo
29+
params:
30+
aws_account_id: ((aws_account_sdp_((env))))
31+
aws_role_arn: arn:aws:iam::((aws_account_sdp_((env)))):role/sdp-concourse-((env))
32+
secrets: ((sdp_((env))_github_copilot_secrets))
33+
run: # binary used to build the image
34+
path: sh
35+
args:
36+
- -cx
37+
- |
38+
apk add --no-cache aws-cli podman jq iptables curl
39+
40+
if [[ "((env))" == "prod" ]]; then
41+
tag=$(curl "https://api.github.com/repos/ONS-Innovation/github-copilot-usage-dashboard/releases" | jq -r '.[0].tag_name')
42+
export tag
43+
else
44+
export tag=((tag))
45+
fi
46+
git rev-parse --abbrev-ref HEAD
47+
chmod u+x ./resource-repo/concourse/scripts/assume_role.sh
48+
chmod u+x ./resource-repo/concourse/scripts/build_image.sh
49+
source ./resource-repo/concourse/scripts/assume_role.sh
50+
./resource-repo/concourse/scripts/build_image.sh
51+
timeout: 10m
52+
- task: terraform
53+
privileged: true
54+
config:
55+
platform: linux
56+
image_resource:
57+
type: docker-image
58+
source: {repository: hashicorp/terraform}
59+
inputs:
60+
- name: resource-repo
61+
params:
62+
secrets: ((sdp_((env))_github_copilot_secrets))
63+
github_access_token: ((github_access_token))
64+
run:
65+
path: sh
66+
args:
67+
- -cx
68+
- |
69+
apk add --no-cache jq curl
70+
71+
if [[ "((env))" == "prod" ]]; then
72+
tag=$(curl "https://api.github.com/repos/ONS-Innovation/github-copilot-usage-dashboard/releases" | jq -r '.[0].tag_name')
73+
export tag
74+
else
75+
export tag=((tag))
76+
fi
77+
chmod u+x ./resource-repo/concourse/scripts/terraform_infra.sh
78+
export env=((env))
79+
./resource-repo/concourse/scripts/terraform_infra.sh
80+
timeout: 30m

concourse/scripts/assume_role.sh

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
set -euo pipefail
2+
3+
aws sts assume-role --output text \
4+
--role-arn "${aws_role_arn}" \
5+
--role-session-name concourse-pipeline-run \
6+
--query "Credentials.[AccessKeyId,SecretAccessKey,SessionToken]" \
7+
| awk -F '\t' '{print $1 > ("AccessKeyId")}{print $2 > ("SecretAccessKey")}{print $3 > ("SessionToken")}'
8+
9+
10+
export AWS_ACCESS_KEY_ID="$(cat AccessKeyId)"
11+
export AWS_SECRET_ACCESS_KEY="$(cat SecretAccessKey)"
12+
export AWS_SESSION_TOKEN="$(cat SessionToken)"

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 "$secrets" | 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/lambda_data_logger/
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.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
repo_name=${1}
2+
3+
if [[ $# -gt 1 ]]; then
4+
branch=${2}
5+
git rev-parse --verify ${branch}
6+
if [[ $? -ne 0 ]]; then
7+
echo "Branch \"${branch}\" does not exist"
8+
exit 1
9+
fi
10+
else
11+
branch=$(git rev-parse --abbrev-ref HEAD)
12+
fi
13+
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+
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
set -euo pipefail
2+
3+
aws_account_id=$(echo "$secrets" | jq -r .aws_account_id)
4+
aws_access_key_id=$(echo "$secrets" | jq -r .aws_access_key_id)
5+
6+
aws_secret_access_key=$(echo "$secrets" | jq -r .aws_secret_access_key)
7+
aws_secret_name=$(echo "$secrets" | jq -r .aws_secret_name)
8+
9+
env_name=$(echo "$secrets" | jq -r .env_name)
10+
lambda_name=$(echo "$secrets" | jq -r .lambda_name)
11+
12+
github_app_client_id=$(echo "$secrets" | jq -r .github_app_client_id)
13+
lambda_arch=$(echo "$secrets" | jq -r .lambda_arch)
14+
15+
github_org=$(echo "$secrets" | jq -r .github_org)
16+
container_image=$(echo "$secrets" | jq -r .container_image)
17+
18+
schedule=$(echo "$secrets" | jq -r .schedule)
19+
lambda_timeout=$(echo "$secrets" | jq -r .lambda_timeout)
20+
21+
export AWS_ACCESS_KEY_ID=$aws_access_key_id
22+
export AWS_SECRET_ACCESS_KEY=$aws_secret_access_key
23+
24+
git config --global url."https://x-access-token:$github_access_token@github.com/".insteadOf "https://github.com/"
25+
26+
if [[ ${env} != "prod" ]]; then
27+
env="dev"
28+
fi
29+
30+
cd resource-repo/terraform/data_logger/
31+
32+
terraform init -backend-config=env/${env}/backend-${env}.tfbackend -reconfigure
33+
34+
terraform apply \
35+
-var "aws_account_id=$aws_account_id" \
36+
-var "aws_access_key_id=$aws_access_key_id" \
37+
-var "aws_secret_access_key=$aws_secret_access_key" \
38+
-var "aws_secret_name=$aws_secret_name" \
39+
-var "env_name=$env_name" \
40+
-var "lambda_version=${tag}" \
41+
-var "lambda_name=$lambda_name" \
42+
-var "lambda_arch=$lambda_arch" \
43+
-var "lambda_timeout=$lambda_timeout" \
44+
-var "github_app_client_id=$github_app_client_id" \
45+
-var "github_org=$github_org" \
46+
-var "schedule=$schedule" \
47+
-auto-approve

lambda_data_logger/README.md

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -243,4 +243,39 @@ Delete the service resources by running the following ensuring your reference th
243243
terraform refresh -var-file=env/dev/dev.tfvars
244244
245245
terraform destroy -var-file=env/dev/dev.tfvars
246-
```
246+
```
247+
248+
### Deployments with Concourse
249+
250+
#### Allowlisting your IP
251+
To setup the deployment pipeline with concourse, you must first allowlist your IP address on the Concourse
252+
server. IP addresses are flushed everyday at 00:00 so this must be done at the beginning of every working day
253+
whenever the deployment pipeline needs to be used. Follow the instructions on the Confluence page (SDP Homepage > SDP Concourse > Concourse Login) to
254+
login. All our pipelines run on sdp-pipeline-prod, whereas sdp-pipeline-dev is the account used for
255+
changes to Concourse instance itself. Make sure to export all necessary environment variables from sdp-pipeline-prod (AWS_ACCESS_KEY_ID, AWS_SECRET_ACCESS_KEY, AWS_SESSION_TOKEN).
256+
257+
#### Setting up a pipeline
258+
When setting up our pipelines, we use ecs-infra-user on sdp-dev to be able to interact with our infrastructure on AWS. The credentials for this are stored on
259+
AWS Secrets Manager so you do not need to set up anything yourself.
260+
261+
To set the pipeline, run the following script:
262+
```bash
263+
chmod u+x ./concourse/scripts/set_pipeline.sh
264+
./concourse/scripts/set_pipeline.sh github-copilot-usage-lambda
265+
```
266+
Note that you only have to run chmod the first time running the script in order to give permissions.
267+
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.
268+
269+
The pipeline name itself will usually follow a pattern as follows: `<repo-name>-<branch-name>`
270+
If you wish to set a pipeline for another branch without checking out, you can run the following:
271+
```bash
272+
./concourse/scripts/set_pipeline.sh github-copilot-usage-lambda <branch_name>
273+
```
274+
275+
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.
276+
277+
#### Triggering a pipeline
278+
Once the pipeline has been set, you can manually trigger a build on the Concourse UI, or run the following command:
279+
```bash
280+
fly -t aws-sdp trigger-job -j github-copilot-usage-lambda-<branch-name>/build-and-push
281+
```

terraform/data_logger/providers.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ terraform {
22
required_providers {
33
aws = {
44
source = "hashicorp/aws"
5-
version = "~> 5.0"
5+
version = "~> 6.2.0"
66
}
77
}
88
}

0 commit comments

Comments
 (0)