You're only using the task variable. The task variable will only hold task specific task variables. You'll need to get the workflow object, then set the variable in the workflow object instead.
Ideally we can only re-assign value to a Job variable from an external task script, i.e. we can't add a new job variable from the external task script directly.