Getting Started with GitHub Container Registry

為什麼會寫這篇文章

隨著公司專案數量增加,每個專案的環境需求也變得更加多樣化。我們決定將原本使用 Docker 建置的 Android Jenkins Server 轉型為更靈活的架構:一個主要的 Jenkins Server(Master)搭配多個 Android Build Environment(Slave),後者透過 Docker 創建乾淨的環境。這篇文章旨在記錄此過程,不僅作為個人學習的回顧,也希望能對其他開發者提供幫助。


文章簡介

本文將引導初學者及希望深入了解如何將 GitHub 的新工具融入 CI/CD 流程的開發者,透過簡明的指南和實用的技巧,學習如何將容器映像推送至 GitHub Container Registry。我將一步步展示如何設定 GitHub Actions,自動化構建與部署過程,讓你的開發工作變得更加高效。


開始之前

在深入主題之前,讓我們先透過 express 框架,快速搭建一個運行於 Node.js 上的簡易應用。


Create a node_sample folder

mkdir node_sample
cd node_sample

Install node package express

npm init -y
npm install express

Create an app.js file

vim app.js
const express = require("express");
const app = express();
const port = 3000;

app.get("/", (req, res) => {
  res.send("Hello, World!");
});

app.listen(port, () => {
  console.log(`Example app listening at http://localhost:${port}`);
});

Create a .gitignore file

TIP

請至 gitignore.io 產生 .gitignore 檔案,選擇 Node 類型即可。


Run app.js

node app.js

在 Chrome 上打開網址 localhost:3000 就會看到如下:


Create a Dockerfile

vim Dockerfile
FROM node:latest
WORKDIR /usr/src/app
COPY package*.json app.js ./
RUN npm install
EXPOSE 3000
CMD ["node", "app.js"]

上傳 Docker Image 到 Github Container Registry

寫完 Dockerfile 後有兩種方式可以將 image 上傳到 GitHub Container Registry:

  1. 用 Command line 手動上傳
  2. 使用 GitHub Actions 自動化上傳

用 Command line 方式手動上傳


利用 Dockerfile 產生 image

docker build -t node_sample .

查看 Docker images

docker images

創建 image tag

docker tag node_sample:latest ghcr.io/{NAMESPACE}/node_sample:latest
TIP

記得將 {NAMESPACE} 替換為你的 GitHub 帳號名稱。


Generate Personal access tokens (classic)

登入 GitHub Container Registry 前,需要生成一組 GITHUB_TOKEN

打開 GitHub → 右上角 Profile → Settings → Developer settings → Personal access tokens → Tokens (classic) → Generate new token

勾選 write:packagesread:packagesdelete:packages 權限:


登入 GitHub Container Registry

export CR_PAT=YOUR_TOKEN
echo $CR_PAT | docker login ghcr.io -u USERNAME --password-stdin

Login Succeeded 🎉


上傳 Images

docker push ghcr.io/{NAMESPACE}/node_sample:latest

用 GitHub Action 方式

使用 GitHub Action 的方式更簡單,在 node_sample 目錄下:


Create a deploy-image.yml

mkdir -p .github/workflows
vim .github/workflows/deploy-image.yml
name: Create and publish a Docker image
on:
  push:
    branches: ["release"]
env:
  REGISTRY: ghcr.io
  IMAGE_NAME: $

jobs:
  build-and-push-image:
    runs-on: ubuntu-latest
    permissions:
      contents: read
      packages: write
      attestations: write
      id-token: write
    steps:
      - name: Checkout repository
        uses: actions/checkout@v4
      - name: Log in to the Container registry
        uses: docker/login-action@v2
        with:
          registry: $
          username: $
          password: $
      - name: Extract metadata (tags, labels) for Docker
        id: meta
        uses: docker/metadata-action@v4
        with:
          images: $/$
      - name: Build and push Docker image
        id: push
        uses: docker/build-push-action@v5
        with:
          context: .
          push: true
          tags: $
          labels: $
      - name: Generate artifact attestation
        uses: actions/attest-build-provenance@v1
        with:
          subject-name: $/$
          subject-digest: $
          push-to-registry: true

Pushing node_sample repository

將 node_sample 推到 GitHub,之後每次 release 分支 push 變更時就會自動觸發 GitHub Action,並完成 image 上傳。


下載 image


Install from the command line

docker pull ghcr.io/nickhuangcyh/node_sample:TAG

Use as base image in Dockerfile

FROM ghcr.io/nickhuangcyh/node_sample:TAG
TIP

您可於此 node_sample 下載 node_sample 的程式碼。




    Enjoy Reading This Article?

    Here are some more articles you might like to read next:

  • 💡 一台電腦操作多個 GitHub 帳號:最簡單快速的 SSH 設定方法
  • 🚀 如何使用 Excalidraw AI 快速生成專業級圖表,提升工作效率!
  • Setup Development Environment on a New macOS
  • Design Pattern (28) - Interpreter Pattern (解譯器模式)
  • Design Pattern (27) - Visitor Pattern (訪問者模式)