Well yeah, that you'll have to check. You can find out on the internet what the ports are on your 2 computers or in the manual or just by looking at them if you know the difference.
I just checked on everymac.com and it says that the 2012 iMac has Thunderbolt v1 and the 2017 has Thunderbolt v3. So you would need a thunderbolt cable with a v1 plug on one end and a v3 plug on the other.