We constrain the coupling of thermally-produced axion-like particles (here axions) with photons and gluons, using data from the cosmic microwave background (CMB) spectra and baryon acoustic oscillations. The axion possesses an explicit soft breaking mass term and it is produced thermally in the early Universe from either axion-photon or axion-gluon processes, accounting for the recent progresses in the field. We derive the most stringent bounds on the axion-gluon coupling to date on the mass range considered 10^(-4)eV < m_a < 100eV, superseding the current bounds from SN1987A. The bounds on the axion-photon coupling are competitive with the results from the CAST collaboration for the axion mass > 3eV. We comment on the forecast reaches that will be available given the sensitivity of future CMB-S4 missions. Based on 2205.01637