Skip to content

Commit 94d47b6

Browse files
ben-swilliamsBen Williams
andauthored
feat: dockerise (#1)
* Dockerised server * Circle CI * Triggger CI --------- Co-authored-by: Ben Williams <ben.w@automata.tech>
1 parent 60b6869 commit 94d47b6

File tree

8 files changed

+396
-1
lines changed

8 files changed

+396
-1
lines changed

.circleci/config.yml

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
version: 2.1
2+
setup: true
3+
orbs:
4+
continuation: circleci/continuation@1
5+
jobs:
6+
generate-config:
7+
executor: continuation/default
8+
steps:
9+
- checkout
10+
- run:
11+
name: Generate Pipeline generated_config.yml file
12+
command: |
13+
sudo apt-get update
14+
sudo apt-get install -y gettext-base
15+
.circleci/scripts/generate-pipeline-config
16+
- continuation/continue:
17+
parameters: "{}"
18+
configuration_path: .circleci/configs/generated_config.yml
19+
workflows:
20+
setup-workflow:
21+
jobs:
22+
- generate-config:
23+
filters:
24+
branches:
25+
only: /.*/
26+
tags:
27+
only: /^v[0-9]+\.[0-9]+\.[0-9]+$/
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
{
2+
"rules": [
3+
{
4+
"rulePriority": 1,
5+
"description": "Expire untagged images older than 1 day",
6+
"selection": {
7+
"tagStatus": "untagged",
8+
"countType": "sinceImagePushed",
9+
"countUnit": "days",
10+
"countNumber": 1
11+
},
12+
"action": { "type": "expire" }
13+
},
14+
{
15+
"rulePriority": 2,
16+
"description": "Keep last 100 images tagged with main or master",
17+
"selection": {
18+
"tagStatus": "tagged",
19+
"tagPrefixList": ["main", "master"],
20+
"countType": "imageCountMoreThan",
21+
"countNumber": 100
22+
},
23+
"action": { "type": "expire" }
24+
},
25+
{
26+
"rulePriority": 3,
27+
"description": "Expire feature branch images after 90 days",
28+
"selection": {
29+
"tagStatus": "tagged",
30+
"countType": "sinceImagePushed",
31+
"countUnit": "days",
32+
"countNumber": 90,
33+
"tagPrefixList": ["feature-", "fix-", "pr-"]
34+
},
35+
"action": { "type": "expire" }
36+
}
37+
]
38+
}

.circleci/ecr-repo-policy.json

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
{
2+
"Version": "2012-10-17",
3+
"Statement": [
4+
{
5+
"Sid": "AllowImagePull",
6+
"Effect": "Allow",
7+
"Principal": {
8+
"AWS": ["*"]
9+
},
10+
"Action": [
11+
"ecr:BatchCheckLayerAvailability",
12+
"ecr:BatchGetImage",
13+
"ecr:DescribeImages",
14+
"ecr:DescribeRepositories",
15+
"ecr:GetDownloadUrlForLayer"
16+
],
17+
"Condition": {
18+
"StringEquals": {
19+
"aws:PrincipalOrgID": "o-8ol2awd4x4"
20+
}
21+
}
22+
}
23+
]
24+
}
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#!/bin/env bash
2+
3+
set -euo pipefail
4+
5+
PYTHON_VERSION=$(cat .python-version) #Python Version to install
6+
POETRY_VERSION="$(cat .tool-versions | grep poetry | cut -d ' ' -f 2)"
7+
VERSION=${CIRCLE_TAG:-${CIRCLE_SHA1:0:7}}
8+
9+
# Use \$ to escape the variables to be replaced when the pipeline is running
10+
# eg: ${VERSION} -> \${VERSION}
11+
12+
mkdir -p .circleci/configs/
13+
CI_TEMPLATE_PYTHON_VERSION=${PYTHON_VERSION} \
14+
CI_TEMPLATE_POETRY_VERSION=${POETRY_VERSION} \
15+
CI_TEMPLATE_VERSION=${VERSION} \
16+
envsubst '$CI_TEMPLATE_PYTHON_VERSION $CI_TEMPLATE_POETRY_VERSION $CI_TEMPLATE_VERSION' < .circleci/scripts/template.yml > .circleci/configs/generated_config.yml

.circleci/scripts/template.yml

Lines changed: 262 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,262 @@
1+
version: 2.1
2+
3+
orbs:
4+
semantic-release: automata-tech/semantic-release@1
5+
atmta-python: automata-tech/python@1
6+
python: circleci/python@2
7+
aws-ecr: circleci/aws-ecr@9
8+
aws-cli: circleci/aws-cli@5
9+
sonarcloud: sonarsource/sonarcloud@3.0.0
10+
11+
executors:
12+
python:
13+
docker:
14+
- image: cimg/python:3.12.2
15+
16+
alias:
17+
pr-only-filters: &pr-only-filters
18+
branches:
19+
ignore: main
20+
main-only-filters: &main-only-filters
21+
branches:
22+
only: main
23+
semver-tags-only-filters: &semver-tags-only-filters
24+
branches:
25+
ignore: /.*/
26+
tags:
27+
only: /^v[0-9]+\.[0-9]+\.[0-9]+$/
28+
29+
workflows:
30+
main:
31+
jobs:
32+
- build_arm64_container:
33+
name: build-arm64-docker-image-on-PR
34+
filters: *pr-only-filters
35+
context: ["aws-oidc-roles", "github-automata"]
36+
dockerfile: docker/Dockerfile
37+
version: ${CIRCLE_SHA1:0:7}-arm64
38+
# requires:
39+
# - tests
40+
- build_amd64_container:
41+
name: build-amd64-docker-image-on-PR
42+
filters: *pr-only-filters
43+
context: ["aws-oidc-roles", "github-automata"]
44+
dockerfile: docker/Dockerfile
45+
version: ${CIRCLE_SHA1:0:7}-amd64
46+
# requires:
47+
# - tests
48+
- combine_docker_containers_and_push:
49+
name: combine-arm-and-amd-images-on-PR
50+
filters: *pr-only-filters
51+
context: ["aws-oidc-roles", "github-automata"]
52+
output_tag: ${CIRCLE_SHA1:0:7}
53+
tags_to_combine: ${CIRCLE_SHA1:0:7}-amd64 ${CIRCLE_SHA1:0:7}-arm64
54+
requires:
55+
- build-arm64-docker-image-on-PR
56+
- build-amd64-docker-image-on-PR
57+
58+
####################
59+
# main branch #
60+
####################
61+
- semantic-release/release:
62+
context:
63+
- semantic-release
64+
- Slack
65+
filters: *main-only-filters
66+
67+
####################
68+
# semver tags #
69+
####################
70+
71+
semver-release:
72+
when:
73+
and:
74+
- matches:
75+
pattern: /^v\d+\.\d+\.\d+$/
76+
value: << pipeline.git.tag >>
77+
jobs:
78+
- build_arm64_container:
79+
name: build-arm64-docker-image-SEMVER
80+
filters: *semver-tags-only-filters
81+
context: ["aws-oidc-roles", "github-automata"]
82+
dockerfile: docker/Dockerfile
83+
version: << pipeline.git.tag >>-arm64
84+
85+
- build_amd64_container:
86+
name: build-amd64-docker-image-SEMVER
87+
filters: *semver-tags-only-filters
88+
context: ["aws-oidc-roles", "github-automata"]
89+
dockerfile: docker/Dockerfile
90+
version: << pipeline.git.tag >>-amd64
91+
92+
- combine_docker_containers_and_push:
93+
name: combine-arm-and-amd-images-SEMVER
94+
filters: *semver-tags-only-filters
95+
context: ["aws-oidc-roles", "github-automata"]
96+
output_tag: << pipeline.git.tag >>
97+
tags_to_combine: << pipeline.git.tag >>-amd64 << pipeline.git.tag >>-arm64
98+
requires:
99+
- build-amd64-docker-image-SEMVER
100+
- build-arm64-docker-image-SEMVER
101+
102+
jobs:
103+
combine_docker_containers_and_push:
104+
environment:
105+
AWS_DEFAULT_REGION: eu-west-1
106+
working_directory: ~/project
107+
docker:
108+
- image: cimg/base:2024.02
109+
parameters:
110+
output_tag:
111+
type: string
112+
tags_to_combine:
113+
type: string
114+
steps:
115+
- setup_remote_docker
116+
- checkout
117+
- aws-cli/setup:
118+
role_arn: ${AWS_OIDC_ROLE_ARN_ATMTA028}
119+
region: ${AWS_DEFAULT_REGION}
120+
- run:
121+
name: set AWS_ACCOUNT_ID env variable
122+
command: |
123+
echo "export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)" >> $BASH_ENV
124+
- aws-ecr/ecr_login:
125+
region: ${AWS_DEFAULT_REGION}
126+
account_id: $AWS_ACCOUNT_ID
127+
- run:
128+
name: combine images
129+
command: |
130+
#!/usr/bin/env bash
131+
set -ex
132+
133+
repo="${AWS_ACCOUNT_ID}.dkr.ecr.${AWS_DEFAULT_REGION}.amazonaws.com/${CIRCLE_PROJECT_REPONAME}"
134+
output_tag="${repo}:<<parameters.output_tag>>"
135+
136+
resolved_tags=""
137+
IFS=' ' read -ra tags \<<< "<<parameters.tags_to_combine>>"
138+
for tag in "${tags[@]}"; do
139+
new_resolved_tags="${resolved_tags}${repo}:${tag} "
140+
resolved_tags="${new_resolved_tags}"
141+
done
142+
143+
docker manifest create $output_tag $resolved_tags
144+
docker manifest push $output_tag
145+
146+
build_arm64_container:
147+
environment:
148+
AWS_DEFAULT_REGION: eu-west-1
149+
working_directory: ~/project
150+
machine:
151+
image: ubuntu-2204:2023.07.1
152+
resource_class: arm.medium
153+
parameters:
154+
version:
155+
type: string
156+
dockerfile:
157+
type: string
158+
steps:
159+
- checkout
160+
- atmta-python/login_bash
161+
- run:
162+
name: Export poetry password secret
163+
command: |
164+
echo "export POETRY_HTTP_BASIC_AUTOMATA_PASSWORD=$CODEARTIFACT_AUTH_TOKEN" >> $BASH_ENV
165+
- run:
166+
name: set poetry token to tmpfile
167+
command: |
168+
echo $CODEARTIFACT_AUTH_TOKEN > /tmp/docker-secrets-codeartifact_auth_token
169+
- run:
170+
name: set AWS_ACCOUNT_ID env variable
171+
command: |
172+
echo "export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)" >> $BASH_ENV
173+
- aws-ecr/build_and_push_image:
174+
account_id: $AWS_ACCOUNT_ID
175+
dockerfile: <<parameters.dockerfile>>
176+
auth:
177+
- aws-cli/setup:
178+
role_arn: ${AWS_OIDC_ROLE_ARN_ATMTA028}
179+
role_session_name: ${CIRCLE_PROJECT_REPONAME}
180+
region: ${AWS_DEFAULT_REGION}
181+
repo: ${CIRCLE_PROJECT_REPONAME}
182+
tag: <<parameters.version>>
183+
platform: linux/arm64
184+
extra_build_args: >-
185+
--provenance=false
186+
--sbom=false
187+
--build-arg PYTHON_VERSION=${CI_TEMPLATE_PYTHON_VERSION}
188+
--build-arg POETRY_VERSION=${CI_TEMPLATE_POETRY_VERSION}
189+
--build-arg VERSION=${CI_TEMPLATE_VERSION}
190+
--secret id=POETRY_HTTP_BASIC_AUTOMATA_PASSWORD
191+
--target=final
192+
create_repo: true
193+
lifecycle_policy_path: .circleci/ecr-lifecycle-policy.json
194+
repo_policy_path: .circleci/ecr-repo-policy.json
195+
set_repo_policy: true
196+
repo_scan_on_push: true
197+
skip_when_tags_exist: true
198+
- run:
199+
name: remove buildx builder
200+
command: |
201+
# There is a bug in circlci where it attempts to create the same builder multiple times.
202+
# This is a workaround to remove the builder before creating it again.
203+
docker buildx rm DLC_builder || true
204+
205+
build_amd64_container:
206+
environment:
207+
AWS_DEFAULT_REGION: eu-west-1
208+
working_directory: ~/project
209+
docker:
210+
- image: cimg/python:3.10.14
211+
parameters:
212+
version:
213+
type: string
214+
dockerfile:
215+
type: string
216+
steps:
217+
- setup_remote_docker
218+
- checkout
219+
- atmta-python/login_bash
220+
- run:
221+
name: Export poetry password secret
222+
command: |
223+
echo "export POETRY_HTTP_BASIC_AUTOMATA_PASSWORD=$CODEARTIFACT_AUTH_TOKEN" >> $BASH_ENV
224+
- run:
225+
name: set poetry token to tmpfile
226+
command: |
227+
echo $CODEARTIFACT_AUTH_TOKEN > /tmp/docker-secrets-codeartifact_auth_token
228+
- run:
229+
name: set AWS_ACCOUNT_ID env variable
230+
command: |
231+
echo "export AWS_ACCOUNT_ID=$(aws sts get-caller-identity --query Account --output text)" >> $BASH_ENV
232+
- aws-ecr/build_and_push_image:
233+
account_id: $AWS_ACCOUNT_ID
234+
dockerfile: <<parameters.dockerfile>>
235+
auth:
236+
- aws-cli/setup:
237+
role_arn: ${AWS_OIDC_ROLE_ARN_ATMTA028}
238+
role_session_name: ${CIRCLE_PROJECT_REPONAME}
239+
region: ${AWS_DEFAULT_REGION}
240+
repo: ${CIRCLE_PROJECT_REPONAME}
241+
tag: <<parameters.version>>
242+
platform: linux/amd64
243+
extra_build_args: >-
244+
--provenance=false
245+
--sbom=false
246+
--build-arg PYTHON_VERSION=${CI_TEMPLATE_PYTHON_VERSION}
247+
--build-arg POETRY_VERSION=${CI_TEMPLATE_POETRY_VERSION}
248+
--build-arg VERSION=${CI_TEMPLATE_VERSION}
249+
--secret id=POETRY_HTTP_BASIC_AUTOMATA_PASSWORD
250+
--target=final
251+
create_repo: true
252+
lifecycle_policy_path: .circleci/ecr-lifecycle-policy.json
253+
repo_policy_path: .circleci/ecr-repo-policy.json
254+
set_repo_policy: true
255+
repo_scan_on_push: true
256+
skip_when_tags_exist: true
257+
- run:
258+
name: remove buildx builder
259+
command: |
260+
# There is a bug in circlci where it attempts to create the same builder multiple times.
261+
# This is a workaround to remove the builder before creating it again.
262+
docker buildx rm DLC_builder || true

Cargo.toml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,10 @@ edition = "2021"
66
license = "GPL-3.0-or-later"
77
repository = "https://github.com/euanwm/pflex-module-rs"
88

9+
[[bin]]
10+
name = "mock-server"
11+
path = "src/bin/mock_server.rs"
12+
913
[dependencies]
1014
log = "0.4.22"
1115
strum = "0.26.3"

0 commit comments

Comments
 (0)