This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: Build and Push Docker Image | |
on: | |
push: | |
branches: [ develop, main, docker ] | |
pull_request: | |
branches: [ develop ] | |
workflow_dispatch: | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
permissions: | |
contents: read | |
packages: write | |
outputs: | |
image_digest: ${{ steps.image_digest.outputs.IMAGE_DIGEST }} | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Set up Docker Buildx | |
uses: docker/setup-buildx-action@v2 | |
- name: Log in to GitHub Container Registry | |
uses: docker/login-action@v2 | |
with: | |
registry: ghcr.io | |
username: ${{ github.actor }} | |
password: ${{ secrets.GITHUB_TOKEN }} | |
- name: Extract metadata for Docker | |
id: meta | |
uses: docker/metadata-action@v4 | |
with: | |
images: ghcr.io/${{ github.repository }}/cws | |
tags: | | |
type=ref,event=branch | |
type=ref,event=pr | |
type=sha,format=short | |
type=raw,value=latest,enable=${{ github.ref == format('refs/heads/{0}', 'develop') }} | |
- name: Prepare Docker build context | |
run: | | |
cd ./install/docker/cws-image | |
# Download joda-time jar | |
curl -O https://repo1.maven.org/maven2/joda-time/joda-time/2.1/joda-time-2.1.jar | |
# Create a minimal cws_server.tar.gz | |
mkdir -p cws/server/apache-tomcat-9.0.75/logs | |
echo "placeholder" > cws/server/apache-tomcat-9.0.75/logs/placeholder.txt | |
mkdir -p cws/bin | |
echo "#!/bin/bash" > cws/configure.sh | |
echo "echo 'Mock configure script'" >> cws/configure.sh | |
echo "mkdir -p server/apache-tomcat-9.0.75/logs" >> cws/configure.sh | |
chmod +x cws/configure.sh | |
echo "#!/bin/bash" > cws/start_cws.sh | |
echo "echo 'Mock start script'" >> cws/start_cws.sh | |
echo "tail -f /dev/null" >> cws/start_cws.sh | |
chmod +x cws/start_cws.sh | |
tar -czf cws_server.tar.gz cws | |
rm -rf cws | |
# Make scripts executable | |
chmod +x wait_for_db_es_console.sh startup.sh | |
- name: Build and push Docker image | |
uses: docker/build-push-action@v4 | |
id: docker_build | |
with: | |
context: ./install/docker/cws-image | |
push: ${{ github.event_name != 'pull_request' }} | |
tags: ${{ steps.meta.outputs.tags }} | |
labels: ${{ steps.meta.outputs.labels }} | |
cache-from: type=gha | |
cache-to: type=gha,mode=max | |
- name: Get primary image tag | |
id: image_digest | |
run: | | |
PRIMARY_TAG=$(echo "${{ steps.meta.outputs.tags }}" | head -n 1) | |
echo "IMAGE_DIGEST=${PRIMARY_TAG}" >> $GITHUB_OUTPUT | |
test: | |
needs: build | |
runs-on: ubuntu-latest | |
if: github.event_name != 'pull_request' | |
steps: | |
- name: Checkout repository | |
uses: actions/checkout@v3 | |
- name: Set up test environment | |
run: | | |
mkdir -p test-output | |
- name: Pull Docker image | |
run: | | |
IMAGE_TAG="${{ needs.build.outputs.image_digest }}" | |
echo "Using image: ${IMAGE_TAG}" | |
docker pull ${IMAGE_TAG} | |
- name: Run tests in container | |
run: | | |
IMAGE_TAG="${{ needs.build.outputs.image_digest }}" | |
# Create a Docker volume for test results | |
docker volume create cws-test-results | |
# Run the container with test environment and mount the repository | |
docker run --name cws-test -d \ | |
-e DB_HOST=mock-db \ | |
-e DB_USER=mockuser \ | |
-e DB_PW=mockpw \ | |
-e ES_PROTOCOL=http \ | |
-e ES_HOST=mock-es \ | |
-e ES_PORT=9200 \ | |
-e JAVA_HOME=/usr/lib/jvm/java-openjdk \ | |
-v ${PWD}:/workspace \ | |
-w /workspace \ | |
--entrypoint /bin/bash \ | |
${IMAGE_TAG} -c "tail -f /dev/null" | |
# Check if container is running | |
docker ps | |
# Verify Java setup in the container | |
docker exec cws-test java -version | |
docker exec cws-test echo "JAVA_HOME=$JAVA_HOME" | |
# Install Maven and ensure we have the correct Java version | |
docker exec cws-test yum install -y maven | |
# Verify Java version | |
docker exec cws-test java -version | |
# Run simple tests directly in the container | |
docker exec cws-test bash -c "echo 'Running Java version check'" | |
docker exec cws-test java -version | |
# Run Maven tests directly with explicit Java settings | |
docker exec -w /workspace cws-test bash -c "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk && export PATH=\$JAVA_HOME/bin:\$PATH && mvn -version" | |
# Run unit tests | |
docker exec -w /workspace cws-test bash -c "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk && export PATH=\$JAVA_HOME/bin:\$PATH && mvn -Dmaven.compiler.release=17 -Dmaven.compiler.source=17 -Dmaven.compiler.target=17 clean test jacoco:report-aggregate" | |
# Run integration tests | |
docker exec -w /workspace cws-test bash -c "export JAVA_HOME=/usr/lib/jvm/java-17-openjdk && export PATH=\$JAVA_HOME/bin:\$PATH && mvn -Dmaven.compiler.release=17 -Dmaven.compiler.source=17 -Dmaven.compiler.target=17 integration-test verify -DskipTests" | |
# Show summary of test results | |
docker exec -w /workspace cws-test bash -c "find . -name 'surefire-reports' -type d | xargs -I{} cat {}/*.txt 2>/dev/null || echo 'No test reports found'" || true | |
- name: Clean up | |
if: always() | |
run: | | |
docker stop cws-test || true | |
docker rm cws-test || true |