0

I have a group named groupname and within that group a project named projectname. The group has a GitLab page which is available at http(s)://groupname.gitlab.io/.

Now I want to create a GitLab page for this project which should be available at http(s)://groupname.gitlab.io/projectname. In order to make this happen - as far as I understood from the docs - the gitlab-page-project needs to be hosted under the group and it's name needs to be projectname. But that's already the name of the actual project the page refers to!

While I could host the project under a different path - say groupname/projectname-doc - and still rename it to projectname it will make both projects show up with the same name in the group's project overview. Am I missing something here or is this the recommended way to host both a project and its GitLab page under the same namespace?

a_guest
  • 34,165
  • 12
  • 64
  • 118
  • From what I understand in the docs, you should have a `pages` job in the `.gitlab-ci.yml` file of your already existing project. That means you have to have the same version control for the project and its website. It doesn't seem very practical to me... – Jawad Apr 11 '17 at 20:17
  • This is not possible for me as the master branch of my project reflects the most recent release and I can't release a new version each time I modify the project's documentation. I can't believe this was the intention of the GitLab developers. Or do they mean to only push the `yaml` file in the original project and have the website's source code in a different repo which is somehow linked to the job specified in the `yaml`? I can't figure how this would be realized then. – a_guest Apr 11 '17 at 20:29
  • The way I would do it would be through `Deploy Keys` and maybe a webhook. Basically add a deploy key to your website project then add a private variable to your actual project and add a `pages` job in your yaml file that will pull the website project and deploy it. Then you could setup a webhook in order to run your main project's CI pipeline everytime you push to your website project. I can write a full fledged answer tomorrow morning if you're interested (not really practical now as I'm on my phone...) here's a pointer in the meantime http://stackoverflow.com/a/43311371/6654146 – Jawad Apr 11 '17 at 20:54
  • @Jawad That would be great as I'm not familiar with deploy keys and how to setup that webhook. All in all that means the pages job will pull the website project onto the build server and use its content for the build process? Where exactly does the build process take place? Within the actual project's repo then? – a_guest Apr 12 '17 at 12:23

1 Answers1

1

Here's a follow up to the discussion we had in the comments to your question.

Setting up the deploy keys and the pipeline

First generate a SSH key pair. You can use ssh-keygen -t rsa for that.

Then create a gitlab project for the page, let's call it projectname-docs. In this project, locate the Deploy Keys setting. There you should paste the public key you just generated.

Then go to projectname and locate the Variables page. Create a new private variable with the name SSH_PRIVATE_KEY for instance and paste the private key you generated there.

In your .gitlab-ci.yml file in the projectname project, add the following so that your private key will be available to your CI environment:

pages:
  stage: deploy
  script:
    - 'which ssh-agent || ( apt-get update -y && apt-get install openssh-client -y )'
    # Run ssh-agent (inside the build environment)
    - eval $(ssh-agent -s)
    # Add the SSH key stored in SSH_PRIVATE_KEY variable to the agent store
    - ssh-add <(echo "$SSH_PRIVATE_KEY")
    - mkdir -p ~/.ssh
    - '[[ -f /.dockerenv ]] && echo -e "Host *\n\tStrictHostKeyChecking no\n\n" > ~/.ssh/config'
    - git clone git@gitlab.com:groupname/projectname-docs.git
    - cd projectname-docs
    - mkdir ~/.public
    - cp -r * ~/.public
    - cd ../
    - mv ~/.public public
  artifacts:
    paths:
      - public

At this point, every time you push something to your main project, it will pull the projectname-docs project and deploy it using Gitlab pages. The docs should then be available.

Setting up the webhook

What you may want, is to be able to run the main project's pipeline every time you push to projectname-docs so the web page is updated.

One way to do this is through webhooks.

First go to your main project's gitlab page and go to Settings -> CI/CD Pipelines -> Triggers and click the Add Trigger button. This will create a new token we'll use later.

Then, go to projectname-docs and navigate to Settings -> Integrations and insert the following for the URL:

http://gitlab.com/api/v4/projects/ID/ref/REF_NAME/trigger/pipeline?token=TOKEN

Where ID is the id of projectname, REF_NAME is the name of the branch or tag to run the pipeline for (e.g master) and TOKEN is the token you generated in the previous step.

Make sure the push event is selected and add the webhook.

Jawad
  • 4,457
  • 1
  • 26
  • 29
  • Thanks for the detailed answer! So if I understood it correctly the website is served from `~/public` and this is where all my HTML should go? Because I'm building from Sphinx and thus I need to know where to copy the build. – a_guest Apr 12 '17 at 14:51
  • The HTML needs to be in the root of the `projectname-docs` git repo. Then the Ci will copy everything to the `public` directory. You can put your HTML somewhere else but then change `cp -r * ~/.public` to copy from the correct location. So at the end of the CI pipeline, yes, the HTML needs to be in `~/public` – Jawad Apr 12 '17 at 14:56
  • All right because I plan to have my documentation source in `projectname-docs` and to add an additional build step to the job (+ followed by copying the resulting files to `~/public`). Thanks! – a_guest Apr 12 '17 at 14:58
  • I tried to follow your approach and ran the pipeline however I get a `Permission denied (publickey). fatal: Could not read from remote repository. Please make sure you have the correct access rights and the repository exists.` I added both public and private key as you mentioned. Also I don't quite understand why the project is cloned from `git@gitlab.io:groupname/projectname-docs.git`. I tried changing it to `git@gitlab.com:groupname/projectname-docs.git` however I get the same error message. Adding the private key the response was `Identity added: /dev/fd/63 (rsa w/o comment)`. @Jawad – a_guest Apr 13 '17 at 19:15
  • Nevermind, turned out that the public key has been deleted. Actually I wanted to re-add it however deleting it failed with an error message, so I supposed it was still there. Apparently it had been deleted though and this just showed up in the GUI later. – a_guest Apr 13 '17 at 19:23
  • Just changed gitlab.io to gitlab.com in the answer for future reference. – Jawad Apr 13 '17 at 19:54
  • To get the page served apparently the HTML content must be placed in a `public` folder which resides in the project's directory (not in the home directory). See [this article](https://docs.gitlab.com/ee/user/project/pages/getting_started_part_four.html#the-public-directory). If the content is placed in a different folder the page won't be served even if you specify that other location under `artifacts`. Also all paths specified under `artifacts` seem to be relative to the `.gitlab.ci.yml` file which resides in the projects directory too. – a_guest Apr 14 '17 at 01:17