How do you create a gradient that follows a path?

I'm trying to create a gradient that follows the path shown below. I have tried mesh fills, but whenever I try, the gradient changes colours. Any suggestions?