GitHub Actions में FreeBSD का उपयोग करना
सर्वर
Published: 2024-12-22

यह लेख साकुरा इंटरनेट एडेवेंट कैलेण्डर 2024 के 22वें दिन का लेख है।

शुरूआत

कभी-कभी, आप GitHub Actions में FreeBSD का उपयोग करना चाहते हैं।
वैसे, साकुरा इंटरनेट में FreeBSD का उपयोग किया जा रहा है, इसलिए आप इसे किसी भी तरह से इस्तेमाल करना चाहते हैं।

वर्तमान में, मैं SSH के माध्यम से Jenkins और FreeBSD वातावरण का उपयोग कर रहा हूँ और बिल्ड कर रहा हूँ, लेकिन
यदि यह GitHub Actions में किया जा सके तो यह बेहतर होगा।

क्रॉस-प्लेटफ़ॉर्म GitHub Action का उपयोग करना

क्रॉस-प्लेटफ़ॉर्म GitHub Action का उपयोग करने से, आप FreeBSD का उपयोग कर सकते हैं।
यह GitHub Actions पर Qemu का उपयोग करके अन्य OS को VM के रूप में चलाने की सुविधा प्रदान करता है।

यह एक चुनौतीपूर्ण दृष्टिकोण है, लेकिन GitHub Actions में FreeBSD का उपयोग करना बहुत उपयोगी है।

Actions बनाना

सबसे पहले, क्रॉस-प्लेटफ़ॉर्म GitHub Action का उपयोग करने के लिए एक रेपो बनाएं।

$ git init actions_freebsd
$ cd actions_freebsd

इसके बाद, .github/workflows निर्देशिका बनाएं और उसमें main.yml बनाएँ।

$ mkdir -p .github/workflows
$ vi .github/workflows/main.yml

main.yml में निम्नलिखित सामग्री लिखें।

name: FreeBSD

on: push

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: test freebsd 13.4
        uses: cross-platform-actions/action@v0.26.0
        with:
          memory: 512M
          operating_system: freebsd
          version: "13.4"
          shell: sh
          run: |
            uname -a            

git push करें और GitHub Actions को चलाएं।

$ git remote add origin git@github.com:masa23/actions_freebsd.git
$ git add .
$ git commit -m "पहला कमिट"
$ git branch -M main
$ git push origin main

जब GitHub Actions चलाया जाएगा, तो FreeBSD 13.4 का uname -a चलाया जाएगा।

Actions के परिणाम पहला कमिट #1

आसानी से FreeBSD का उपयोग किया जा सकता है🎉

FreeBSD का कस्टम इमेज

कस्टम इमेज बनाना

freebsd-builder का उपयोग करके, आप FreeBSD के कस्टम इमेज का उपयोग कर सकते हैं।

वास्तविक वातावरण में, हम कस्टम इमेज को निर्दिष्ट कर रहे हैं।
यह बातें थोड़ी शर्मनाक हैं, लेकिन 11.2 जैसे पुराने FreeBSD काम कर रहे हैं, और golang डालने के लिए एक इमेज की आवश्यकता है,
इसलिए मैंने कस्टम इमेज तैयार किया है।

इस बार हम FreeBSD 13.4 में golang और cgo के लिए gcc डालने के लिए एक इमेज बनाएंगे।

रेपो को क्लोन करें और कस्टम इमेज बनाएं।

$ git clone https://github.com/cross-platform-actions/freebsd-builder.git
$ cd freebsd-builder
  • custom.sh का उपयोग करके, मैं कस्टम इमेज बना रहा हूँ।

CGO के निर्माण के लिए gcc और Go स्थापित कर रहा हूँ।

$ vi resource/custom.sh
#!/bin/sh

set -exu

# यहां अपने कस्टम VM इमेज के लिए अतिरिक्त प्रावधान जोड़ें।

GOLANG_VERSION=1.23.4

pkg install -y gcc
fetch -o - https://go.dev/dl/go${GOLANG_VERSION}.freebsd-amd64.tar.gz | tar xz -f - -C /usr/local/
ln -s /usr/local/go/bin/go /usr/local/bin/go

निर्माण वातावरण की स्थापना

sudo curl -fsSL https://apt.releases.hashicorp.com/gpg | sudo apt-key add -
sudo apt-add-repository "deb [arch=amd64] https://apt.releases.hashicorp.com $(lsb_release -cs) main"
sudo apt-get update && sudo apt-get install -y qemu-system-x86 packer
packer plugins install github.com/hashicorp/qemu

इमेज का निर्माण

PACKER_LOG=1 sudo -E bash ./build.sh 13.4 x86-64 -var headless=true
# ls -lh output/
total 664M
-rw-r--r-- 1 root root 664M Dec 20 16:29 freebsd-13.4-x86-64.qcow2

रिलीज

$ gh release create v0.0.1 --title "FreeBSD 13.4 add golang" --notes "FreeBSD 13.4" output/freebsd-13.4-x86-64.qcow2

इमेज का उपयोग करके CI चलाना

name: FreeBSD

on: 

jobs:
  test:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/checkout@v4
      - name: test for freebsd 13.0
        uses: cross-platform-actions/action@v0.26.0
        with:
          memory: 512M
          operating_system: freebsd
          version: 13.4
          image_url: https://github.com/masa23/freebsd-builder/releases/download/v0.0.1/freebsd-13.4-x86-64.qcow2 # यहां कस्टम इमेज का URL निर्दिष्ट करें
          shell: sh
          run: |
            export CC=gcc
            go test -v ./...            

Actions के परिणामों की जांच करना

Actions के परिणाम golang test cgo #3

सफलतापूर्वक कस्टम इमेज का उपयोग करके CI चलाया गया।

समाप्ति

  • मैंने GitHub Actions में FreeBSD का उपयोग करने का तरीका पेश किया।
  • चूंकि Qemu का उपयोग करते समय मेमोरी का अधिक उपयोग होता है, इसलिए इसे ध्यान में रखते हुए, FreeBSD का उपयोग करना आसान रहा।
  • चूंकि Qemu के उपयोग के कारण CI में समय लगता है, इसलिए इसे Pull Request या Release के समय ही सीमित करना उचित होगा।