The chemical reactions and pathways involving ergosterol, (22E)-ergosta-5,7,22-trien-3-beta-ol, a sterol found in ergot, yeast and moulds. It is the most important of the D provitamins and is converted to vitamin D2 on irradiation with UV light.