-
Notifications
You must be signed in to change notification settings - Fork 1
Serve app assets from S3 #2192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Serve app assets from S3 #2192
Changes from all commits
1c6c72c
6f92163
1ab7dac
f64c4f8
72e2cff
f176dce
d9d077a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| version: 0.2 | ||
| phases: | ||
| pre_build: | ||
| commands: | ||
| # Check Image URI is not the default pipeline value | ||
| - | | ||
| if [[ "${IMAGE_URI}" = "MUST_BE_SET" ]]; then | ||
| echo "The IMAGE_URI has not been set by the caller. The value of IMAGE_URI is \"${IMAGE_URI}\"" | ||
| exit 1 | ||
| fi | ||
|
|
||
| - echo "Log in to Amazon ECR repository ${CONTAINER_REGISTRY}" | ||
| - aws ecr get-login-password --region eu-west-2 | docker login --username AWS --password-stdin "${CONTAINER_REGISTRY}" | ||
| build: | ||
| commands: | ||
| # Copy the built assets out of the container image | ||
| - docker pull "${IMAGE_URI}" | ||
| - CONTAINER_ID=$(docker create "${IMAGE_URI}") | ||
| - docker cp "${CONTAINER_ID}:/app/public/assets" ./assets | ||
| - docker rm "${CONTAINER_ID}" | ||
|
|
||
| # Upload the assets so they are available before the new tasks | ||
| # serve any pages which reference them. Assets from previous | ||
| # releases are deliberately left in place so tasks which have not | ||
| # yet been replaced keep working during the deployment. | ||
| - | | ||
| aws s3 sync ./assets "s3://${ASSETS_BUCKET}/assets/" \ | ||
| --exclude ".vite/*" \ | ||
| --cache-control "public, max-age=300" \ | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. as these assets are fingerprinted to versions, we should set a much longer TTL (eg. This means we could dramatically reduce origin fetches, and staleness wouldn't be an issue. |
||
| --no-progress | ||
|
theseanything marked this conversation as resolved.
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| # A single project is shared by the app deploy pipelines. All of the | ||
| # apps store their assets in the same bucket: every file is fingerprinted | ||
| # with a content hash, so names cannot collide. | ||
| module "sync_assets" { | ||
| source = "../../../modules/code-build-build" | ||
| project_name = "sync_assets_${var.environment_name}" | ||
| project_description = "Sync assets from a container image to the assets bucket" | ||
| environment = var.environment_name | ||
| artifact_store_arn = module.artifact_bucket.arn | ||
| buildspec = file("${path.root}/buildspecs/sync-assets/sync-assets.yml") | ||
| log_group_name = "codebuild/sync_assets_${var.environment_name}" | ||
| codebuild_service_role_arn = data.aws_iam_role.deployer_role.arn | ||
|
|
||
| environment_variables = { | ||
| # try() so that the first apply succeeds before the environment state | ||
| # has published the assets_bucket_name output | ||
| ASSETS_BUCKET = try(data.terraform_remote_state.forms_environment.outputs.assets_bucket_name, "govuk-forms-${var.environment_name}-assets") | ||
| CONTAINER_REGISTRY = var.container_registry | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this will accumulate releases forever.
secure-bucketalso has versioning_enabled set to true by default, so we're going to have to set up a lifecycle rule on this eventually. Has a followup card already been written for this?