First off, I recommend you check out 'Seveneves' by Neal Stephenson. I know it's not exactly the same scenario you describe, but it's pretty close: the moon explodes without warning and humanity is left trying to figure out how to survive what comes next. (that's not a spoiler BTW, it's pretty much the first sentence of the book!)
First off no spoilers.
This isn't tied to any fiction (that I know of) but I was just musing on this in my head and wondered what others could say to add to this.
I'm not sure how to build these two either so I'll just write down my thoughts.
Scenario 1 The sun for whatever reason starts to go darker and eventually go out.
Yes I know it's very fanciful and not ever likely to happen but say we had indications of this and it would take a while to happen, so what do we do here on Earth to prepare ourselves and keep our civilization going?
What steps would we take to ensure life on our planet continues?
Well the most immediate consequence is that the Earth will get a lot colder. How we respond to that really depends on the time scale. If we're talking centuries of millennia, then I can see a migration in-system where Venus and Mercury suddenly become a lot more viable for habitats as the Goldilocks Zone contracts.
You're never going to move billions of people by ship even in that space of time, so the new colonies would be primarily geared towards food production. Back on earth, those that stay on the surface would move down between the tropics of Cancer & Capricorn (as far away from the poles as possible) to escape the worst of the cold as the oceans freeze and the atmosphere thins.
Probably the most viable option is to dig down hundreds of miles though the crust and into the mantle where one might build vast underground cities that run on geo-thermal. Water won't be hard to come by as you can pipe it down from the deep oceans above. The real trick is in keeping the air down there breathable, but since you're also building domed colonies on Mercury, odds are you've cracked reliable large scale life support systems.
Figuring out how to get all those millions of tones of spoil up to the surface is no small feat either, as you're going to need to shift a lot of it to hollow out your new city caverns, trans-global tunnel network and mineral mines to actually build all these things.
Long term though: you're going to be looking to leave the system and find a nice earth-like world around a nice stable red dwarf star that's not going to burn out for a few trillion years. That's a problem unto itself and if you want a book that speculates on the inherent complications of a generational flight to even a nearby star, then I'd recommend having a look at 'Aurora' by Kim Stanley Robinson.
Scenario 2. The Sun for whatever reason heats up and expands, or explodes.
Again what do we do in this kind of situation to ensure that humanity and nature survive in some form or another?
Same as above, just a lot more urgent. If by "explode" you mean goes nova (our sun isn't massive enough to go supernova) then you're going to have to move the bulk of humanity as far into the outer system as possible, as fast as possible.
I don't know for a fact, but I imagine Jupiter would survive the initial burst of matter and energy, not sure if it's moons would be as fortunate. Maybe if any of them happen to be on the dark side of the planet when it hits they'd come out of it unscathed? Either way, the inner system is toast. Nothing but dead rocks and expanding hot gas. Titan may be a viable possibility once Sol goes all red giant on us.