Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Git & GitHub - GitHub Packages

How to use GitHub Packages

GitHub Packages is a service that allows you to host and manage packages and container images alongside your source code on GitHub. This guide explains how to use GitHub Packages to publish, manage, and use packages in your projects.

Key Points:

  • GitHub Packages supports various package formats, including npm, Maven, NuGet, RubyGems, and Docker.
  • Packages are integrated with GitHub repositories, making it easy to associate code and packages.
  • Access control and permissions are managed through your GitHub repository settings.

Publishing a Package

Step 1: Authenticate with GitHub Packages

To publish a package, you need to authenticate with GitHub Packages using a personal access token or the GitHub CLI. For npm, update your .npmrc file:


# Update .npmrc with authentication token
//npm.pkg.github.com/:_authToken=YOUR_PERSONAL_ACCESS_TOKEN
                

Step 2: Configure Package Settings

Configure your package settings in the package.json file for npm:


{
  "name": "@username/package-name",
  "version": "1.0.0",
  "repository": {
    "type": "git",
    "url": "git+https://github.com/username/repository-name.git"
  },
  "publishConfig": {
    "registry": "https://npm.pkg.github.com/"
  }
}
                

Step 3: Publish the Package

Publish your package to GitHub Packages using the npm publish command:


# Publish the package
$ npm publish
                

Your package will be available in the GitHub repository under the "Packages" section.

Using a Package

Step 1: Authenticate with GitHub Packages

To use a package from GitHub Packages, authenticate by updating your .npmrc file:


# Update .npmrc with authentication token
//npm.pkg.github.com/:_authToken=YOUR_PERSONAL_ACCESS_TOKEN
                

Step 2: Install the Package

Install the package in your project using the npm install command:


# Install the package
$ npm install @username/package-name
                

Managing Packages

You can manage your packages from the "Packages" section in your GitHub repository. Here you can view package details, download statistics, and manage versions.

GitHub Packages Overview

To deprecate or delete a package, navigate to the package page and use the options available under the "Package settings" dropdown.

Using GitHub Packages with Docker

Step 1: Authenticate with GitHub Packages

To use GitHub Packages with Docker, authenticate using the GitHub CLI:


# Authenticate with GitHub Packages
$ echo $CR_PAT | docker login ghcr.io -u USERNAME --password-stdin
                

Step 2: Build and Tag Your Image

Build and tag your Docker image:


# Build and tag the image
$ docker build -t ghcr.io/username/image-name:tag .
                

Step 3: Push the Image

Push the image to GitHub Packages:


# Push the image
$ docker push ghcr.io/username/image-name:tag
                

Summary

This guide covered how to use GitHub Packages, including publishing and using packages, managing package settings, and using GitHub Packages with Docker. GitHub Packages integrates seamlessly with your GitHub repository, making it easy to manage and distribute packages and container images alongside your code.