Context
A registry with a deny-by-default firewall needs the hosted CI agent’s public IP allowed before the agent can push to it. The pipeline did this at the start of the job:
- looked up its public IP through an IP echo service
- added the IP as a firewall rule
- confirmed with a registry login that the rule worked
Then came several minutes of image builds and vulnerability scans, none of which needed the registry. The first push request failed with 403 Forbidden. Nothing had changed the registry in the meantime, so the agent most likely sent the push from a different public IP than the one it had reported earlier.
Insight
Keep the window between adding the rule and pushing as short as possible, and make the push step say which IP it actually used:
- add the firewall rule directly before the push steps, after builds and scans
- log the agent’s current IP next to the allowed one in each push step, and warn if they differ
- retry a failed push a couple of times
- if it’s still unreliable, use an agent with a fixed outgoing IP, or one that reaches the registry through a private endpoint, so no IP rules are needed
Snippet
for attempt in 1 2 3; do current_ip="$(curl -fsS --max-time 10 https://api.ipify.org || echo unknown)" echo "push ${attempt}/3: agent IP now ${current_ip}, allowed ${AGENT_IP:-unknown}" docker push "$IMAGE" && exit 0 sleep 15doneexit 1