Skip to content

Commit c1200cf

Browse files
Implement git hook for Docker image tagging with git commit hash (#263)
* Test pre-commit hook: Update docker-compose.yaml comment * Add git-hooks directory with pre-commit hook for Docker versioning * Rename PROJECT_VERSION to DOCKER_IMAGE_TAG and add auto-generated comment * Update docker-compose.yaml comments * Update docker-compose files to use DOCKER_IMAGE_TAG instead of PROJECT_VERSION * Update git-hooks README with additional explanation * Move comment above DOCKER_IMAGE_TAG and rename pre-commit hook to update-docker-image-tag.pre-commit * Update docker-compose.yaml comments * Fix duplicate comment issue in pre-commit hook * Add more details to docker-compose.yaml comments --------- Co-authored-by: openhands <[email protected]>
1 parent 5381f28 commit c1200cf

File tree

9 files changed

+120
-7
lines changed

9 files changed

+120
-7
lines changed

.env

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# This top-level .env file under AirStack/ defines variables that are propagated through docker-compose.yaml
22
PROJECT_NAME="airstack"
3-
PROJECT_VERSION="0.13.1"
3+
# auto-generated from git commit hash
4+
DOCKER_IMAGE_TAG="3f238b1"
45
# can replace with your docker hub username
56
PROJECT_DOCKER_REGISTRY="airlab-storage.andrew.cmu.edu:5001/shared"
67
DEFAULT_ISAAC_SCENE="omniverse://airlab-storage.andrew.cmu.edu:8443/Projects/AirStack/AFCA/fire_academy_faro_with_sky.scene.usd"

docker-compose.yaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
11
# top-level docker-compose file for the autonomy stack
2+
# This file includes all component docker-compose files
3+
# Each component has its own docker-compose.yaml file
4+
# Components are organized by functionality
5+
# This allows for modular development and deployment
26
include:
37
- simulation/isaac-sim/docker/docker-compose.yaml
48
- robot/docker/docker-compose.yaml

docs/docker/docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ services:
55
- ""
66
- sitl
77
- hitl
8-
image: &image_tag ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${PROJECT_VERSION}_mkdocs
8+
image: &image_tag ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${DOCKER_IMAGE_TAG}_mkdocs
99
build:
1010
dockerfile: ./Dockerfile
1111
tags:

git-hooks/README.md

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# Git Hooks
2+
3+
This directory contains git hooks used in the AirStack repository.
4+
5+
## Available Hooks
6+
7+
### Docker Versioning Hook
8+
9+
The `update-docker-image-tag.pre-commit` hook automatically updates the `DOCKER_IMAGE_TAG` in the `.env` file with the current git commit hash whenever Docker-related files (Dockerfile or docker-compose.yaml) are modified. It also adds a comment above the variable indicating that the value is auto-generated from the git commit hash.
10+
11+
This ensures that Docker images are always tagged with the exact commit they were built from, eliminating version conflicts between parallel branches.
12+
13+
### Installation
14+
15+
To install the hooks:
16+
17+
1. Copy the hook to your local .git/hooks directory:
18+
```bash
19+
cp git-hooks/docker-versioning/update-docker-image-tag.pre-commit .git/hooks/pre-commit
20+
```
21+
22+
2. Make sure the hook file is executable:
23+
```bash
24+
chmod +x .git/hooks/pre-commit
25+
```
26+
27+
## How the Docker Versioning Hook Works
28+
29+
1. When you commit changes, the hook checks if any Dockerfile or docker-compose.yaml files are being committed
30+
2. If Docker-related files are detected, it updates the DOCKER_IMAGE_TAG in the .env file with the current git commit hash and adds a comment above the variable
31+
3. The modified .env file is automatically added to the commit
32+
33+
This approach eliminates version conflicts between parallel branches by ensuring Docker images are tagged with the exact commit they were built from.

git-hooks/docker-versioning/README.md

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Docker Versioning Git Hook
2+
3+
This directory contains a git hook that automatically updates the Docker image tag with the current git commit hash.
4+
5+
## Hook: update-docker-image-tag.pre-commit
6+
7+
This pre-commit hook automatically updates the `DOCKER_IMAGE_TAG` in the `.env` file with the current git commit hash whenever Docker-related files (Dockerfile or docker-compose.yaml) are modified.
8+
9+
### Features
10+
11+
- Automatically updates `DOCKER_IMAGE_TAG` with the git commit hash
12+
- Adds a comment above the variable indicating it's auto-generated
13+
- Only triggers when Docker-related files are modified
14+
- Automatically stages the modified .env file for commit
15+
16+
### Installation
17+
18+
To install the hook:
19+
20+
1. Copy the hook to your local .git/hooks directory:
21+
```bash
22+
cp update-docker-image-tag.pre-commit ../../.git/hooks/pre-commit
23+
```
24+
25+
2. Make sure the hook file is executable:
26+
```bash
27+
chmod +x ../../.git/hooks/pre-commit
28+
```
29+
30+
### Benefits
31+
32+
- Eliminates version conflicts between parallel branches
33+
- Ensures Docker images are tagged with the exact commit they were built from
34+
- Simplifies tracking which version of the code is running in Docker containers
35+
- Provides a consistent and automated versioning system for Docker images
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
#!/bin/bash
2+
3+
# Pre-commit hook to update DOCKER_IMAGE_TAG in .env file with git commit hash
4+
# when Dockerfile or docker-compose.yaml files are modified
5+
6+
# Check if any Dockerfile or docker-compose.yaml files are staged for commit
7+
DOCKER_FILES_CHANGED=$(git diff --cached --name-only | grep -E 'Dockerfile|docker-compose\.yaml$')
8+
9+
if [ -n "$DOCKER_FILES_CHANGED" ]; then
10+
echo "Docker-related files changed. Updating DOCKER_IMAGE_TAG in .env file..."
11+
12+
# Get the current commit hash (short version)
13+
COMMIT_HASH=$(git rev-parse --short HEAD)
14+
15+
# Update the DOCKER_IMAGE_TAG in .env file
16+
if [ -f ".env" ]; then
17+
# Check if DOCKER_IMAGE_TAG line exists
18+
if grep -q "^DOCKER_IMAGE_TAG=" .env; then
19+
# Replace the existing DOCKER_IMAGE_TAG line and ensure comment is above it
20+
# First, remove any existing auto-generated comment
21+
sed -i '/^# auto-generated from git commit hash$/d' .env
22+
# Add the comment above the DOCKER_IMAGE_TAG line
23+
sed -i '/^DOCKER_IMAGE_TAG=/i\# auto-generated from git commit hash' .env
24+
# Update the DOCKER_IMAGE_TAG value
25+
sed -i "s/^DOCKER_IMAGE_TAG=.*$/DOCKER_IMAGE_TAG=\"$COMMIT_HASH\"/" .env
26+
echo "Updated DOCKER_IMAGE_TAG to $COMMIT_HASH in .env file"
27+
28+
# Stage the modified .env file for commit
29+
git add .env
30+
else
31+
echo "Error: DOCKER_IMAGE_TAG line not found in .env file"
32+
exit 1
33+
fi
34+
else
35+
echo "Error: .env file not found"
36+
exit 1
37+
fi
38+
fi
39+
40+
exit 0

ground_control_station/docker/ground-control-station-base-docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# docker compose file
22
services:
33
ground-control-station-base:
4-
image: &gcs_image ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${PROJECT_VERSION}_gcs
4+
image: &gcs_image ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${DOCKER_IMAGE_TAG}_gcs
55
build:
66
context: ../
77
dockerfile: docker/Dockerfile.gcs

robot/docker/docker-compose.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ services:
1010
extends:
1111
file: ./robot-base-docker-compose.yaml
1212
service: robot_base
13-
image: &desktop_image ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${PROJECT_VERSION}_robot-x86-64
13+
image: &desktop_image ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${DOCKER_IMAGE_TAG}_robot-x86-64
1414
build:
1515
dockerfile: ./Dockerfile.robot
1616
args:
@@ -42,7 +42,7 @@ services:
4242
extends:
4343
file: ./robot-base-docker-compose.yaml
4444
service: robot_base
45-
image: &l4t_image ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${PROJECT_VERSION}_robot-l4t
45+
image: &l4t_image ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${DOCKER_IMAGE_TAG}_robot-l4t
4646
build:
4747
dockerfile: ./Dockerfile.robot
4848
args:

simulation/isaac-sim/docker/docker-compose.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
services:
22
isaac-sim:
3-
image: &image_tag ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${PROJECT_VERSION}_isaac-sim
3+
image: &image_tag ${PROJECT_DOCKER_REGISTRY}/${PROJECT_NAME}:v${DOCKER_IMAGE_TAG}_isaac-sim
44
build:
55
context: ../
66
dockerfile: docker/Dockerfile.isaac-ros
@@ -58,7 +58,7 @@ services:
5858
# developer stuff
5959
- .dev:/root/.dev:rw # developer config
6060
- .bashrc:/root/.bashrc:rw # bash config
61-
- ../../common/inputrc:/etc/inputrc:rw # for using page up/down to search through command history
61+
- ../../common/inputrc:/etc/inputrc:rw # for using page up/down to search through command history
6262
# code
6363
- ../sitl_integration/kit-app-template/source/extensions/:/root/Documents/Kit/shared/exts/
6464
- ../sitl_integration:/sitl_integration:rw

0 commit comments

Comments
 (0)