Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 27 additions & 43 deletions .github/workflows/docker-build-tag-push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ name: Docker Build, Test and Push

on:
pull_request:
branches: [ master ]
branches: [ main, master ]
push:
branches: [ master ]
branches: [ main, master ]

env:
REGISTRY: docker.io
IMAGE_NAME: ${{ github.repository }}
REPO_NAME: ${{ github.event.repository.name }}

jobs:
test:
Expand All @@ -21,26 +21,25 @@ jobs:
- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3

- name: Build Docker image
- name: Build Docker image for testing
run: |
docker build -t $IMAGE_NAME:test .
- name: Run tests on the image
docker build -t $REPO_NAME:test .

- name: Run tests
run: |
# Add your test commands here
# Example: docker run --rm $IMAGE_NAME:test npm test
# Or run any other validation tests
echo "Running tests on the built image..."
# Replace with your actual test commands
docker run --rm $IMAGE_NAME:test echo "Test completed successfully"
echo "Running tests..."
# Add your actual test commands here
docker run --rm $REPO_NAME:test echo "Tests passed"

build-and-push:
if: github.event_name == 'push'
runs-on: ubuntu-latest
#needs: test
needs: test
steps:
- name: Checkout code
uses: actions/checkout@v4
with:
fetch-depth: 0 # Get full history for commit count

- name: Set up Docker Buildx
uses: docker/setup-buildx-action@v3
Expand All @@ -51,42 +50,27 @@ jobs:
username: ${{ secrets.DOCKERHUB_USERNAME }}
password: ${{ secrets.DOCKERHUB_TOKEN }}

- name: Calculate version number
- name: Calculate version from commit count
id: version
run: |
# Get the latest version from Docker Hub tags or start from 1.0.1
LATEST_VERSION=$(curl -s "https://hub.docker.com/v2/repositories/${{ secrets.DOCKERHUB_USERNAME }}/$(echo ${{ github.repository }} | cut -d'/' -f2)/tags/?page_size=100" | \
jq -r '.results[] | select(.name | test("^[0-9]+\\.[0-9]+\\.[0-9]+$")) | .name' | \
sort -V | tail -n 1 || echo "1.0.0")

# Increment patch version
MAJOR=$(echo $LATEST_VERSION | cut -d. -f1)
MINOR=$(echo $LATEST_VERSION | cut -d. -f2)
PATCH=$(echo $LATEST_VERSION | cut -d. -f3)
NEW_PATCH=$((PATCH + 1))
NEW_VERSION="$MAJOR.$MINOR.$NEW_PATCH"

echo "Latest version: $LATEST_VERSION"
echo "New version: $NEW_VERSION"
echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT
echo "VERSION=$NEW_VERSION" >> $GITHUB_ENV
# Get total commit count for simple versioning
COMMIT_COUNT=$(git rev-list --count HEAD)
VERSION="1.0.$COMMIT_COUNT"
echo "version=$VERSION" >> $GITHUB_OUTPUT
echo "VERSION=$VERSION" >> $GITHUB_ENV
echo "Using version: $VERSION"

- name: Build and push Docker image
- name: Build and push
uses: docker/build-push-action@v5
with:
context: .
push: true
tags: |
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:${{ env.VERSION }}
${{ env.REGISTRY }}/${{ env.IMAGE_NAME }}:latest
cache-from: type=gha
cache-to: type=gha,mode=max
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}:${{ env.VERSION }}
${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}:latest

- name: Update version file (optional)
- name: Verify push
run: |
echo "VERSION=${{ env.VERSION }}" > VERSION
git config user.name "github-actions"
git config user.email "github-actions@github.com"
git add VERSION
git commit -m "Bump version to ${{ env.VERSION }}" || echo "No changes to commit"
git push
echo "Successfully pushed:"
echo " - ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}:${{ env.VERSION }}"
echo " - ${{ secrets.DOCKERHUB_USERNAME }}/${{ env.REPO_NAME }}:latest"