name: Build and Publish Docker Image on: pull_request: branches: - main push: branches: - main jobs: build-and-publish: name: Build and Publish runs-on: docker steps: - name: Checkout uses: actions/checkout@v4 with: fetch-depth: 0 # Fetch all history for tags - name: Setup Docker Buildx run: | docker buildx create --use || true - name: Login to Registry run: | echo "${{ secrets.REGISTRY_PASSWORD }}" | docker login git.jusemon.com -u "${{ secrets.REGISTRY_USERNAME }}" --password-stdin - name: Determine Version id: version run: | # Get branch name (works for both GitHub and Forgejo) BRANCH_NAME="${GITHUB_REF#refs/heads/}" BRANCH_NAME="${BRANCH_NAME#refs/remotes/origin/}" if [[ -z "$BRANCH_NAME" ]]; then BRANCH_NAME=$(git rev-parse --abbrev-ref HEAD) fi # Get latest version tag LATEST_TAG=$(git describe --tags --match 'v*.*.*' --abbrev=0 2>/dev/null || echo "v0.0.0") LATEST_VERSION="${LATEST_TAG#v}" # Parse version components IFS='.' read -r MAJOR MINOR PATCH <<< "$LATEST_VERSION" MAJOR=${MAJOR:-0} MINOR=${MINOR:-0} PATCH=${PATCH:-0} # Determine version bump based on branch prefix if [[ "$BRANCH_NAME" == release/* ]]; then # Release branch: use the version from branch name or bump minor VERSION_FROM_BRANCH=$(echo "$BRANCH_NAME" | sed -n 's|release/\(.*\)|\1|p') if [[ -n "$VERSION_FROM_BRANCH" && "$VERSION_FROM_BRANCH" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; then NEW_VERSION="$VERSION_FROM_BRANCH" else MINOR=$((MINOR + 1)) PATCH=0 NEW_VERSION="$MAJOR.$MINOR.$PATCH" fi elif [[ "$BRANCH_NAME" == feature/* ]]; then # Feature branch: bump minor version MINOR=$((MINOR + 1)) PATCH=0 NEW_VERSION="$MAJOR.$MINOR.$PATCH" elif [[ "$BRANCH_NAME" == bugfix/* ]] || [[ "$BRANCH_NAME" == hotfix/* ]]; then # Bugfix/Hotfix: bump patch version PATCH=$((PATCH + 1)) NEW_VERSION="$MAJOR.$MINOR.$PATCH" elif [[ "$BRANCH_NAME" == main ]]; then # Main branch: increment patch version from latest tag, or start at 0.1.0 if no tags exist if [[ "$LATEST_TAG" == "v0.0.0" ]]; then # First version NEW_VERSION="0.1.0" else # Increment patch version for each merge to main PATCH=$((PATCH + 1)) NEW_VERSION="$MAJOR.$MINOR.$PATCH" fi else # Default: bump patch PATCH=$((PATCH + 1)) NEW_VERSION="$MAJOR.$MINOR.$PATCH" fi # For PRs, use a pre-release version EVENT_NAME="${{ github.event_name }}" if [[ -z "$EVENT_NAME" ]]; then EVENT_NAME="${{ gitea.event_name }}" fi if [[ "$EVENT_NAME" == "pull_request" ]]; then PR_NUMBER="${{ github.event.pull_request.number }}" if [[ -z "$PR_NUMBER" ]]; then PR_NUMBER="${{ gitea.event.pull_request.number }}" fi if [[ -n "$PR_NUMBER" ]]; then NEW_VERSION="${NEW_VERSION}-pr${PR_NUMBER}" fi fi echo "version=$NEW_VERSION" >> $GITHUB_OUTPUT echo "tag=v$NEW_VERSION" >> $GITHUB_OUTPUT echo "Branch: $BRANCH_NAME" echo "Latest tag: $LATEST_TAG" echo "New version: $NEW_VERSION" - name: Build Docker Image run: | IMAGE_NAME="git.jusemon.com/jusemon/threejs-test:${{ steps.version.outputs.version }}" docker build -t "$IMAGE_NAME" . echo "IMAGE_NAME=$IMAGE_NAME" >> $GITHUB_ENV - name: Validate Image run: | docker run --rm -d --name test-container -p 8080:80 "$IMAGE_NAME" sleep 2 curl -f http://localhost:8080 || exit 1 docker stop test-container - name: Push Docker Image if: (github.event_name == 'push' || gitea.event_name == 'push') && (github.ref == 'refs/heads/main' || gitea.ref == 'refs/heads/main') run: | IMAGE_NAME="git.jusemon.com/jusemon/threejs-test:${{ steps.version.outputs.version }}" docker push "$IMAGE_NAME" # Also tag as 'latest' for main branch docker tag "$IMAGE_NAME" "git.jusemon.com/jusemon/threejs-test:latest" docker push "git.jusemon.com/jusemon/threejs-test:latest" - name: Create Git Tag if: (github.event_name == 'push' || gitea.event_name == 'push') && (github.ref == 'refs/heads/main' || gitea.ref == 'refs/heads/main') run: | git config user.name "forgejo-actions" git config user.email "forgejo-actions@forgejo.io" TAG="${{ steps.version.outputs.tag }}" # Check if tag already exists if git rev-parse "$TAG" >/dev/null 2>&1; then echo "Tag $TAG already exists, skipping tag creation" else git tag -a "$TAG" -m "Release ${{ steps.version.outputs.version }}" git push origin "$TAG" fi