从GitLab到Jenkins的Web钩子不起作用,但令牌正常

时间:2018-07-13 23:39:23

标签: jenkins gitlab

几个小时以来,我无法成功触发从本地GitLab安装到Jenkins的Web Hook(实际上两个都在Docker容器中运行)。这是我已采取的步骤(我决定这样写,因为它会包含很多屏幕截图):

DOCKER_NETWORK="ci_cd_network"
docker network create ${DOCKER_NETWORK}

CONTAINER_NAME="jenkins_new"
mkdir -p /srv/${CONTAINER_NAME}

docker run -u root --rm -d \
  -p 8081:8080 -p 50000:50000 \
  -v /srv/${CONTAINER_NAME}:/var/jenkins_home \
  -v /var/run/docker.sock:/var/run/docker.sock \
  -v /etc/timezone:/etc/timezone:ro \
  -v /etc/localtime:/etc/localtime:ro \
  -v /root/.ssh:/root/.ssh \
  --name ${CONTAINER_NAME} \
  --hostname ${CONTAINER_NAME} \
  --network ${DOCKER_NETWORK} \
  jenkinsci/blueocean


CONTAINER_NAME="gitlab_new"
mkdir -p /srv/${CONTAINER_NAME}/config
mkdir -p /srv/${CONTAINER_NAME}/logs
mkdir -p /srv/${CONTAINER_NAME}/config

docker run --detach \
    --hostname ${CONTAINER_NAME} \
    --env GITLAB_OMNIBUS_CONFIG="external_url 'http://${CONTAINER_NAME}';" \
    --env GITLAB_OMNIBUS_CONFIG="time_zone 'Europe/Bratislava';" \
    --publish 8080:80 --publish 2222:22 \
    --name ${CONTAINER_NAME} \
    --network ${DOCKER_NETWORK} \
    --volume /srv/${CONTAINER_NAME}/config:/etc/gitlab \
    --volume /srv/${CONTAINER_NAME}/logs:/var/log/gitlab \
    --volume /srv/${CONTAINER_NAME}/data:/var/opt/gitlab \
    --volume /etc/timezone:/etc/timezone:ro \
    --volume /etc/localtime:/etc/localtime:ro \
    --volume /root/.ssh:/root/.ssh \
    gitlab/gitlab-ce:latest


Gitlab web interface:
#####################
1. Profile -> Settings -> SSH Keys -> [content of ~/.ssh/id_rsa.pub]
2. Profile -> Settings -> Access Tokens -> [kWny_LCeV_ehVoQVq54L]
3. Create Project -> Private Project -> hollo-world


Jenkins web interface:
######################

1. Install Suggested Plugins
2. Manage Jenkins -> Plugins -> Install -> Gitlab
3. Credentials -> System -> Global credentials (unrestricted) -> Add credentials:
a.)
Kind: SSH User Name With Private Key
Username: root
Enter Directly:
[content of ~/.ssh/id_rsa in form:
-----BEGIN RSA PRIVATE KEY-----
[private key]
-----END RSA PRIVATE KEY-----]
id: ssh-root-private

b.)
Kind: SSH User Name With Private Key
Username: root
Enter Directly: [blah blah for debugging purposes]
id: ssh-root-private-wrong

c.)
Kind: GitLab API token
API token: [paste token from GitLab: kWny_LCeV_ehVoQVq54L]
id: gitlab-api-token

d.)
Kind: GitLab API token
API token: [blah blah for debugging purposes]
id: gitlab-api-token-wrong


4. Manage Jenkins -> Configure System -> Gitlab
a.) Tried but not saved
Connection name: gitlab_new
Gitlab host URL: http://gitlab_new
Credentials: gitlab-api-token-wrong
Test Connection: Client error: HTTP 401 Unauthorized

b.) Tried and saved
Connection name: gitlab_new
Gitlab host URL: http://gitlab_new
Credentials: gitlab-api-token
Test Connection: Success


5. New Item -> Freestyle project
6. Source Code Management
Git: Repository URL: git@gitlab_new:root/hello-world.git
a.) Tried but not saved
Credentials: ssh-root-private-wrong
Following text appears:
Failed to connect to repository : Command "git ls-remote -h git@gitlab_new:root/hello-world.git HEAD" returned status code 128:
stdout:
stderr: Load key "/tmp/ssh6373409174430186701.key": invalid format
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

b.) Tried and saved
Credentials: ssh-root-private


7. Build Triggers
Build when a change is pushed to GitLab. GitLab webhook URL: http://localhost:8081/project/freestyle-hello-world



Jenkins bash:
#############
bash-4.4# # SET GIT CREDENTIALS
bash-4.4# git config --global user.email "root@root.com"
bash-4.4# git config --global user.name "root"
bash-4.4# # CLONE GIVEN REPO
bash-4.4# git clone git@gitlab_new:root/hello-world.git
bash-4.4# cd hello-world
bash-4.4# # MAKE FIRST COMMIT
bash-4.4# echo "1 commit" >> commit_history.txt
bash-4.4# git add .
bash-4.4# git commit -m "1 commit"
bash-4.4# git push

Gitlab bash:
############
root@gitlab_new# # SET GIT CREDENTIALS
root@gitlab_new# git config --global user.email "root@root.com"
root@gitlab_new# git config --global user.name "root"
root@gitlab_new# # CLONE GIVEN REPO
root@gitlab_new# git clone git@gitlab_new:root/hello-world.git
root@gitlab_new# cd hello-world
root@gitlab_new# # MAKE SECOND COMMIT
root@gitlab_new# echo "2 commit" >> commit_history.txt
root@gitlab_new# git add .
root@gitlab_new# git commit -m "2 commit"
root@gitlab_new# git push

Jenkins bash:
#############
bash-4.4# git # pull changes made from GitLab docker
bash-4.4# git pull


Jenkins web interface:
######################
1. freestyle-hello-world -> Build Now -> OK


Gitlab web interface:
#####################
1.
a) Admin Area -> Settings -> Outbound requests -> Allow requests to the local network from hooks and services -> Disabled
b) Projects -> hello-world -> Settings -> Integrations -> Webhooks Test -> Push event
URL: http://jenkins:8080/project/freestyle-hello-world
Secret Token: [Empty]
Trigger: Push events
Enable SSL verification: False
Result: Hook execution failed: URL 'http://jenkins:8080/project/freestyle-hello-world' is blocked: Requests to localhost are not allowed

2.
a) Admin Area -> Settings -> Outbound requests -> Allow requests to the local network from hooks and services -> Enabled
b) Projects -> hello-world -> Settings -> Integrations -> Webhooks Test -> Push event
URL: http://jenkins:8080/project/freestyle-hello-world
Secret Token: [Empty]
Trigger: Push events
Enable SSL verification: False
Result:

Hook executed successfully but returned HTTP 404 <!DOCTYPE html> <html> <head> <meta content="width=device-width, initial-scale=1, maximum-scale=1" name="viewport"> <title>The page you're looking for could not be found (404)</title> <style> body { color: #666; text-align: center; font-family: "Helvetica Neue", Helvetica, Arial, sans-serif; margin: auto; font-size: 14px; } h1 { font-size: 56px; line-height: 100px; font-weight: 400; color: #456; } h2 { font-size: 24px; color: #666; line-height: 1.5em; } h3 { color: #456; font-size: 20px; font-weight: 400; line-height: 28px; } hr { max-width: 800px; margin: 18px auto; border: 0; border-top: 1px solid #EEE; border-bottom: 1px solid white; } img { max-width: 40vw; display: block; margin: 40px auto; } a { line-height: 100px; font-weight: 400; color: #4A8BEE; font-size: 18px; text-decoration: none; } .container { margin: auto 20px; } .go-back { display: none; } </style> </head> <body> <img src="data:image/svg+xml;base64,PHN2ZyB3aWR0aD0iMjEwIiBoZWlnaHQ9IjIxMCIgdmlld0JveD0iMCAwIDIxMCAyMTAiIHhtbG5zPSJodHRwOi8vd3d3LnczLm9yZy8yMDAwL3N2ZyI+CiAgPHBhdGggZD0iTTEwNS4wNjE0IDIwMy42NTVsMzguNjQtMTE4LjkyMWgtNzcuMjhsMzguNjQgMTE4LjkyMXoiIGZpbGw9IiNlMjQzMjkiLz4KICA8cGF0aCBkPSJNMTA1LjA2MTQgMjAzLjY1NDhsLTM4LjY0LTExOC45MjFoLTU0LjE1M2w5Mi43OTMgMTE4LjkyMXoiIGZpbGw9IiNmYzZkMjYiLz4KICA8cGF0aCBkPSJNMTIuMjY4NSA4NC43MzQxbC0xMS43NDIgMzYuMTM5Yy0xLjA3MSAzLjI5Ni4xMDIgNi45MDcgMi45MDYgOC45NDRsMTAxLjYyOSA3My44MzgtOTIuNzkzLTExOC45MjF6IiBmaWxsPSIjZmNhMzI2Ii8+CiAgPHBhdGggZD0iTTEyLjI2ODUgODQuNzM0Mmg1NC4xNTNsLTIzLjI3My03MS42MjVjLTEuMTk3LTMuNjg2LTYuNDExLTMuNjg1LTcuNjA4IDBsLTIzLjI3MiA3MS42MjV6IiBmaWxsPSIjZTI0MzI5Ii8+CiAgPHBhdGggZD0iTTEwNS4wNjE0IDIwMy42NTQ4bDM4LjY0LTExOC45MjFoNTQuMTUzbC05Mi43OTMgMTE4LjkyMXoiIGZpbGw9IiNmYzZkMjYiLz4KICA8cGF0aCBkPSJNMTk3Ljg1NDQgODQuNzM0MWwxMS43NDIgMzYuMTM5YzEuMDcxIDMuMjk2LS4xMDIgNi45MDctMi45MDYgOC45NDRsLTEwMS42MjkgNzMuODM4IDkyLjc5My0xMTguOTIxeiIgZmlsbD0iI2ZjYTMyNiIvPgogIDxwYXRoIGQ9Ik0xOTcuODU0NCA4NC43MzQyaC01NC4xNTNsMjMuMjczLTcxLjYyNWMxLjE5Ny0zLjY4NiA2LjQxMS0zLjY4NSA3LjYwOCAwbDIzLjI3MiA3MS42MjV6IiBmaWxsPSIjZTI0MzI5Ii8+Cjwvc3ZnPgo=" alt="GitLab Logo" /> <h1> 404 </h1> <div class="container"> <h3>The page could not be found or you don't have permission to view it.</h3> <hr /> <p>The resource that you are attempting to access does not exist or you don't have the necessary permissions to view it.</p> <p>Make sure the address is correct and that the page hasn't moved.</p> <p>Please contact your GitLab administrator if you think this is a mistake.</p> <a href="javascript:history.back()" class="js-go-back go-back">Go back</a> </div> <script> (function () { var goBack = document.querySelector('.js-go-back'); if (history.length > 1) { goBack.style.display = 'inline'; } })(); </script> </body> </html>

TL / DR:

这有效:

  1. Jenkins和GitLab的docker容器
  2. 容器之间的通信(ping,访问端口等)
  3. 从两个容器中拉出/推入GitLab容器(使用SSH密钥)
  4. 在GitLab和Jenkins之间进行git pull(使用SSH密钥)
  5. 使用GitLab令牌从GitLab到Jenkins的连接
  6. Jenkins和GitLab之间的连接仅在使用正确的SSH密钥和GitLab令牌的情况下起作用

这不起作用:

  1. 从GitLab到Jenkins的Web钩子

配置:

  1. GitLab社区版10.8.1
  2. 詹金斯2.107.3
  3. Jenkins GitLab插件1.5.8

非常感谢您

0 个答案:

没有答案