コンテンツにスキップ

OSをアップデートするTaskfile.ymlサンプル

Taskを使い、macOS/Linux(AmazonLinux/Ubuntu)を見分けてOSのアップデートを実施するTaskfile.ymlのサンプルをメモしておきます。

検証環境

対象 バージョン
macOS Tahoe 26.5.2
Ubuntu 26.04 LTS
Amazon Linux 2023.12.20260710

Taskfile.ymlのサンプル

# yaml-language-server: $schema=https://taskfile.dev/schema.json

version: '3'

silent: true

vars:
  DETECTED_OS:
    sh: echo {{OS}}

  LINUX_DISTRO:
    sh: |
      if [ -f /etc/os-release ]; then
        . /etc/os-release
        echo "$ID"
      else
        echo "unknown"
      fi

tasks:
  default:
    cmds:
      - task --list

  _time-start:
    internal: true
    cmds:
      - python3 -c 'import time; print(int(time.time() * 1000))' > .task_start_time

  _time-end:
    internal: true
    cmds:
      - |
        start=$(cat .task_start_time 2>/dev/null || echo 0)
        end=$(python3 -c 'import time; print(int(time.time() * 1000))')
        elapsed_ms=$((end - start))
        python3 -c "g='\033[32m'; n='\033[0m'; p='======================================== '; ms=$elapsed_ms; s=ms//1000; r=ms%1000; print(f'{g}{p}Elapsed: {s} second{\"s\" if s!=1 else \"\"} {r} millisecond{\"s\" if r!=1 else \"\"}{n}') if s>0 else print(f'{g}{p}Elapsed: {r} millisecond{\"s\" if r!=1 else \"\"}{n}')"
        rm -f .task_start_time

  docker-check:
    aliases: [dc]
    desc: Display unhealthy containers
    cmds:
      - docker ps -a --filter "health=unhealthy"

  docker-restart:
    aliases: [dr]
    desc: Restart unhealthy containers
    cmds:
      - docker restart $(docker ps -a --filter "health=unhealthy" -q)

  os-check:
    aliases: [oc,c]
    desc: Detects the OS/distribution and checks whether package updates are available (no changes applied)
    cmds:
      - task: _time-start
      - task: os-check:amazon-linux
      - task: os-check:macos
      - task: os-check:ubuntu
      - task: _time-end

  os-check:amazon-linux:
    internal: true
    status:
      - '[ "{{.DETECTED_OS}}" != "linux" ] || [ "{{.LINUX_DISTRO}}" != "amzn" ]'
    cmds:
      - echo "Checking for Amazon Linux 2023 package updates..."
      - |
        output=$(sudo dnf check-update 2>&1)
        rc=$?
        echo "$output"
        if [ "$rc" -eq 100 ]; then
          echo "----------------------------------------"
          echo "Updates are available (see the list above)."
        elif [ "$rc" -eq 0 ]; then
          echo "No updates available. System is up to date."
        else
          echo "Failed to check updates (exit code: $rc)."
        fi

  os-check:macos:
    internal: true
    status:
      - '[ "{{.DETECTED_OS}}" != "darwin" ]'
    cmds:
      - echo "Checking for macOS Homebrew package updates..."
      - brew update
      - |
        outdated=$(brew outdated --verbose)
        if [ -n "$outdated" ]; then
          echo "The following package(s) can be updated:"
          echo "$outdated"
        else
          echo "No updates available. System is up to date."
        fi

  os-check:ubuntu:
    internal: true
    status:
      - '[ "{{.DETECTED_OS}}" != "linux" ] || [ "{{.LINUX_DISTRO}}" != "ubuntu" ]'
    cmds:
      - echo "Checking for Ubuntu package updates..."
      - sudo apt-get update -qq
      - |
        upgradable=$(apt list --upgradable 2>/dev/null | grep -v '^Listing')
        if [ -n "$upgradable" ]; then
          echo "The following package(s) can be updated:"
          echo "$upgradable"
        else
          echo "No updates available. System is up to date."
        fi

  os-remove:
    aliases: [or,r]
    desc: Detects the OS/distribution and removes unnecessary packages and caches
    cmds:
      - task: _time-start
      - task: os-remove:amazon-linux
      - task: os-remove:macos
      - task: os-remove:ubuntu
      - task: _time-end

  os-remove:amazon-linux:
    internal: true
    status:
      - '[ "{{.DETECTED_OS}}" != "linux" ] || [ "{{.LINUX_DISTRO}}" != "amzn" ]'
    cmds:
      - echo "Removing unnecessary packages and cache (Amazon Linux)..."
      - sudo dnf autoremove -y
      - sudo dnf clean all

  os-remove:macos:
    internal: true
    status:
      - '[ "{{.DETECTED_OS}}" != "darwin" ]'
    cmds:
      - echo "Removing Homebrew cache and old versions..."
      - brew cleanup

  os-remove:ubuntu:
    internal: true
    status:
      - '[ "{{.DETECTED_OS}}" != "linux" ] || [ "{{.LINUX_DISTRO}}" != "ubuntu" ]'
    cmds:
      - echo "Removing unnecessary packages and cache (Ubuntu)..."
      - sudo apt-get autoremove -y
      - sudo apt-get clean

  os-update:
    aliases: [ou,u]
    desc: Automatically detects the OS/distribution and runs the system update
    cmds:
      - task: _time-start
      - task: os-update:amazon-linux
      - task: os-update:macos
      - task: os-update:ubuntu
      - task: _time-end

  os-update:amazon-linux:
    internal: true
    status:
      - '[ "{{.DETECTED_OS}}" != "linux" ] || [ "{{.LINUX_DISTRO}}" != "amzn" ]'
    cmds:
      - echo "Running Amazon Linux system update..."
      - sudo dnf update -y
      - |
        if [ -f /opt/gitlab/embedded/service/gitlab-shell/bin/gitlab-sshd ]; then
          sudo setcap 'cap_net_bind_service=+ep' /opt/gitlab/embedded/service/gitlab-shell/bin/gitlab-sshd
        fi

  os-update:macos:
    internal: true
    status:
      - '[ "{{.DETECTED_OS}}" != "darwin" ]'
    cmds:
      - echo "Running macOS Homebrew update..."
      - brew update
      - brew upgrade -y

  os-update:ubuntu:
    internal: true
    status:
      - '[ "{{.DETECTED_OS}}" != "linux" ] || [ "{{.LINUX_DISTRO}}" != "ubuntu" ]'
    cmds:
      - echo "Running Ubuntu system update..."
      - sudo apt-get update
      - sudo apt-get upgrade -y