The CMS experiment at the LHC is equipped with a high granularity lead tungstate crystal electromagnetic calorimeter (ECAL) offering an excellent energy resolution. The ECAL was crucial in the discovery and subsequent characterization of the Higgs boson, particularly in the two photon and two Z boson decay channels. The LHC has reached an unprecedented luminosity during Run 2 (2016-2018), leading to a high numbers of proton-proton interactions per bunch collision (pileup), exceeding the design value and resulting in a very high radiation environment. We will present how we maintain the high performance of the calorimeter in these difficult conditions, challenging for both the calibration and the reconstruction. A new readout is being developed to operate the calorimeter at the high-luminosity LHC (HL-LHC) with an even higher luminosity, reaching an average pileup of up to 200. Precise signal time measurement and limitation of the dark current induced by radiation damaged are key ingredients to maintain a high energy resolution in the HL-LHC conditions. This upgrade of the CMS electromagnetic calorimeter will be presented.