Skip to content

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のサンプル

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# 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