Packages and CI/CD
July 12, 2024 — 19:41

Author: silver  Category: dev  Comments: Off

Building packages for 3rd party code with CI/CD in GitHub or GitLab.

Besides building application packages, you might want to create your own (custom) OS packages – for example in rpm or deb format. Which can then be tested and deployed. Using GitHub Actions or GitLab’s CI & API, creating packages can easily be automated. And that’s what this post is about.

First make sure we get source code. GitLab supports Repository mirroring and GitHub has a checkout action and several actions available in Marketplace that fully mirror repos. Also if there is no method yet to build the code and create a package, we need to create our own build script (in a new branch). Now a build job can run, either on a runner or using build image and container.

GitHub

Add job to workflow to get build artifacts and create release using ‘gh’ cli tool

Example:

jobs:
# ...
   create-release:
     name: Get artifacts and create release
     runs-on: ubuntu-latest
     steps:
       - uses: actions/checkout@v4
       - name: Download artifacts
         uses: actions/download-artifact@v4
         with:
           path: build-artifacts
       - name: Create release using 'gh'
          run: |
            gh release create mypkg-v$(date +%Y%m%d) $(find -type f -printf '%p ') --notes "my release  notes"
         working-directory: ./build-artifacts
         env:
           GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}

GitLab

Ensure build artifacts are stored earlier in your pipeline and add a job that uploads and publish to package registry

Example:

# ...
build_job:
  stage: build
  #...
  artifacts:
      name: my-build-artifacts
      paths:
        - "build/example*"

publish_job:
  stage: publish
  dependencies: build_job
  script:
    - '/usr/bin/curl --silent
      --header "JOB-TOKEN: $CI_JOB_TOKEN"
      --upload-file "$FILE_NAME"
      "https://instance-or-gitlab.com/projects/${CI_PROJECT_ID}/packages/generic/my_pkg/$(date +%Y%m%d)/${FILE_NAME}?status=default" 
      grep -q "201 Created" || exit 1'

For “versioning” we’ve simply used the current date, this probably should be a git tag or extracted from source. The next steps are adding a job to test the package and to deploy it (push). To be able to pull instead, you could set up your own repository. You could also consider using SaaS like packagecloud.io, artifactory or Ubuntu’s PPA to distribute the package.








We use Matomo free and open source web analytics
We also use Jetpack WordPress.com Stats which honors DNT