The hotly contested governor’s race that attracted national attention cost more than $30 million, for television commercials, consultants and other campaign expenses.

The biggest single contributor was the State of Connecticut, which contributed $13 million in accordance with the state’s campaign finance reform law. The state gave $6.5 million each to the campaign of incumbent Gov. Dannel P. Malloy and his Republican challenger, Tom Foley.

Malloy emerged the victor by less than 30,000 votes out of more than 1 million cast, expanding on his winning margin from his 2010 battle with Foley that was the closest gubernatorial election in Connecticut in more than 50 years.

Because of changes in federal and state law, money came pouring into the tight governor’s race, prompting so many television commercials that some voters said they were looking forward to Election Day so the ads would end.

The national Republican Governors Association and the Democratic Governors Association were the two largest contributors among outside groups, providing $5.48 million and $3.54 million, respectively, to the campaigns.

Malloy, an active member of the Democratic governors group, successfully defended his governorship in a year when Republicans made major gains around the country, in the U.S. House of Representatives, the U.S. Senate, and in state legislatures.

Republican Governors Association Chairman Chris Christie, the governor of New Jersey who has clashed publicly with Malloy in recent years, visited Connecticut five times to boost Foley’s chances. The association was the largest contributor to Grow Connecticut Inc., an independent group that created commercials critical of Malloy. Despite spending $7.57 million overall, the group was unable to help Foley win the race.

“Campaigns are expensive, and they’re really expensive in Connecticut,’’ said Liz Kurantowicz, a Republican who is Grow Connecticut’s treasurer. “When you want to have a conversation with voters and tell the story of what needs to happen to make Connecticut better, you have to spend money to do that.’’

A rival PAC that supported Malloy, known as Connecticut Forward, spent $5.675 million overall, including money received from the Democratic governors and about $2 million from various unions.

Separately, Independence USA PAC, which is operated by former New York City Mayor Michael Bloomberg to support pro-gun-control and pro-education candidates, spent $1.7 million, with half for commercials promoting Malloy and half opposing Foley.

Another national group, Americans for Responsible Solutions, spent about $500,000 to support Malloy. The political action committee is headed by former U.S. Rep. Gabrielle Giffords, who visited West Hartford during the campaign to talk with advocates about gun violence.

Overall, outside groups spent about $9 million on behalf of Malloy and about $7.6 million to support Foley, according to public records.

The State Elections Enforcement Commission, which oversees campaign spending, had not tallied the final totals in the governor’s race as of Friday, said Joshua Foley, the department’s spokesman. The commission expects to issue a report in January on expenses by third party groups.

While Malloy and Foley each received $6.5 million in public funds, their final reports on how they spent the money are not due until January.

Interim reports showed that they were spending at a feverish pace, including $1.67 million in one week by Foley near the end of the campaign. In addition to $6.5 million for the general campaign, Foley also received $1.35 million in public funds for the primary against Senate Minority Leader John McKinney of Fairfield.

The state Democratic Party also spent more than $575,000 on behalf of Malloy’s campaign, including nearly $400,000 to Global Strategy Group of Hartford in four payments for professional consulting, according to public records. In the 2010 race, Global Strategy billed more than $550,000 in the governor's race for a total of about $1 million over two races.

Total spending from the state’s coffers not only for the governor’s race but for all state legislative races and statewide offices, reached more than $33 million this year, according to state totals.