Reputation: 649
I run several docker commands in gitlab-ci.yml
.
Some of them require current machine IP address to be passed to them, like this:
docker build --pull -t my_image . --add-host=<my service>:<current ip>
$CI_SERVER_HOSTNAME
is not the one, its value is gitlab.com
. I need actual IP address of the CI machine as ifconfig
would see it from .gitlab-cy.yml
file.
I am not finding any $CI_... variable for that. I know extraction from ifconfig is possible, but won't work when the docker commands executed one-by-one on Mac.
Note: I know it's usually something like 172.0.0.x, but need an exact one plus I wonder if the variable for it exists.
Upvotes: 6
Views: 6097
Reputation: 4400
In order to get the ip address of the machine that the runner is executed.
We will use the Gitlab API https://docs.gitlab.com/ee/api/runners.html#get-runners-details
GET /runners/:id
This API call will return the details of the runner with this specific :id. When a job executes this id is available on CI_RUNNER_ID predefined variable.
By combining all this and utilizing jq
and sed
We get the following one liner that returns the ip address of the runner that is executing the current job
curl -s --header "PRIVATE-TOKEN: <your access token>" https://gitlab.com/api/v4/runners/${CI_RUNNER_ID} | jq '.ip_address' | sed 's/^"\(.*\)"$/\1/'
Upvotes: 1