Skip to content

Commit e2a2cdc

Browse files
committed
Read deployed image for drift-detection plan
The Detect Drift workflow runs a terraform plan with no build behind it, but the ECS deployment requires a container image (TF_VAR_TAGGED_IMAGE). The variable has no default and the task definition tracks it directly, so a drift plan either fails with "no value for required variable" or, if given a placeholder, reports permanent false drift on the task definition. Before planning, each environment reads the image that is currently deployed straight from its ECS task definition and passes it to the reusable drift workflow as TF_VAR_TAGGED_IMAGE. The image lookup is done explicitly in this repository rather than in the shared kosli-dev/tf workflow, so Kosli's own drift detection is unaffected. The logic lives in a local reusable workflow, detect-drift-env.yml, which is parameterized by AWS account and environment. detect-drift.yml holds only the schedule and a beta/prod matrix that calls it, so there is no duplication between environments and adding another is a two-line change.
1 parent 7ba8029 commit e2a2cdc

2 files changed

Lines changed: 88 additions & 0 deletions

File tree

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
name: 'Detect Drift (environment)'
2+
3+
# Drift detection for a single environment.
4+
#
5+
# The terraform for this repository lives in deployment/terraform and deploys
6+
# the ECS service with a per-build container image (TF_VAR_TAGGED_IMAGE). The
7+
# variable has no default and the task definition tracks it directly, so a
8+
# drift plan must be given the image that is currently deployed - otherwise
9+
# the plan either fails (no value) or reports false drift (wrong value).
10+
#
11+
# This workflow reads the live image from the deployed ECS task definition and
12+
# passes it to the shared kosli-dev/tf drift workflow. The lookup is kept here,
13+
# in this repository, rather than in the shared workflow, so Kosli's own drift
14+
# detection is unaffected.
15+
16+
on:
17+
workflow_call:
18+
inputs:
19+
aws_account_id:
20+
required: true
21+
type: string
22+
environment:
23+
required: true
24+
type: string
25+
26+
jobs:
27+
image:
28+
runs-on: ubuntu-latest
29+
permissions:
30+
id-token: write
31+
contents: read
32+
outputs:
33+
image: ${{ steps.read.outputs.image }}
34+
steps:
35+
- name: Configure AWS credentials
36+
uses: aws-actions/configure-aws-credentials@ec61189d14ec14c8efccab744f656cffd0e33f37 # v6.1.0
37+
with:
38+
role-to-assume: arn:aws:iam::${{ inputs.aws_account_id }}:role/gh_actions_services
39+
aws-region: eu-central-1
40+
role-session-name: ${{ github.event.repository.name }}
41+
- name: Read deployed image
42+
id: read
43+
run: |
44+
IMAGE=$(aws ecs describe-task-definition --task-definition nginx \
45+
--query 'taskDefinition.containerDefinitions[0].image' --output text)
46+
echo "image=${IMAGE}" >> "$GITHUB_OUTPUT"
47+
48+
detect-drift:
49+
needs: image
50+
permissions:
51+
id-token: write
52+
contents: write
53+
uses: kosli-dev/tf/.github/workflows/detect-drift.yml@main
54+
with:
55+
aws_region: eu-central-1
56+
aws_role_arn: "arn:aws:iam::${{ inputs.aws_account_id }}:role/gh_actions_services"
57+
environment: ${{ inputs.environment }}
58+
working_directory: deployment/terraform/
59+
tf_version: v1.14.9
60+
tf_vars: TF_VAR_TAGGED_IMAGE=${{ needs.image.outputs.image }}

.github/workflows/detect-drift.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
name: 'Detect Drift'
2+
3+
on:
4+
schedule:
5+
- cron: '20 6,8,18 * * *'
6+
workflow_dispatch:
7+
8+
concurrency:
9+
group: ${{ github.workflow }}
10+
11+
jobs:
12+
detect-drift:
13+
strategy:
14+
fail-fast: false
15+
matrix:
16+
include:
17+
- aws_account_id: 244531986313
18+
environment: beta
19+
- aws_account_id: 274425519734
20+
environment: prod
21+
name: ${{ matrix.environment }}
22+
permissions:
23+
id-token: write
24+
contents: write
25+
uses: ./.github/workflows/detect-drift-env.yml
26+
with:
27+
aws_account_id: ${{ matrix.aws_account_id }}
28+
environment: ${{ matrix.environment }}

0 commit comments

Comments
 (0)