使用Shell判断钥匙串里是否包含mobileprovision里的Identity

Shell系列下一篇:使用shell判断描述文件mobileprovision属于debug/enterprise/ad-hoc/appstore的类型


参考:

  • 漫谈iOS程序的证书和签名机制

将mobileprovision转换成plist:security cms -D -i xxx.mobileprovision > xxx.plist
读取plist:/usr/libexec/PlistBuddy -c "print xxxx" xxx.plist
读取所有合法的Identity:security find-identity -v -p codesigning
查找证书:security find-certificate -c xxx
导入证书:security import "p12路径" -k "/Users/xxx/Library/Keychains/login.keychain" -P "电脑密码" -T "/usr/bin/codesign"

前言

在配置jenkins自动打包时需要通过xxx.mobileprovision判断描述文件所包含的证书是否已经安装,如果没有安装,则从服务器下载来安装,这个判断过程主要有两种

方式一:

使用security find-identity -v -p codesigning读取Mac电脑上已安装的合法的证书列表,然后使用字符串匹配的方式进行判断是否有描述文件包含的的Identity

#使用security find-identity -v -p codesigning的方式判断
function findIdentityISExist1()
{
  #接收参数,也就是xxx.mobileprovision的路径
  profile_path=$1
  #临时将xxx.mobileprovision转换成plist文件路径
  temp_plist_path="./temp_profile.plist"
  #删除之前存在的plist文件
  rm -rf $temp_plist_path
  #将xxx.mobileprovision转换成xxx.plist
  security cms -D -i "$profile_path" > $temp_plist_path

  #读取xxx的plist,得到对应的内容并组装成Identity的格式
  config_disName=$(/usr/libexec/PlistBuddy -c "print Name" $temp_plist_path)
  config_TeamName=$(/usr/libexec/PlistBuddy -c "print TeamName" $temp_plist_path)
  config_teamId=$(/usr/libexec/PlistBuddy -c "print :Entitlements:com.apple.developer.team-identifier" $temp_plist_path)

  find_identity="iPhone Distribution: $config_TeamName ($config_teamId)"

 #获取本机安装的所有合法的Identity列表
  valid_list=`security find-identity -v -p codesigning`
  if [[ $valid_list == *$find_identity* ]] ; then
      echo "---find-success----"
      return 1
  else
      echo "---find-fail----"
      return 0
  fi
}


方式二:

使用security find-certificate -c "xxxx"的方式判断Identity是否存在

#使用security find-certificate -c "xxxx"的方式判断
function findIdentityISExist2()
{
  #接收参数,也就是xxx.mobileprovision的路径
  profile_path=$1
  #临时将xxx.mobileprovision转换成plist文件路径
  temp_plist_path="./temp_profile.plist"
  #删除之前存在的plist文件
  rm -rf $temp_plist_path
  #将xxx.mobileprovision转换成xxx.plist
  security cms -D -i "$profile_path" > $temp_plist_path

  #读取xxx的plist,得到对应的内容并组装成Identity的格式
  config_disName=$(/usr/libexec/PlistBuddy -c "print Name" $temp_plist_path)
  config_TeamName=$(/usr/libexec/PlistBuddy -c "print TeamName" $temp_plist_path)
  config_teamId=$(/usr/libexec/PlistBuddy -c "print :Entitlements:com.apple.developer.team-identifier" $temp_plist_path)

  find_identity="iPhone Distribution: $config_TeamName ($config_teamId)"

  #查找证书
  find_result=`security find-certificate -c "$find_identity"`
  #echo $find_result
  if [[ -n $find_result ]] ; then
      echo "---find_result-success----"
      return 1
  else
      echo "---find_result-fail----"
      return 0
  fi
}

完整判断及demo

# !/bin/bash

#使用security find-identity -v -p codesigning的方式判断
function findIdentityISExist1()
{
  #接收参数,也就是xxx.mobileprovision的路径
  profile_path=$1
  #临时将xxx.mobileprovision转换成plist文件路径
  temp_plist_path="./temp_profile.plist"
  #删除之前存在的plist文件
  rm -rf $temp_plist_path
  #将xxx.mobileprovision转换成xxx.plist
  security cms -D -i "$profile_path" > $temp_plist_path

  #读取xxx的plist,得到对应的内容并组装成Identity的格式
  config_disName=$(/usr/libexec/PlistBuddy -c "print Name" $temp_plist_path)
  config_TeamName=$(/usr/libexec/PlistBuddy -c "print TeamName" $temp_plist_path)
  config_teamId=$(/usr/libexec/PlistBuddy -c "print :Entitlements:com.apple.developer.team-identifier" $temp_plist_path)

  find_identity="iPhone Distribution: $config_TeamName ($config_teamId)"

 #获取本机安装的所有合法的Identity列表
  valid_list=`security find-identity -v -p codesigning`
  if [[ $valid_list == *$find_identity* ]] ; then
      echo "---find-success----"
      return 1
  else
      echo "---find-fail----"
      return 0
  fi
}

#使用security find-certificate -c "xxxx"的方式判断
function findIdentityISExist2()
{
  #接收参数,也就是xxx.mobileprovision的路径
  profile_path=$1
  #临时将xxx.mobileprovision转换成plist文件路径
  temp_plist_path="./temp_profile.plist"
  #删除之前存在的plist文件
  rm -rf $temp_plist_path
  #将xxx.mobileprovision转换成xxx.plist
  security cms -D -i "$profile_path" > $temp_plist_path

  #读取xxx的plist,得到对应的内容并组装成Identity的格式
  config_disName=$(/usr/libexec/PlistBuddy -c "print Name" $temp_plist_path)
  config_TeamName=$(/usr/libexec/PlistBuddy -c "print TeamName" $temp_plist_path)
  config_teamId=$(/usr/libexec/PlistBuddy -c "print :Entitlements:com.apple.developer.team-identifier" $temp_plist_path)

  find_identity="iPhone Distribution: $config_TeamName ($config_teamId)"

  #查找证书
  find_result=`security find-certificate -c "$find_identity"`
  #echo $find_result
  if [[ -n $find_result ]] ; then
      echo "---find_result-success----"
      return 1
  else
      echo "---find_result-fail----"
      return 0
  fi
}

#将mobileprovision解析成plist文件
profile_path="/xxx/xxx/xxx.mobileprovision"
p12_path="/xxx/xxx/xxx.p12"

findIdentityISExist1 $profile_path
result1=$?
echo ">>>aaaa>>>: $result1"
if [ $result1 == 1 ]; then
    echo "已存在Identity---aaaa---11111"
else
    echo "还不存在Identity--aaaa---0000"
    #安装p12发布证书
    security import $p12_path \
      -k "/Users/xxx/Library/Keychains/login.keychain" \
      -P "电脑密码" \
      -T "/usr/bin/codesign"
fi

findIdentityISExist2 $profile_path
result2=$?
echo ">>>bbbb>>>: $result2"
if [ $result2 == 1 ]; then
    echo "已存在Identity--bbbb---11111"

else
    echo "还不存在Identity---bbbb---0000"
    #安装p12发布证书
    security import $p12_path \
      -k "/Users/xxx/Library/Keychains/login.keychain" \
      -P "电脑密码" \
      -T "/usr/bin/codesign"
fi

你可能感兴趣的:(使用Shell判断钥匙串里是否包含mobileprovision里的Identity)