  The $B^{\pm}$ meson production asymmetry in $pp$ collisions is measured using
$B^+ \to \bar{D}^0 \pi^+$ decays. The data were recorded by the LHCb experiment
during Run 1 of the LHC at centre-of-mass energies of $\sqrt{s}=$ 7 and 8 TeV.
The production asymmetries, integrated over transverse momenta in the range $2
< p_{\rm T} < 30$ GeV/$c$, and rapidities in the range $2.1 < y < 4.5$, are
measured to be
  \begin{align*} \mathcal{A}_{\rm prod}(B^+,\sqrt{s}=7~{\rm TeV}) &= (-0.41 \pm
0.49 \pm 0.10) \times 10^{-2},\\ \mathcal{A}_{\rm prod}(B^+,\sqrt{s}=8~{\rm
TeV}) &= (-0.53 \pm 0.31 \pm 0.10) \times 10^{-2},
  \end{align*} where the first uncertainties are statistical and the second are
systematic. These production asymmetries are used to correct the raw
asymmetries of $B^{+} \to J/\psi K^{+}$ decays, thus allowing a measurement of
the $CP$ asymmetry, \begin{equation*}
  \mathcal{A}_{CP} = (0.09 \pm 0.27 \pm 0.07) \times 10^{-2}.
  \end{equation*}
