Prior to his desertion, Riley served in Company K of the 5th US Infantry Regiment. Riley and Patrick Dalton deserted in 1846, just before the beginning of the Mexican-American War. Both men subsequently joined the Mexican Army, where they eventually formed the "Batallón de San Patricio", or Saint Patrick's Battalion. It was made up of mostly Irish and German immigrants, although it included Catholics from many other countries as well. The unit fought in several battles during the Mexican-American War. Eventually, the battalion was forced to make a last stand at the Battle of Churubusco, which took place on the outskirts of Mexico City. Approximately 35 members of the battalion were killed, while another 85 were captured by US forces, including Riley; the remnants of the unit, numbering approximately 85 men, managed to escape alongside the retreating Mexican forces. Some of the surviving soldiers took part in the Battle for Mexico City, though they were too few to constitute a cohesive military unit. The battalion itself was formally disbanded in August 1848, six months after the end of the war, allegedly due to one of the unit's officers being implicated in an attempted military coup.