Moving artifact from Gitlab CI to project Wiki page

3 min read 07-10-2024
Moving artifact from Gitlab CI to project Wiki page


Moving Artifacts from GitLab CI to Project Wiki Pages: A Streamlined Approach

The Problem:

Have you ever built a report, generated documentation, or created a large file in your GitLab CI pipeline and wished you could easily share it with your team? You might have thought about simply storing it in the project directory, but that's not always ideal. It can clutter the repository and complicate version control.

The Solution:

Move your artifacts to the project Wiki page! This approach provides a dedicated, well-organized space to store and access important project information. This article will guide you through the process of transferring your artifacts and creating a structured Wiki page for easy access.

Setting the Stage:

Imagine you're building a Python application and generating a comprehensive code coverage report as part of your CI pipeline. You want to make this report readily available for your team to analyze and improve code quality.

Let's look at a sample .gitlab-ci.yml file:

stages:
  - build
  - test
  - report

build:
  stage: build
  script:
    - pip install pytest pytest-cov
  artifacts:
    paths:
      - coverage.xml

test:
  stage: test
  script:
    - pytest --cov=my_app --cov-report=xml:coverage.xml

report:
  stage: report
  script:
    - echo "Generating report..."
    - coverage html
  artifacts:
    paths:
      - htmlcov/

This pipeline builds the project, runs tests, and generates a code coverage report (stored in coverage.xml). It then creates a htmlcov directory with an interactive HTML report.

Transferring the Artifacts:

Instead of keeping the htmlcov directory in the repository, we can upload it to the project Wiki. Let's modify our .gitlab-ci.yml file:

stages:
  - build
  - test
  - report

build:
  stage: build
  script:
    - pip install pytest pytest-cov
  artifacts:
    paths:
      - coverage.xml

test:
  stage: test
  script:
    - pytest --cov=my_app --cov-report=xml:coverage.xml

report:
  stage: report
  script:
    - echo "Generating report..."
    - coverage html
  artifacts:
    paths:
      - htmlcov/
  script:
    - echo "Uploading report to Wiki..."
    - curl -X POST -H "PRIVATE-TOKEN: $CI_API_TOKEN" -F "file=@htmlcov" "https://gitlab.com/your-username/your-project/-/wikis/uploads" 

Here's the breakdown of the new report stage:

  1. Uploading to Wiki: We use curl to make an API request to upload the htmlcov directory to the project Wiki.
  2. API Token: We use the CI_API_TOKEN environment variable, which is automatically provided by GitLab CI to authenticate the request.
  3. File Upload: The -F "file=@htmlcov" part uploads the entire htmlcov directory as a file.

Important Notes:

  • API Token: Make sure your CI_API_TOKEN variable has sufficient permissions to upload files to the Wiki. You can configure this in your project settings under "CI/CD" -> "Variables."
  • URL: Replace https://gitlab.com/your-username/your-project/-/wikis/uploads with the correct upload URL for your project. You can find this URL in your project's Wiki settings.

Organizing the Wiki:

Now that your report is uploaded, you can organize it into a dedicated Wiki page. Create a new Wiki page titled "Code Coverage Reports" and paste the following content:

## Code Coverage Reports

This page provides an overview of code coverage for our project. 

[Click here to view the latest coverage report](https://gitlab.com/your-username/your-project/-/wikis/uploads/htmlcov/index.html)

**Note:**  The link above will open the interactive HTML report in a new tab.

This page provides context and a clear link to the uploaded report, making it easily accessible to your team.

Benefits of Using Wiki:

  • Centralized Location: The Wiki page provides a single, dedicated space for all your project artifacts, making them easy to locate and share.
  • Version Control: Each upload to the Wiki is versioned, allowing you to track changes over time.
  • Easy Access: Team members can access the Wiki pages directly through the project interface without needing to clone the repository.
  • Simplified Repository: Keeping artifacts outside of the main repository keeps your codebase clean and focused on source code.

Conclusion:

Moving artifacts to your project Wiki page offers a streamlined and efficient way to share important project information. By leveraging the GitLab CI and Wiki integration, you can create a well-organized and accessible hub for your project's documentation and reports.