If its a custom spell, use one of the auto-cast base spells. (Orb of Anhilation, Cold Arrows, Searing Arrows, etc.) However, if you want to cast an active spell on attack, you need to detect the attack and order the unit to cast your spell.
-
Auto-Cast
-
Events
-

Unit - Unit is attacked
-
Conditions
-

((Attacking Unit) Equal to Your_Hero)
-
Actions
-

Unit - Order Your_Hero to Human Archmage - Blizzard
You can add other conditions if you like, such as Mana percentage, Attacked units Health percentage, etc. Note that the spells String ID is the Archmage's Blizzard. Just find your spell base in the list, or change the ID on your spell.