Wikipedia indicates that the Salyut 3 was launched with a gun, which could have been used in self-defense or against other satellites, and which was actually tested after the last crew had departed in the Soyuz 15.
Is this the only example of a gun being deployed onboard a spacecraft? Elsewhere, Wikipedia indicates that the unflown OPS-4 station would have been equipped with a different space-to-space system, which would have fired missiles - but I have not come across any other such examples.