Merge pull request #14 from opencitations/2025_add_health_k8s #56
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: Docker Build and Push | |
| on: | |
| push: | |
| branches: [ main, master ] | |
| pull_request: | |
| branches: [ main, master ] | |
| env: | |
| SERVIZIO_OC: oc_api | |
| DOCKER_REGISTRY: opencitations | |
| jobs: | |
| docker-build-push: | |
| runs-on: ubuntu-latest | |
| steps: | |
| - name: Checkout code | |
| uses: actions/checkout@v4 | |
| - name: Read version from docker_version.txt | |
| id: get_version | |
| run: | | |
| if [ ! -f docker_version.txt ]; then | |
| echo "Error: docker_version.txt file not found" | |
| exit 1 | |
| fi | |
| VERSION=$(cat docker_version.txt | tr -d '\n\r' | xargs) | |
| if [ -z "$VERSION" ]; then | |
| echo "Error: empty version in docker_version.txt" | |
| exit 1 | |
| fi | |
| echo "VERSION=$VERSION" >> $GITHUB_OUTPUT | |
| echo "Found version: $VERSION" | |
| - name: Check if image exists on DockerHub | |
| id: check_image | |
| env: | |
| VERSION: ${{ steps.get_version.outputs.VERSION }} | |
| run: | | |
| echo "Checking if image exists: ${{ env.DOCKER_REGISTRY }}/${{ env.SERVIZIO_OC }}:${VERSION}" | |
| # Query DockerHub API to check if tag exists | |
| HTTP_CODE=$(curl -s -o /dev/null -w "%{http_code}" \ | |
| "https://hub.docker.com/v2/repositories/${{ env.DOCKER_REGISTRY }}/${{ env.SERVIZIO_OC }}/tags/${VERSION}/") | |
| if [ "$HTTP_CODE" == "200" ]; then | |
| echo "Image already exists on DockerHub" | |
| echo "IMAGE_EXISTS=true" >> $GITHUB_OUTPUT | |
| else | |
| echo "Image not found on DockerHub, will build new one" | |
| echo "IMAGE_EXISTS=false" >> $GITHUB_OUTPUT | |
| fi | |
| - name: Set up Docker Buildx | |
| if: steps.check_image.outputs.IMAGE_EXISTS == 'false' | |
| uses: docker/setup-buildx-action@v3 | |
| - name: Login to DockerHub | |
| if: steps.check_image.outputs.IMAGE_EXISTS == 'false' | |
| uses: docker/login-action@v3 | |
| with: | |
| username: ${{ secrets.DOCKERHUB_USERNAME }} | |
| password: ${{ secrets.DOCKERHUB_TOKEN }} | |
| - name: Build and push Docker image | |
| if: steps.check_image.outputs.IMAGE_EXISTS == 'false' | |
| env: | |
| VERSION: ${{ steps.get_version.outputs.VERSION }} | |
| run: | | |
| echo "Building image: ${{ env.DOCKER_REGISTRY }}/${{ env.SERVIZIO_OC }}:${VERSION}" | |
| # Build image with no cache | |
| docker build --no-cache -t ${{ env.DOCKER_REGISTRY }}/${{ env.SERVIZIO_OC }}:${VERSION} . | |
| sleep 1 | |
| # Tag image | |
| docker tag ${{ env.DOCKER_REGISTRY }}/${{ env.SERVIZIO_OC }}:${VERSION} ${{ env.DOCKER_REGISTRY }}/${{ env.SERVIZIO_OC }}:${VERSION} | |
| sleep 1 | |
| # Push to registry | |
| echo "Pushing image to DockerHub..." | |
| docker push ${{ env.DOCKER_REGISTRY }}/${{ env.SERVIZIO_OC }}:${VERSION} | |
| sleep 1 | |
| # Check result | |
| if [ $? == 0 ]; then | |
| echo "ALL DONE !" | |
| else | |
| echo "NO NO NOOOOOOO !" | |
| exit 1 | |
| fi | |
| - name: Build summary | |
| run: | | |
| VERSION="${{ steps.get_version.outputs.VERSION }}" | |
| IMAGE_EXISTS="${{ steps.check_image.outputs.IMAGE_EXISTS }}" | |
| echo "Build Summary:" | |
| echo "Version: ${VERSION}" | |
| echo "Image: ${{ env.DOCKER_REGISTRY }}/${{ env.SERVIZIO_OC }}:${VERSION}" | |
| if [ "$IMAGE_EXISTS" == "true" ]; then | |
| echo "Status: Image already exists, skipped build" | |
| else | |
| echo "Status: New image built and pushed successfully" | |
| fi |