Gauge invariance of the Abelian Chern-Simons term Given the Abelian form of the Chern - Simons action we have
$$
\mathcal{S}_{CS}[A]\equiv\frac{k}{4\pi}\int d^3x\epsilon^{\mu\nu\rho}A_\mu\partial_\nu A_\rho
$$
If we would like to check whether this term is gauage invariant we can take a gauage transformation on our gauge field as
$$
A_\mu\rightarrow A_\mu+\partial_\mu\omega
$$
so our action becomes
$$
\begin{aligned}
\mathcal{S}_{CS}[A]&\rightarrow\frac{k}{4\pi}\int d^3x \epsilon^{\mu\nu\rho}(A_\mu+\partial_\mu\omega)\partial_\nu (A_\rho+\partial_\rho\omega)\\
&=\frac{k}{4\pi}\int d^3x \epsilon^{\mu\nu\rho}\left(A_\mu\partial_\nu A_\rho+A_\mu\partial_\nu\partial_\rho\omega+\partial_\mu\omega\partial_\nu A_\rho\right)+\mathcal{O}(\omega^2)\\
&=\mathcal{S}_{CS}[A]+\frac{k}{4\pi}\int d^3x \epsilon^{\mu\nu\rho}(A_\mu \partial_\nu\partial_\rho\omega+\partial_\mu\omega\partial_\nu A_\rho)\\
&=\mathcal{S}_{CS}[A]+\frac{k}{4\pi}\int d^3x \epsilon^{\mu\nu\rho}\left[\partial_\mu(\omega
\partial_\nu A_\rho)-A_\rho\partial_\mu\partial_\nu\omega-\omega\partial_\mu\partial_\nu A_\rho\right]
\end{aligned}$$
According to these notes the gauge transformation of the action is
$$
\mathcal{S}_{CS}[A]\rightarrow \mathcal{S}_{CS}[A]+\frac{k}{4\pi}\int d^3x \epsilon^{\mu\nu\rho}\partial_\mu(\omega\partial_\nu A_\rho)
$$
How are the remaining two terms in my calculation zero? Is there some property of the gauge parameter that I didn't take into account?
 A: To explain the problem in details, I will start from the most generic (non-Abelian) case of the Chern-Simons theory.

Attention: if you are only interested in the answer for Abelian Chern-Simons theory, please jump directly to the last section.

First of all, we start from a principal bundle $P$ over $M$ with the structure group $G$, whose Lie algebra is $\mathfrak{g}$. Let $A$ be its connection, i.e. a $\mathfrak{g}$-valued $1$-form, and $U$ takes values in $G$. The gauge transformation is $$A\rightarrow U^{-1}dU+U{-1}AU.$$
The important thing that some people usually ignore is that neither the connection $1$-form $A$ nor the term $U^{-1}dU$, known as the Maurer–Cartan form, can always be globally defined.
In fact, if the principal bundle $P$ is non-trivial, (e.g. a Dirac monopole is inserted in $M$) then $A$ is only locally defined on $M$.
Under the above gauge transformation, the Chern-Simons action transforms in the following way:
\begin{align}
&\,\,\,\,S[A^{\prime}]=\frac{k}{4\pi}\int_{M}\mathrm{tr}\left(A^{\prime}\wedge dA^{\prime}+\frac{2}{3}A^{\prime}{\wedge}A^{\prime}{\wedge}A^{\prime}\right) \\
&=\frac{k}{4\pi}\int_{M}\mathrm{tr}\left(A\wedge dA+\frac{2}{3}A\wedge A\wedge A\wedge A\right)-I_{\partial M}[U;A]-\Omega[U],
\end{align}
where $$I_{\partial M}[U;A]=\frac{k}{4\pi}\int_{M}\mathrm{tr}\left\{d\left[(dU)U^{-1}\wedge A\right]\right\}=\frac{k}{4\pi}\int_{\partial M}\mathrm{tr}\left[(dU)U^{-1}\wedge A\right]$$ and $$\Omega[U]=\frac{k}{12\pi}\int_{M}\mathrm{tr}(U^{-1}dU\wedge U^{-1}dU\wedge U^{-1}dU).$$
Then, we immediately encounters two difficulties:

*

*The action $S[A]$ may not well-defined on $M$ because, in general, $A$ isn't globally defined and the Lagrangian density of action $S[A]$ depends the choice of coordinate charts.


*The action $S[A]$ is not gauge-invariant.
For the first problem, since the Chern-Simons form is only locally defined, one must use these local data to glue them together so that the action makes sense. The local data is related with the Cech-Deligne cohomology, and is explained in this book. In one of Witten's paper on Chern-Simons theory, he defined the theory in the following way:

Find a four dimensional manifold $N$, such that $M$ is the boundary of $N$. This is always possible because any closed oriented three dimensional manifold can be realized as the boundary of an oriented four dimensional manifold. The proof can be found in this book. Then, using the Stoke's theorem, one defines the Chern-Simons theory as $$S[A]=\frac{k}{4\pi}\int_{M}\mathrm{tr}\left(A\wedge dA+\frac{2}{3}A\wedge A\wedge A\right):=\frac{k}{4\pi}\int_{N}\mathrm{tr}(F\wedge F),$$
where $F=dA+A\wedge A$ is the curvature tensor defined over $N$.

The problem with the above definition is that by extending $M$ to $N$, one also needs to extend $A$ over $M$ to $A$ over $N$. Or one must extend the principal bundle $P\rightarrow M$ to a new principal bundle $P\rightarrow N$, but this is not alway possible. The topological obstruction to the existence of such an extension is measured by the image of the classifying map $$f:M\rightarrow BG,$$
in the cohomology group $H^{3}(BG,\mathbb{Z})$, where $BG$ is the classifying space of the Lie group $G$. There's a theorem in this paper showing that for connected, simply connected Lie group $G$, the third cohomology class vanishes, and so Witten's four-dimensional extension serves a definition of the three dimensional Chern-Simons theory.
In your case, the gauge group is $U(1)$, which is not simply connected, the claim is that such a four dimensional extension is still possible, and is related with bordism theory. The proof of the existence of such a four-dimensional extension can be found here.
Now, we are safe to say that the definition of the Abelian Chern-Simons theory is indeed independent of the choice of coordinate, since $\mathrm{tr}(F\wedge F)$ is well-defined on the four dimensional manifold $N$.
Next, we focus on its gauge invariance, and study the two terms $I_{\partial M}$ and $\Omega[U]$. For the last term $\Omega[U]$, there a theorem (can be found here):

Theorem: Let $G$ be a simple, compact, and simply connected Lie group. The Wess-Zumino-Witten $3$-form $$\frac{1}{24\pi^{2}}\mathrm{tr}(U^{-1}dU\wedge U^{-1}dU\wedge U^{-1}dU)$$
is an element of the third cohomology $H^{3}(G,\mathbb{Z})$.

This implies that, in the path-integral quantization, the term $\Omega[U]$ takes no contribution because it contributes to the functional integrand a factor $e^{2\pi i\mathbb{Z}}$. In your case, since you are interested in the Abelian gauge group, the Wess-Zumino-Witten term never appears.
In more general cases, the quantization of the Chern-Simons level $k$ for a generic Lie group $G$ is reduced to the classifying space $BG$. But the cohomology group of $BG$ is homotopy invariant, one finds that the problem for a generic gauge group $G$ can always be reduced to the case of its maximal compact subgroup. For example, the quantization of $k$ for the $SL(N,\mathbb{C})$-Chern-Simons theory is equivalent to that of $SU(N)$-Chern-Simons Theory, and the quantization of $k$ for the $SL(2,\mathbb{R})$-Chern-Simons theory is equivalent to that of $U(1)$-Chern-Simons theory.

Last Section:
Lastly, we only need to focus on the boundary term. Some people may naively assume that $$I_{\partial M}[U;A]=\frac{k}{4\pi}\int_{M}\mathrm{tr}\left\{d\left[(dU)U^{-1}\wedge A\right]\right\}$$
vanishes on a closed manifold $M$.
This is, however, not true!
The reason is that the Maurer-Cartan form is not globally defined on $M$. This integral may have a non-trivial contribution when the fundamental group $\pi_{1}(M)$ is non-trivial. This is, again, related with Dirac monopole insertion.
To be specific, let's consider a manifold $M=\mathbb{S}^{2}\times\mathbb{S}^{1}$, and a Dirac monopole lies in the sphere $\mathbb{S}^{2}$. Then, the field strength should satisfy the quantization condition $$\int_{\mathbb{S}^{2}}\frac{F}{2\pi}\in\mathbb{Z}.$$
Consider a gauge transformation $A\rightarrow A+d\theta$, the Abelian Chern-Simons action transforms to
\begin{align}
&\,\,\,\,\,\,\,\frac{k}{4\pi}\int_{M}A\wedge dA+\frac{k}{4\pi}\int_{M}d\theta\wedge dA \\
&=\frac{k}{4\pi}\int_{M}A\wedge dA+\frac{k}{2}\int_{\mathbb{S}^{1}}d\theta\int_{\mathbb{S}^{2}}\frac{F}{2\pi}.
\end{align}
Thus, your boundary term contributes a winding number $$\int_{\mathbb{S}^{1}}d\theta\in\pi_{1}(\mathbb{S}^{1}).$$
