@@ -2,9 +2,8 @@ name: FHIR IG CI/CD Pipeline with Version Persistence
on :
push :
branches : [ main, develop ]
tags :
- 'v*.*.*' # Trigger on version tags like v0.3.0
- 'v*.*.*'
pull_request :
branches : [ main ]
@@ -14,7 +13,7 @@ env:
jobs :
build-ig :
runs-on : ubuntu-latest
runs-on : fhir-runner
steps :
- name : Checkout repository
@@ -25,7 +24,6 @@ jobs:
- name : Extract version from IG
id : version
run : |
# Extract version from ImplementationGuide resource
VERSION=$(grep -oP '<version value="\K[^"]+' input/bd.fhir.core.xml | head -1)
if [ -z "$VERSION" ]; then
@@ -36,12 +34,10 @@ jobs:
echo "Extracted version: $VERSION"
echo "version=$VERSION" >> $GITHUB_OUTPUT
# Determine if this is a release build (git tag) or dev build
if [[ "$GITHUB_REF" == refs/tags/v* ]]; then
BUILD_TYPE="release"
TAG_VERSION="${GITHUB_REF#refs/tags/v}"
# Verify tag matches IG version
if [ "$TAG_VERSION" != "$VERSION" ]; then
echo "ERROR: Git tag version ($TAG_VERSION) doesn't match IG version ($VERSION)"
exit 1
@@ -53,31 +49,25 @@ jobs:
echo "build_type=$BUILD_TYPE" >> $GITHUB_OUTPUT
echo "Build type: $BUILD_TYPE"
# NEW STEP: Updat e package-list.json BEFORE build so IG Publisher can use it
- name : Pre-build package-list.json update
- name : Prepar e package-list.json and history.xml for IG Publisher
run : |
VERSION="${{ steps.version.outputs.version }}"
BUILD_TYPE="${{ steps.version.outputs.build_type }}"
DATE=$(date +%Y-%m-%d)
# Only update for release builds
if [ "$BUILD_TYPE" != "release" ]; then
echo "ℹ ️ Dev build - skipping package-list.json pre-build update"
exit 0
fi
export VERSION DATE BUILD_TYPE
echo "📋 Updat ing package-list.json before build ..."
echo "📋 Prepar ing package-list.json and history.xml for IG Publisher ..."
# Check if package-list.json exists
if [ ! -f "package-list.json" ]; then
echo "⚠️ package-list.json not found in repo - history.html won't be generated "
echo "Creating m inim al package-list.json for this build ..."
cat > package-list.json << EOF
echo "⚠️ package-list.json not found in repo root "
echo "Creating initi al package-list.json..."
cat > package-list.json << 'PKG EOF'
{
"package-id": "bd.fhir.core",
"title": "Bangladesh Core FHIR Implementation Guide",
"canonical": "https://fhir.dghs.gov.bd/core",
"introduction": "Core FHIR profiles and extensions for Bangladesh healthcare ",
"introduction": "The Bangladesh Core FHIR IG defines national base profiles, value sets, and extensions for health data interoperability. ",
"list": [
{
"version": "current",
@@ -88,24 +78,42 @@ jobs:
}
]
}
EOF
PKG EOF
fi
# Update package-list.json with new version
python3 << 'PYEOF'
import json
import os
import sys
version = "$ VERSION"
date = "$DATE"
version = os.environ.get(' VERSION', '')
date = os.environ.get('DATE', '')
build_type = os.environ.get('BUILD_TYPE', '')
with open('package-list.json', 'r') as f:
with open('package-list.json', 'r', encoding='utf-8' ) as f:
pkg_list = json.load(f)
# Check if this version already exists
version_exists = any(e['version'] == version for e in pkg_list['list'] )
if 'list' not in pkg_list or not isinstance(pkg_list['list'], list):
print("ERROR: package-list.json does not contain a valid 'list' array" )
sys.exit(1)
if not version_exists:
current_entries = [e for e in pkg_list['list'] if e.get('version') == 'current']
if not current_entries:
pkg_list['list'].insert(0, {
"version": "current",
"desc": "Continuous Integration Build (latest in version control)",
"path": "https://fhir.dghs.gov.bd/core/",
"status": "ci-build",
"current": True
})
if build_type == 'release':
version_entry = None
for e in pkg_list['list']:
if e.get('version') == version:
version_entry = e
break
if version_entry is None:
new_entry = {
"version": version,
"date": date,
@@ -114,19 +122,194 @@ jobs:
"status": "trial-use",
"sequence": "STU 1"
}
# Insert after 'current' entry
pkg_list['list'].insert(1, new_entry)
with open('package-list.json', 'w') as f:
json.dump(pkg_list, f, indent=2)
insert_index = 1
for i, entry in enumerate(pkg_list['list']):
if entry.get('version') == 'current':
insert_index = i + 1
break
pkg_list['list'].insert(insert_index, new_entry)
print(f"✅ Added version {version} to package-list.json")
else:
print(f"ℹ ️ Version {version} already exists in package-list.json")
else:
print("ℹ ️ Dev build - using existing package-list.json without release modification")
with open('package-list.json', 'w', encoding='utf-8') as f:
json.dump(pkg_list, f, indent=2, ensure_ascii=False)
PYEOF
echo "📋 package-list.json is ready for IG Publisher "
echo "🔍 Validating package-list.json... "
python3 -m json.tool package-list.json > /dev/null && echo "✅ Valid JSON" || (echo "❌ Invalid JSON!" && exit 1)
echo "📂 Ensuring package-list.json is in required locations..."
mkdir -p input
cp package-list.json input/package-list.json
echo "📝 Generating static history.xml from package-list.json..."
mkdir -p input/pagecontent
python3 << 'PYEOF'
import json
import os
from html import escape
os.makedirs('input/pagecontent', exist_ok=True)
with open('package-list.json', 'r', encoding='utf-8') as f:
pkg_list = json.load(f)
xml = '''<?xml version="1.0" encoding="UTF-8"?>
<div xmlns="http://www.w3.org/1999/xhtml">
<p>This page provides the version history for the Bangladesh Core FHIR Implementation Guide.</p>
<p>For a machine-readable version history see <a href="package-list.json">package-list.json</a>.</p>
<p><b>Published Versions</b></p>
<table class="grid table table-striped table-bordered table-hover">
<thead>
<tr>
<th>Version</th>
<th>Date</th>
<th>Status</th>
<th>Description</th>
</tr>
</thead>
<tbody>
'''
def version_key(v):
try:
return tuple(int(x) for x in v.split('.'))
except:
return (0,)
published = [
e for e in pkg_list['list']
if e.get('version') and e.get('version') != "current"
]
# Sort newest version first
published.sort(
key=lambda e: version_key(e.get("version", "0.0.0")),
reverse=True
)
published_found = False
first_row = True
for entry in published:
published_found = True
version = escape(entry.get('version', 'Unknown'))
date = escape(entry.get('date', 'N/A'))
desc = escape(entry.get('desc', ''))
path = escape(entry.get('path', '#'))
status_val = entry.get('status', 'unknown')
if status_val == "trial-use":
status = '<span class="badge bg-info">Trial Use</span>'
elif status_val == "normative":
status = '<span class="badge bg-success">Normative</span>'
else:
status = f'<span class="badge bg-secondary">{escape(status_val)}</span>'
badge = '<span class="badge bg-success">Latest</span> ' if first_row else ''
xml += f'''
<tr>
<td><a href="{path}">{badge}{version}</a></td>
<td>{date}</td>
<td>{status}</td>
<td>{desc}</td>
</tr>
'''
first_row = False
if not published_found:
xml += '''
<tr>
<td colspan="4">No published versions available yet.</td>
</tr>
'''
xml += '''
</tbody>
</table>
<p class="lead"><b>Continuous Integration Build</b></p>
'''
current_entry = None
for entry in pkg_list['list']:
if entry.get('version') == 'current':
current_entry = entry
break
if current_entry:
path = escape(current_entry.get('path', pkg_list.get('canonical', '') + '/'))
xml += f'''
<p>The latest development build is available at:
<a href="{path}">{path}</a></p>
'''
else:
xml += '''
<p><i>No CI build entry found in package-list.json.</i></p>
'''
xml += '''
</div>
'''
with open('input/pagecontent/history.xml', 'w', encoding='utf-8') as f:
f.write(xml)
print("✅ Generated static history.xml")
print(f" File location: {os.path.abspath('input/pagecontent/history.xml')}")
print(f" File size: {os.path.getsize('input/pagecontent/history.xml')} bytes")
PYEOF
if [ -f "input/pagecontent/history.xml" ]; then
echo "✅ Verified: history.xml exists"
echo " First 20 lines:"
head -20 input/pagecontent/history.xml
else
echo "❌ ERROR: history.xml was not created!"
exit 1
fi
echo "✅ Pre-build preparation complete:"
echo " - Root: $(pwd)/package-list.json"
echo " - Input: $(pwd)/input/package-list.json"
echo " - History: $(pwd)/input/pagecontent/history.xml"
echo "==============================="
echo "PACKAGE LIST USED FOR BUILD:"
cat package-list.json
echo "-------------------------------"
echo "INPUT COPY:"
cat input/package-list.json
echo "==============================="
- name : Emergency Disk Cleanup
run : |
echo "Disk usage before:"
df -h
echo "Clearing tool cache..."
rm -rf /opt/hostedtoolcache/* || true
rm -rf /usr/share/dotnet || true
rm -rf /usr/local/lib/android || true
rm -rf /opt/ghc || true
rm -rf ~/.fhir/packages || true
echo "Disk usage after:"
df -h
- name : Install Docker CLI
run : |
@@ -134,49 +317,102 @@ jobs:
apt-get install -y docker.io
docker --version
- name : Preload previous IG package for comparison
run : |
echo "Detecting previous version..."
PREV_VERSION=$(python3 <<'PY'
import json
with open("package-list.json") as f:
data = json.load(f)
versions = [v["version"] for v in data["list"] if v["version"] != "current"]
print(versions[0] if versions else "")
PY
)
if [ -z "$PREV_VERSION" ]; then
echo "No previous version found. Skipping preload."
exit 0
fi
echo "Previous version detected: $PREV_VERSION"
mkdir -p previous-packages
TMPDIR=$(mktemp -d)
URL="https://fhir.dghs.gov.bd/core/$PREV_VERSION/package.tgz"
echo "Downloading $URL"
curl -L "$URL" -o "$TMPDIR/package.tgz"
mkdir -p "previous-packages/bd.fhir.core#$PREV_VERSION"
tar -xzf "$TMPDIR/package.tgz" -C "previous-packages/bd.fhir.core#$PREV_VERSION"
echo "Previous package cached:"
ls previous-packages
- name : Build FHIR IG
run : |
echo "Building FHIR IG version ${{ steps.version.outputs.version }}..."
CONTAINER_ID=$(docker create \
-v $(pwd)/previous-packages:/previous-packages \
hl7fhir/ig-publisher-base:latest \
/bin/bash -c "cp -r /home/publisher/ig /tmp/build && cd /tmp/build && _updatePublisher.sh -y && _genonce.sh")
/bin/bash -c "
mkdir -p /tmp/build
cp -r /home/publisher/ig /tmp/build/ig
cd /tmp/build/ig
rm -f package-list.json
_updatePublisher.sh -y
_genonce.sh
")
echo "Container ID: $CONTAINER_ID"
docker cp $(pwd)/. $CONTAINER_ID:/home/publisher/ig/
docker start -a $CONTAINER_ID
docker cp " $(pwd)/." " $CONTAINER_ID:/home/publisher/ig/"
echo "Mounted FHIR packages:"
ls -R previous-packages || echo "No previous packages directory"
docker start -a "$CONTAINER_ID"
EXIT_CODE=$?
# Copy outputs
echo "Copying outputs from container..."
docker cp $CONTAINER_ID:/tmp/build/output ./output || echo "Warning: No output directory"
docker cp $CONTAINER_ID:/tmp/build/fsh-generated ./fsh-generated || echo "No FSH generated"
docker cp $CONTAINER_ID:/tmp/build/input-cache ./input-cache || echo "No input-cache"
docker cp $CONTAINER_ID:/tmp/build/temp ./temp || echo "No temp directory"
docker cp " $CONTAINER_ID:/tmp/build/ig/ output" ./output || echo "Warning: No output directory"
docker cp " $CONTAINER_ID:/tmp/build/ig/ fsh-generated" ./fsh-generated || echo "No FSH generated"
docker cp " $CONTAINER_ID:/tmp/build/ig/ input-cache" ./input-cache || echo "No input-cache"
docker cp " $CONTAINER_ID:/tmp/build/ig/ temp" ./temp || echo "No temp directory"
if [ $EXIT_CODE -ne 0 ]; then
echo "Build failed, showing logs:"
docker logs $CONTAINER_ID
docker rm $CONTAINER_ID
docker logs " $CONTAINER_ID"
docker rm " $CONTAINER_ID"
exit 1
fi
docker rm $CONTAINER_ID
docker rm " $CONTAINER_ID"
if [ ! -f "output/index.html" ]; then
echo "ERROR: Build failed - no index.html"
exit 1
fi
# Check if history.html was generated
echo ""
echo "🔍 Checking for history.html..."
if [ -f "output/history.html" ]; then
echo "✅ history.html generated successfully"
echo "✅ history.html generated successfully! "
echo "📄 history.html size: $(ls -lh output/history.html | awk '{print $5}')"
else
echo "⚠️ WARNING: history.html was not generated"
echo "This usually means package-list.json was missing or invalid "
echo "⚠️ WARNING: history.html was NOT generated"
echo "This might indicate an issue with the template or history.xml/package-list.json "
fi
echo "================================"
echo "IG Publisher comparison log:"
cat output/qa.compare.txt || echo "qa.compare.txt not found"
echo "================================"
echo "✅ Build successful!"
- name : Update package-feed.xml for releases
@@ -205,7 +441,7 @@ jobs:
entry_exists = False
for entry in root.findall('atom:entry', ns):
title = entry.find('atom:title', ns)
if title is not None and version in title.text:
if title is not None and version in ( title.text or '') :
entry_exists = True
entry_updated = entry.find('atom:updated', ns)
if entry_updated is not None:
@@ -241,12 +477,10 @@ jobs:
root.insert(insert_pos, new_entry)
tree.write('output/package-feed.xml', encoding='utf-8', xml_declaration=True)
print(f "✅ Updated package-feed.xml")
print("✅ Updated package-feed.xml")
EOF
python3 update-feed.py "$VERSION" "$DATETIME"
# Also copy the updated package-list.json to output
cp package-list.json output/package-list.json
echo "📋 Updated registry files"
@@ -262,9 +496,8 @@ jobs:
echo "build_type=$BUILD_TYPE" >> deployment.env
echo "build_date=$(date -u +%Y-%m-%dT%H:%M:%SZ)" >> deployment.env
# List what's in the output
echo "📦 Output contents:"
ls -lh output/ | grep -E "(history\.html|package-list\.json|package-feed\.xml|index\.html)"
ls -lh output/ | grep -E "(history\.html|package-list\.json|package-feed\.xml|index\.html)" || echo "Some files may be missing"
ls -lh ig-output.tar.gz
@@ -282,7 +515,7 @@ jobs:
deploy :
needs : build-ig
runs-on : ubuntu-latest
if : ( github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v'))
if : github.ref == 'refs/heads/main' || startsWith(github.ref, 'refs/tags/v')
steps :
- name : Download artifact
@@ -321,7 +554,6 @@ jobs:
script : |
set -e
# Load deployment info
source /tmp/fhir-ig-deploy/deployment.env
echo "=========================================="
@@ -331,69 +563,59 @@ jobs:
echo "Build Date: $build_date"
echo "=========================================="
# Create version directory structure
VERSIONS_DIR="/opt/fhir-ig/versions"
mkdir -p "$VERSIONS_DIR"
# Determine target directory
if [ "$build_type" == "release" ]; then
if [ "$build_type" = "release" ]; then
TARGET_DIR="$VERSIONS_DIR/$version"
echo "📦 Deploying release version to: $TARGET_DIR"
else
TARGET_DIR="$VERSIONS_DIR/dev"
echo "🔧 Deploying dev build to: $TARGET_DIR"
mkdir -p "$TARGET_DIR"
echo "Cleaning old dev files..."
rm -rf "$TARGET_DIR"/*
fi
# Create target directory
mkdir -p "$TARGET_DIR"
# Extract IG output
echo "Extracting IG output..."
tar -xzf /tmp/fhir-ig-deploy/ig-output.tar.gz -C "$TARGET_DIR"
# Verify history.html was deployed
if [ -f "$TARGET_DIR/history.html" ]; then
echo "✅ history.html deployed successfully"
else
echo "⚠️ WARNING: history.html not found in deployment"
fi
cp /tmp/fhir-ig-deploy /package-list.json "$VERSIONS_DIR/package-list.json"
cp "$TARGET_DIR /package-list.json" "$VERSIONS_DIR/package-list.json"
cp "$TARGET_DIR/package-feed.xml" "$VERSIONS_DIR/package-feed.xml"
# Copy package-feed.xml to root
cp /tmp/fhir-ig-deploy /package-feed.xml "$VERSIONS_DIR /package-feed.xml"
cp "$TARGET_DIR/package-list.json" "/opt/fhir-ig/package-list.json"
cp "$TARGET_DIR /package-feed.xml" "/opt/fhir-ig /package-feed.xml"
# Update 'current' symlink for releases
if [ "$build_type" == "release" ]; then
if [ "$build_type" = "release" ]; then
echo "Updating 'current' symlink to point to $version"
rm -f "$VERSIONS_DIR/current"
ln -sf "$version" "$VERSIONS_DIR/current"
fi
# Ensure nginx container is running with correct config
cd /opt/fhir-ig
# Download deployment files if they don't exist
if [ ! -f "docker-compose.prod.yml" ]; then
echo "ERROR: docker-compose.prod.yml not found!"
echo "Please deploy the updated docker-compose.prod.yml and nginx.conf first"
exit 1
fi
# Force recreate container to ensure new config/mounts are applied
# This handles "stuck" states better than a simple restart
docker compose -f docker-compose.prod.yml up -d --force-recreate fhir-ig
# Cleanup
rm -rf /tmp/fhir-ig-deploy
echo "=========================================="
echo "✅ Deployment completed successfully!"
echo "Version $version is now available at:"
if [ "$build_type" == "release" ]; then
if [ "$build_type" = "release" ]; then
echo " - https://fhir.dghs.gov.bd/core/$version/"
echo " - https://fhir.dghs.gov.bd/core/$version/history.html"
echo " - https://fhir.dghs.gov.bd/core/ (current)"
@@ -402,6 +624,5 @@ jobs:
fi
echo "=========================================="
# List all versions
echo "Available versions:"
ls -lh "$VERSIONS_DIR" | grep -v total