The Necrons formed temporary alliances a few times when it benefits them, note though they did not need to do this in order to win they did it so as not to waste their own ships/resources.
Word of the Silent King by LJ Goulding
Most recent example of the Necrons is in the Gehenna Campaign. The Necrons under Szarekh the Silent King formed an alliance with the Blood Angels and Dante to stop a Tyranid hive fleet, though this was after the Necrons attacked the world in question to setup a trap to easily defeat the Tyranid fleet but the Blood Angels interfered costing the Necrons time and resources. The Silent King left the Blood Angels to the front lines and only intervened when needed to stop the Tyranids, costing the Blood Angels massive losses while preserving much of his own forces.
The Silent King had met Sanguinius, the primarch of the Blood Angels, and had great respect for him; he believed a true alliance between the Necrons and Imperium could have been formed if Sanguinius had not fallen. Its unknown if the Silent King would have helped another chapter of space marines.
Perhaps, had he not fallen to illogical and prideful infighting, their Sanguinis-Angel might have steered them towards a more enlightened destiny. Certainly, he would have made a more amenable emperor than a preserved witch-corpse. If ever there were a human to be mourned, noble Szarekh would say it was him. That alliance - the first alliance, perhaps?
There is another example of the Necrons helping the Tau against the Tyranids but this could not really be called an alliance. The Tyranids attacked a Tau world which was a tomb world. The Necrons woke up, killed the Tyranids and left the Tau alone. After the battle, when the Tau went to thank them, the Necrons flayed them and killed everything left on the planet.