We consider the problem of a government that wants to manage the country's debt-to- GDP (gross domestic product) ratio. The latter evolves stochastically in continuous time, and its drift is given by the interest rate on government debt, net of the growth rate of GDP. We further allow the interest rate to be affected by an exogenous macroeconomic risk process modelled by a continuous-time Markov chain with N states. The debt-to-GDP ratio level can be reduced by the government, e.g. through austerity policies in the form of spending cuts, or increased, e.g. by public investments. The aim of the government is to choose a policy which minimises the total expected cost of having debt plus the total expected cost of austerity policies, counterbalanced by the total expected gain from public investments. We model this as a bounded-variation stochastic control problem over an infinite time-horizon with regime switching, and we provide its explicit solution. To the best of our knowledge, such a problem has not been previously solved in the literature. We show that it is optimal for the government to adopt a policy that keeps the debt-to-GDP ratio in an interval, whose boundaries are depending on the states of the risk process, and are given through a zero-sum optimal stopping game with regime switching. We completely characterise these boundaries as solutions to a system of nonlinear algebraic equations with constraints. Finally, we put in practice our methodology in a case study of a Markov chain with N = 2 states; we provide a thorough analysis and we complement our theoretical results by a detailed numerical study on the sensitivity of the optimal debt ratio management policy with respect to the model's parameters.