Barcelona will continue to host Formula One races until 2032 as it strikes a deal that means it will alternate with the Belgian Grand Prix.