As the phrase is primarily recorded in 20th-century America, it is probably a simple extension of ‘spill’ – which has been used to mean ‘divulge’ or ‘reveal’ since as early as the 16th century – but there is a long-lasting folk etymology, which goes back much further in history.
A rudimentary system of voting in Ancient Greece, the birthplace of democracy, was to use beans. Voters would be given both a white and a black or brown bean, one of which would be placed in a jar. White beans were a ‘yes’ vote, while the black or brown stood for ‘no’.
The idea was to keep the ballot secret, so the jar wouldn’t be upended and the beans counted until the end – unless the jar was accidentally knocked over early and the beans spilled.