Using the Pontryagin maximum principle we optimize the operating conditions of a model external-combustion engine to obtain maximal efficiency. The model engine consists of a cylinder equipped with a piston containing a gas, pumped with a given time-dependent rate of heating, and coupled to a heat bath. We consider a fully cyclic engine, wherein both the volume and the energy of the working fluid are periodic. Such engines possess a finite optimal compression ratio. The gain in efficiency over nonoptimal paths is significant. We demonstrate the results with a numerical example, and we perform a thermodynamic analysis.