I'm imagining a situation where the defenders would know in advance and put on their gas masks, leaving the attackers unaware and without theirs. Did anything like this ever happen?
The primary reason for the development of chlorine gas and later mustard gas artillery shells was to help German troops attack heavily defended Entente trenches. Defense during WW1 was easy owing to the machine gun, and usually artillery was much easier to employ defensively. The balance of offense vs defense here was so heavily shifted towards the defender that measures like gas were generally not needed. It was better to just fire HE shells.
On top of that, one of the biggest problems with WW1 gas deployment was that the gas clouds tended to drift on the wind. If you deployed gas right in front of your own lines, it might be pushed by a gust of wind back towards you. While the frontline troopers in this scenario with their gas masks would be fine, troops further back or auxiliaries or wounded would be in more trouble. It would also obscure your vision, partially neutralizing your machine gun defenses.
Achieving a concentration of gas from artillery was very difficult, as well. It worked okay in a trench, but it was unreliable as an incapacitating weapon until the development of mustard gas. Mustard gas doesn't require a thick concentration to cause casualties. However, the wounds it causes are not sufficient to necessarily stop an attacking soldier right away, and because of its potency it would disperse very widely. So, on flat ground, it wasn't a great idea either.
Overall, HE shells are much better defensively, as are machine guns, than attempting to deploy a protective curtain of gas. Maybe in some tactical engagement or another some unit had nothing but gas shells, but it's hard to say.