a_guest
a_guest

Reputation: 36249

How to host a project and its GitLab page under the same namespace?

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?

Upvotes: 0

Views: 1204

Answers (1)

Jawad
Jawad

Reputation: 4665

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 [email protected]: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.

Upvotes: 1

Related Questions