0

I have Jenkins in a docker container and inside the container I have a local git repository, I added a post-commit file which runs a build in Jenkins each time a commit is made but the file doesn't run, what is the problem?

this is the post commit file (it's in the container)

Jenkins which is running inside the docker container on localhost

Louis Smith
  • 73
  • 1
  • 1
  • 9

1 Answers1

1

I see this mistake over and over again.

localhost is always resolved to the 127.0.0.1 of whatever machine it's used on. In context of the container 127.0.0.1 is the container itself, not the machine Docker is running on.

If both Jenkins and Git container use the same network, you can use Jenkins's container IP in your hook URL. Otherwise you need to put them on the same network first - see this question on how to do that.

Konrad Botor
  • 4,765
  • 1
  • 16
  • 26
  • the file itself is not executing, when i execute git commit on the local git directory (which is inside the jenkins container ) the echo commands are not executed – Louis Smith Aug 11 '20 at 13:39
  • So the post-commit hook is inside Git container and the commit is made from within Jenkins container? Then why would you assume it would work? Post-commit hook is a client-side hook (see https://git-scm.com/book/en/v2/Customizing-Git-Git-Hooks for details). Jenkins would need to create it by itself before commiting anything. – Konrad Botor Aug 11 '20 at 13:46
  • i don't have a git container i only have a jenkins container and i cloned the repository inside it – Louis Smith Aug 11 '20 at 13:51
  • Are you executing the commit manually or from Jenkins job? Also where is the post-commit script located and what is it's filename? – Konrad Botor Aug 11 '20 at 13:54
  • i'm executing the commit manually in the local repository inside jenkins docker. the post-commit file is in .git/hooks its name is post-commit – Louis Smith Aug 11 '20 at 13:57
  • Is it executable? – Konrad Botor Aug 11 '20 at 14:02
  • it wasn't, I solved it, thank u, but it gives "connection refused" error – Louis Smith Aug 12 '20 at 07:12
  • By default, inside the container Jenkins listens on port 8080 and you're sending your request to port 8081. – Konrad Botor Aug 12 '20 at 07:20
  • in the Jenkins container i set the ports to 8081:8081 what should i set them to? – Louis Smith Aug 12 '20 at 07:31