Before getting your specific example, we should specify a few things. First, there's more to Kant than just the Groundwork of the Metaphysics of Morals (you called it "Foundations" -- let's stick with that). There's also the Metaphysics of Morals itself ("MM"), and of particular concern to us, the Tugendlehre (which the second half of "MM") which is often translated "Doctrine of Virtue" or "Metaphysical Principle of Virtue" (we'll say "MPV").
Sticking just to the Foundation, the formula of humanity is paired with the formula of rationality. The idea behind both is to:
treat rationality whether in your own person or another as an end and never merely as a means.
Translated a bit, this means that you can never make yourself or anyone a mere tool to an end. (The mere is important but not particularly for your question).
Killing yourself is for Kant in the Foundation always wrong, because you are ending rationality for something else -- and ending it absolutely.
Moving to MPV, Kant has a bit more to say about killing yourself and whether suicide is wrong. The basic framework remains, but Kant does seem to find exceptions for bravely dying for your country and such things.'
BUT it's doubtful Kant would extend that to the example you give. There's two related reasons for this:
- Kant believes the categorical imperative is a single united moral principle that should guide all of our action. And this is merely the application of pure reason to the domain of action.
- Kant (as the Religion within the Bounds of Reason Alone text makes very clear) does not think God could give a command that conflicts with the CI. Instead, God tells us what we would already tell ourselves.
In other words, for Kant, the conflict you describe is impossible, because Kant would not accept that God gives such a command. (In the Religion, Kant explains that Abraham is completely mistaken in believing that God commanded him to kill Isaac (see for instance here)).