LineageOS and custom roms have been doing this for close to two decades. And those are entirely based on volunteer efforts. It actually doesn't take that much work. The reason Google etc don't do it is because then they'd get to sell fewer phones since they wouldn't be getting obsolete so quickly.
Apple supports devices for a lot longer because they get a lot of recurring and ongoing revenue from Apple services on Apple devices, so it's in their favour if devices stay usable for longer.
You can understand almost all decisions made by tech companies if you think along the lines of "how are they profiting by doing _______?"