|
| 1 | +name: Docker Build |
| 2 | + |
| 3 | +on: |
| 4 | + push: |
| 5 | + branches: [main, develop, docker_build] # Adjust branches as needed |
| 6 | + pull_request: |
| 7 | + branches: [main] # Adjust branches as needed |
| 8 | + |
| 9 | +permissions: |
| 10 | + contents: read |
| 11 | + packages: write # Needed to push images to GHCR |
| 12 | + |
| 13 | +jobs: |
| 14 | + build-push-run: |
| 15 | + runs-on: ubuntu-latest |
| 16 | + steps: |
| 17 | + - name: Checkout repository |
| 18 | + uses: actions/checkout@v4 |
| 19 | + |
| 20 | + - name: Set up Docker Buildx |
| 21 | + uses: docker/setup-buildx-action@v3 |
| 22 | + |
| 23 | + - name: Log in to GitHub Container Registry |
| 24 | + uses: docker/login-action@v3 |
| 25 | + with: |
| 26 | + registry: ghcr.io |
| 27 | + username: ${{ github.actor }} |
| 28 | + password: ${{ secrets.GITHUB_TOKEN }} |
| 29 | + |
| 30 | + - name: Extract CWS Version and Define Image Tag |
| 31 | + id: image_info |
| 32 | + run: | |
| 33 | + # Assuming utils.sh is at the repository root |
| 34 | + CWS_VER=$(grep 'export CWS_VER=' utils.sh | cut -d"'" -f2) |
| 35 | + # Use GitHub owner and repo name for GHCR image path (lowercase) |
| 36 | + OWNER_LOWER=$(echo "${{ github.repository_owner }}" | tr '[:upper:]' '[:lower:]') |
| 37 | + REPO_LOWER=$(echo "${{ github.event.repository.name }}" | tr '[:upper:]' '[:lower:]') |
| 38 | + IMAGE_NAME="ghcr.io/$OWNER_LOWER/$REPO_LOWER" |
| 39 | + echo "version=$CWS_VER" >> $GITHUB_OUTPUT |
| 40 | + echo "original_tag=nasa-ammos/common-workflow-service:$CWS_VER" >> $GITHUB_OUTPUT |
| 41 | + echo "ghcr_tag=$IMAGE_NAME:$CWS_VER" >> $GITHUB_OUTPUT |
| 42 | + working-directory: ${{ github.workspace }} # Run from repo root |
| 43 | + |
| 44 | + - name: Build CWS Docker Image using script |
| 45 | + run: | |
| 46 | + chmod +x build.sh |
| 47 | + # The script builds using the 'nasa-ammos/...' tag internally |
| 48 | + # Execute the script directly now that we are in its directory |
| 49 | + ./build.sh |
| 50 | + # Explicitly check the exit code of the script |
| 51 | + if [ $? -ne 0 ]; then |
| 52 | + echo "::error::Docker image build script failed." |
| 53 | + exit 1 |
| 54 | + fi |
| 55 | + working-directory: install/docker/cws-image # Run from the script's directory |
| 56 | + |
| 57 | + - name: Re-tag image for GHCR |
| 58 | + run: | |
| 59 | + echo "Tagging ${{ steps.image_info.outputs.original_tag }} as ${{ steps.image_info.outputs.ghcr_tag }}" |
| 60 | + docker tag "${{ steps.image_info.outputs.original_tag }}" "${{ steps.image_info.outputs.ghcr_tag }}" |
| 61 | +
|
| 62 | + - name: Push Docker image to GHCR |
| 63 | + run: | |
| 64 | + echo "Pushing ${{ steps.image_info.outputs.ghcr_tag }}" |
| 65 | + docker push "${{ steps.image_info.outputs.ghcr_tag }}" |
| 66 | +
|
| 67 | + - name: Prepare Docker Compose Environment |
| 68 | + run: | |
| 69 | + # Create external network required by docker-compose |
| 70 | + docker network create cws-network |
| 71 | + echo "Docker network 'cws-network' created" |
| 72 | + working-directory: install/docker/console-db-es-ls-kibana |
| 73 | + |
| 74 | + - name: Update image tag in docker-compose.yml |
| 75 | + run: | |
| 76 | + # Escape slashes in the image tag for sed |
| 77 | + ESCAPED_TAG=$(echo "${{ steps.image_info.outputs.ghcr_tag }}" | sed 's/\//\\\//g') |
| 78 | + echo "Updating image tag in docker-compose.yml to $ESCAPED_TAG" |
| 79 | + # Target both cws and cws-worker services |
| 80 | + sed -i "s/image: nasa-ammos\/common-workflow-service:.*/image: $ESCAPED_TAG/g" docker-compose.yml |
| 81 | + echo "docker-compose.yml after update:" |
| 82 | + cat docker-compose.yml |
| 83 | + working-directory: install/docker/console-db-es-ls-kibana |
| 84 | + |
| 85 | + - name: Start Services with Docker Compose |
| 86 | + run: docker compose up -d |
| 87 | + working-directory: install/docker/console-db-es-ls-kibana |
| 88 | + |
| 89 | + - name: Verify CWS Console Startup |
| 90 | + run: | |
| 91 | + echo "Waiting up to 1 minute for CWS console to become healthy..." # Updated comment |
| 92 | + MAX_WAIT=60 # 1 minute max wait # Updated value and comment |
| 93 | + INTERVAL=15 # Check every 15 seconds |
| 94 | + ELAPSED=0 |
| 95 | + # Use the healthcheck URL from docker-compose.yml |
| 96 | + HEALTHCHECK_URL="https://localhost:38443/cws-ui/login" |
| 97 | +
|
| 98 | + while true; do |
| 99 | + # Use curl's exit code to check success (-k for self-signed cert, -f to fail on server errors, -s silent, -L follow redirects) |
| 100 | + if curl -kfsL --output /dev/null "$HEALTHCHECK_URL"; then |
| 101 | + echo "CWS console is up and responding at $HEALTHCHECK_URL!" |
| 102 | + echo "Current running containers:" |
| 103 | + docker ps |
| 104 | + exit 0 |
| 105 | + fi |
| 106 | +
|
| 107 | + if [ $ELAPSED -ge $MAX_WAIT ]; then |
| 108 | + echo "CWS console did not become healthy within $MAX_WAIT seconds." |
| 109 | + echo "Current running containers:" |
| 110 | + docker ps |
| 111 | + echo "Docker Compose logs for cws service (cws-console):" |
| 112 | + docker compose logs cws |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | +
|
| 116 | + sleep $INTERVAL |
| 117 | + ELAPSED=$((ELAPSED + INTERVAL)) |
| 118 | + echo "Still waiting for CWS console... ($ELAPSED/$MAX_WAIT seconds)" |
| 119 | + done |
| 120 | + working-directory: install/docker/console-db-es-ls-kibana # Ensure correct context for docker-compose logs |
0 commit comments