メインコンテンツまでスキップ

ビルド中のVPNへの接続

ビルドでVPN接続が必要になる場合があります。これは、サーバーに接続してリポジトリをgit cloneしたり、プライベートなAPIにアクセスしたりするためです。

VPNに接続するには、次の操作が必要です。

  1. ローカルネットワークのアドレス空間が、Bitrise仮想マシンのアドレス空間と重複していないことを確認してください。重複するとVPNでエラーが発生する可能性があります。
  2. 必要なVPNコンポーネントをインストールして設定します。
  3. VPNに接続します。

VPNは、Workflow内のどこでも設定・接続できます。ただし、当然ながらVPN接続を使用する前に行う必要があります。たとえば、リポジトリにアクセスするためにVPN接続が必要な場合は、Git Clone Repository Stepより前に接続しなければなりません。

SSHセッション

VPNツールを選択してセットアップ・設定を行う際は、既存のSSHセッションを再起動または中断しないよう注意してください。bitrise.ioのワーカーは、ビルドのControl/Masterマシンとビルド用仮想マシンの間のSSH接続が切断されると、ビルドを中止します。

VPN経由でのリポジトリへのアクセスClick to copy link​

BitriseプロジェクトのリポジトリがVPN経由でしかアクセスできない場合、特に次の2点に注意する必要があります。

  • ビルド中、リポジトリを仮想マシンにクローンする前にVPN接続を確立しておく必要があります。つまり実際には、接続を確立するStepを、Workflow内のGit CloneStepより前に配置する必要があるということです。
  • 新規プロジェクトを追加する際は、回避策が必要です。プロジェクトの追加処理中は、VPNなしでアクセスできるダミーのリポジトリを使用してください。

この回避策の手順を見ていきましょう。

  1. VPN接続なしでアクセスできる空のリポジトリを作成します。

  2. このリポジトリをソースとして新規プロジェクトを追加します。必ずプライベートプロジェクトにしてください。webhookを登録する必要はありません。

  3. プロジェクトに対するAdminのロールを持つユーザー、またはEdit workflow権限を持つカスタムロールを持つユーザーで、Bitrise上のプロジェクトを開きます。

  4. プロジェクトのメインページで、Project settingsボタンをクリックします。

  5. 左側でRepositoryを選択し、Changeをクリックします。

  6. ダイアログに新しいリポジトリのURLを貼り付けます。

    SCR-20260331-przw.png

  7. Saveをクリックします。

  8. Workflow内で、リポジトリにアクセスする必要があるStepsより前に、VPN接続を確立するStepまたはStepsを配置します。

以上で完了です。VPN接続を確立するさまざまな方法については、以下の設定例を参照してください。

VPNの設定例Click to copy link​

このセクションでは、ビルド中にVPNに接続するための設定例を3つ紹介します。

Connect to OpenVPN Server Stepを使用するClick to copy link​

Connect to OpenVPN Server Stepを使用するには、事前にOpenVPNサーバーを構築し、証明書ファイルと秘密鍵をエンコードしておく必要があります。

  1. OpenVPNサーバーをセットアップします。

  2. 次の項目をBase64でエンコードします。

    • CA証明書
    • クライアント証明書
    • クライアント秘密鍵

    次のコマンドでファイルをbase64エンコードできます。

    $ base64 <filepath>
  3. Bitriseでプロジェクトを開き、Workflow Editorに移動します。

  4. エンコードした証明書と鍵をSecretsとして登録します。

    以下のキーはVPN Stepのデフォルトの入力項目であるため、使用をおすすめします。

    • CA証明書: $VPN_CA_CRT_BASE64
    • クライアント証明書: $VPN_CLIENT_CRT_BASE64
    • 秘密鍵: $VPN_CLIENT_KEY_BASE64
  5. Connect to OpenVPN Server StepをWorkflowに追加します。

    VPN接続が必要になる可能性のあるStepsより前にStepを追加してください。

  6. 先ほど作成したSecretsを、それぞれの入力項目に追加します。

    • Base64 encoded CA Certificate
    • Base64 encoded Client Certificate
    • Base64 encoded Client Private Key

    推奨されたキーを使ってSecretsを作成した場合は、入力項目を変更する必要はありません。

  7. その他の必須項目を入力します。

    • Host: OpenVPNサーバーのIPまたはホスト名
    • Port: OpenVPNサーバーのポート番号
    • Protocol: OpenVPNサーバーのプロトコル

Strongswan VPNを使用するClick to copy link​

これはスクリプトの例です。リポジトリに保存してそこから実行することも、内容をそのままbitrise設定(bitrise.yml / Workflow)内のScript Stepにコピー&ペーストすることもできます。

このスクリプトはStrongswan VPNを使用してVPNに接続します。macOSまたはLinuxのどちらでも動作します。スクリプトの実行に成功すると、それ以降のBitrise StepはVPN接続経由でデバイスやサービスにアクセスできるようになります。

#!/usr/bin/env bash
set -e

echo "WAN IP"
# This prints the servers Internet IP adress to the log, useful for debugging
curl http://httpbin.org/ip

case "$OSTYPE" in
linux*)
echo "Configuring for Linux"

# Variables
etc_dir=/etc
etc_sudo='sudo' # Sudo is needed for Linux Strongswan configuration

# Install strongswan
echo "Installing Strongswan..."
sudo apt-get install -y strongswan

;;
darwin*)
echo "Configuring for Mac OS"

# Variables
etc_dir=/usr/local/etc
etc_sudo='' # Sudo is NOT needed for Mac OS Strongswan configuration

# Install Strongswan using homebrew
echo "Installing OpenSSL..."
# Manually install OpenSSL first to save time, since installing Strongswan directly compiles OpenSSL from source instead
brew install openssl
echo "Installing Strongswan..."
brew install strongswan

;;
*)
echo "Unknown operative system: $OSTYPE, exiting"
exit 1
;;
esac


# Method for rendering a template string file (when run, returns the input string with $VARIABLES replaced from env)
render_template() {
eval "echo \"$(cat $1)\""
}

# Create a temporary directory to hold files
temp_dir=/tmp/vpn-config
mkdir $temp_dir

# IPsec config file, see examples at https://wiki.strongswan.org/projects/strongswan/wiki/IKEv1Examples and https://wiki.strongswan.org/projects/strongswan/wiki/IKEv2Examples
echo "Downloading ipsec.conf..."
wget https://www.example.com/ipsec.conf.template -O $temp_dir/ipsec.conf.template
# IPsec credentials file, see documentation at https://wiki.strongswan.org/projects/strongswan/wiki/IpsecSecrets
echo "Downloading ipsec.secrets..."
wget https://www.example.com/ipsec.secrets.template -O $temp_dir/ipsec.secrets.template
# In some cases you might need to download the certificate, or certificate chain, of your other VPN endpoint
echo "Downloading server.crt..."
wget https://www.example.com/server.crt -O $temp_dir/server.crt

echo "Rendering config templates"
render_template $temp_dir/ipsec.conf.template > $temp_dir/ipsec.conf
render_template $temp_dir/ipsec.secrets.template > $temp_dir/ipsec.secrets

echo "Installing configuration"
$etc_sudo cp $temp_dir/ipsec.conf $etc_dir/ipsec.conf
$etc_sudo cp $temp_dir/ipsec.secrets $etc_dir/ipsec.secrets
$etc_sudo cp $temp_dir/server.crt $etc_dir/ipsec.d/ocspcerts/server.crt

# Start the ipsec service
echo "Starting ipsec"
sudo ipsec start

# We're sleeping between commands, mostly since Mac OS seems to have some problems otherwise
sleep 1

# Output some helpful status to the log
echo "Status ipsec"
sudo ipsec statusall

sleep 1

# Switch out myconnection with the name of your connection in ipsec.conf
echo "Initiating VPN connection"
sudo ipsec up myconnection

sleep 1

case "$OSTYPE" in
linux*)
;;
darwin*)
# In Mac OS El Capitan, the `sudo ipsec up` command consistently fails the first time, but succeeds after a restart of the ipsec service
echo "Restarting ipsec"
sudo ipsec restart

sleep 1

echo "Initiating VPN connection"
sudo ipsec up myconnection

sleep 1

# This step might apply if you are routing all traffic trough the IPsec connection (that is, if your remote IP range is 0.0.0.0/0)
# Mac OS El Capitan seems to have problems getting the DNS configuration from the Strongswan interface. Also IPv6 sometimes causes issues. So we're manually turning off IPv6 and forcing a new DNS configuration.
echo "Disabling IPv6 and forcing DNS settings"
# Fetch main interface
main_interface=$(networksetup -listnetworkserviceorder | awk -F'\\) ' '/\(1\)/ {print $2}')
# Completely disable IPv6
sudo networksetup -setv6off "$main_interface"
# Switch 10.0.0.1 with your DNS server
sudo networksetup -setdnsservers "$main_interface" 10.0.0.1
;;
*) ;;
esac

# Your VPN connection should be up and running. Any following steps of your Bitrise workflow can access devices over your VPN connection 🎉

Cisco VPNを使用するClick to copy link​

Cisco VPN connect Stepを使用できます。これはvpncを使って、VPN3000 Concentrator、Juniper/Netscreen、IOS、PIXが提供するCisco VPNに接続します。

  1. Bitrise にアクセスして Log in、左側の Bitrise CI を選択し、プロジェクトを選択します。

  2. メインページの Workflows ボタンをクリックします。

  3. Workflowの先頭にCisco VPN connect Stepを追加します。

  4. Stepに必要なVPNのクライアント設定と認証情報を、次のいずれかの方法で指定します。

    • 既存のvpnc設定ファイルを使用する: vpnc.conf
    • StepのCommandline options入力項目を使用する。この項目で指定したオプションは、設定ファイルより優先されます。

vpncのセットアップについて詳しくは、vpncホームページとvpncマニュアルをご覧ください。