|
| 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 |
0 commit comments