pipeline { agent any environment { ANSIBLE_HOST_KEY_CHECKING = false PUBLIC_IP = '52.194.230.165' //WebサーバーのパブリックIP } options { skipDefaultCheckout(true) } stages{ stage("checkout") { steps { // Iacコードを取得 checkout scm } } stage('Syntax Check') { steps { script { // Ansible Playbookの文法チェック dir('step2'){ ansiblePlaybook( playbook: 'playbook.yml', inventory: 'host', extras: '--syntax-check' ) } } } } stage('Dry Run (Check Mode)') { steps { script { // Ansible Playbookの仮実行 (チェックモード) dir('step2'){ ansiblePlaybook( playbook: 'playbook.yml', inventory: 'host', extras: '--check' ) } } } } stage('Apply (Real Execution)') { steps { script { // Ansible Playbookの本実行 dir('step2'){ ansiblePlaybook( playbook: 'playbook.yml', inventory: 'host' ) } } } } stage('Check Web Application Status') { steps { script { def status = '0' try { // Webサイトのステータスをチェックする def url = "http://${PUBLIC_IP}/" status = sh(script: "curl -o /dev/null -s -w '%{http_code}' ${url}", returnStdout: true).trim() } catch (Exception e) { // エラー発生時でもジョブを失敗させない echo "Error while checking the website: ${e.getMessage()}" } if (status != '200') { echo "Website is not accessible. Status code: ${status}" } else { echo "Website is running successfully. Status code: ${status}" } } } } } }