Objective
Two repositories exist: repo-a (application source) and repo-b (Kubernetes configuration with values.yaml). When code is pushed to repo-a, the pipeline must build a Docker image tagged with the short Git SHA and update repo-b to use the new tag.
Task
Complete the workflow at /home/interview/repo-a/.github/workflows/promote.yml that triggers on push to main. The repo-b update step is already provided. Add two missing steps: compute the short SHA from $GITHUB_SHA using cut -c1-7 and store it in $GITHUB_ENV as SHORT_SHA, then build a Docker image tagged with $SHORT_SHA.
File Path
- Workflow:
/home/interview/repo-a/.github/workflows/promote.yml
- Config:
/home/interview/repo-b/values.yaml
name: GitOps Promote
on:
push:
branches: [main]
jobs:
build-and-promote:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Calculate Short SHA
run: echo "SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-7)" >> $GITHUB_ENV
- name: Build Docker Image
run: docker build -t app:$SHORT_SHA .
- name: Update Repo B
run: |
cd /home/interview/repo-b
sed -i "s/tag: .*/tag: $SHORT_SHA/" values.yaml
cat values.yaml
git config user.name "CI Bot"
git config user.email "[email protected]"
git add values.yaml
git commit -m "Promote app:$SHORT_SHA"
Explanation
Step 1: Compute SHA with $GITHUB_ENV
- name: Calculate Short SHA
run: echo "SHORT_SHA=$(echo $GITHUB_SHA | cut -c1-7)" >> $GITHUB_ENV
$GITHUB_SHA is a built-in GitHub Actions variable containing the full 40-character commit hash. echo $GITHUB_SHA | cut -c1-7 pipes it to cut, which extracts characters 1 through 7 (the short SHA). Writing it to $GITHUB_ENV makes $SHORT_SHA available as an environment variable in all subsequent steps (unlike $GITHUB_OUTPUT which requires steps.<id>.outputs syntax, $GITHUB_ENV creates a regular environment variable).
Step 2: Build and Promote
- name: Build Docker Image
run: docker build -t app:$SHORT_SHA .
- name: Update Repo B
run: |
cd /home/interview/repo-b
sed -i "s/tag: .*/tag: $SHORT_SHA/" values.yaml
git config user.name "CI Bot"
git config user.email "[email protected]"
git add values.yaml
git commit -m "Promote app:$SHORT_SHA"
The image is built with the SHA tag. Then we navigate to repo-b and use sed -i to update values.yaml with the new tag. -i edits in place. s/tag: .*/tag: $SHORT_SHA/ substitutes the old tag value with the new SHA. The change is committed as "CI Bot".