Ïê½âGitLab×Ô¶¯»¯°²ÅŵÄÁ÷³ÌºÍ»ù±¾ÒªÁì
gitlab ÊÇÏÖÔÚ½ÏΪʢÐеĴúÂëÍйÜƽ̨֮һ£¬Ëü²»µ«Ìṩ´úÂë´æ´¢ºÍ°æ±¾ÖÎÀí¹¦Ð§£¬²¢ÇÒ»¹Ö§³Ö×Ô¶¯»¯°²ÅÅ£¬Àû±ã¿ª·¢Õß¿ìËٵؽ«´úÂë°²Åŵ½Ð§ÀÍÆ÷ÉϾÙÐвâÊÔºÍÉú²úÇéÐÎÐû²¼¡£±¾ÎĽ«ÏÈÈÝ gitlab ×Ô¶¯»¯°²ÅŵÄÁ÷³ÌºÍ»ù±¾ÊµÏÖÒªÁì¡£
Ê×ÏÈ£¬GitLab ×Ô¶¯»¯°²ÅŵÄʵÏÖÐèÒªÒÀÀµÓÚ GitLab CI/CD£¨Continuous Integration/Continuous Deployment£©¹¦Ð§£¬¸Ã¹¦Ð§ÊÇ»ùÓÚ CI/CD Á÷Ë®ÏßÀ´ÊµÏÖ×Ô¶¯»¯¹¹½¨¡¢²âÊԺͰ²Åŵġ£Òò´Ë£¬ÊµÏÖ GitLab ×Ô¶¯»¯°²ÅŵÄÒªº¦¾ÍÊÇÉèÖà CI/CD Á÷Ë®Ïߣ¬²¢ÎªÆäÉèÖÃÏìÓ¦µÄ¹¹½¨¡¢²âÊԺͰ²ÅÅʹÃü¡£
ÒÔÏÂÊÇÒ»¸öµä·¶µÄ GitLab CI/CD Á÷Ë®ÏßʾÀý£º
stages: - build - test - deploy build: stage: build script: - npm install - npm run build test: stage: test script: - npm test deploy: stage: deploy script: - ssh user@server "cd /var/www/html && git pull"
µÇ¼ºó¸´ÖÆ
ÉÏÊöʾÀýÖУ¬ÎÒÃǽç˵ÁËÈý¸ö½×¶Î£¨stage£©£¬»®·ÖÊǹ¹½¨¡¢²âÊԺͰ²ÅÅ¡£ÔÚ¹¹½¨½×¶Î£¬ÎÒÃÇʹÓà npm ×°ÖÃÏà¹ØÒÀÀµ²¢¾ÙÐдúÂë´ò°ü£»ÔÚ²âÊԽ׶Σ¬ÎÒÃÇÖ´ÐÐÏìÓ¦²âÊԾ籾£¬È·±£´úÂëÖÊÁ¿£»ÔÚ°²ÅŽ׶Σ¬ÎÒÃÇͨ¹ý SSH ÅþÁ¬µ½Ð§ÀÍÆ÷£¬½«´úÂë°²Åŵ½Ö¸¶¨µÄλÖá£
ÔÚÉèÖà CI/CD Á÷Ë®Ïß֮ǰ£¬ÎÒÃÇÐèÒªÏÈΪ GitLab ÏîÄ¿Ìí¼Ó°²ÅŹ«Ô¿£¬ÕâÑù²Å»ª¹»½«´úÂë°²Åŵ½Ð§ÀÍÆ÷ÉÏ¡£Ïêϸ°ì·¨ÈçÏ£º
ÔÚЧÀÍÆ÷ÉÏÌìÉú SSH ÃÜÔ¿£¬¿ÉÒÔʹÓà ssh-keygen ÏÂÁîÀ´ÌìÉú¹«Ô¿ºÍ˽Կ£»
½«¹«Ô¿Ìí¼Óµ½ GitLab ÏîÄ¿ÖУ¬ÔÚÏîÄ¿Ò³ÃæÖеã»÷×ó²à²Ëµ¥À¸ÖÐµÄ Settings -> CI/CD -> Deploy keys£¬µã»÷ Add deploy key °´Å¥½«¹«Ô¿Ìí¼Óµ½ÏîÄ¿ÖУ»
ÔÚÏîÄ¿µÄ¸ùĿ¼Öн¨ÉèÒ»¸öÃûΪ .gitlab-ci.yml µÄÎļþ£¬ÓÃÓÚ½ç˵ CI/CD Á÷Ë®Ïߣ»
ÔÚÉÏÊö°ì·¨Íê³Éºó£¬ÎÒÃǾͿÉÒÔ×îÏÈÉèÖà CI/CD Á÷Ë®ÏßÁË¡£ÔÚÁ÷Ë®ÏßÖУ¬¿ÉÒÔʹÓÃÏìÓ¦µÄÓïÑԺ͹¤¾ßÀ´Íê³É¹¹½¨¡¢²âÊԺͰ²ÅŵÈʹÃü£¬ÀýÈç Maven¡¢Gradle¡¢npm¡¢Docker µÈ¡£ÔÚÏêϸʵÏÖʱ£¬ÐèҪעÖØһЩϸ½ÚºÍÎÊÌ⣬ÈçÈçÄÇÀïÖÃĿ¼½á¹¹¡¢ÔõÑùת´ï²ÎÊýºÍÇéÐαäÁ¿¡¢ÈçÄÇÀïÖÃȨÏÞÎÊÌâµÈ¡£
ÔÚ±àдºÃÁË CI/CD Á÷Ë®ÏßÖ®ºó£¬ÎÒÃǾͿÉÒÔ½«´úÂëÌá½»µ½ GitLab ¿ÍÕ»ÖоÙÐÐ×Ô¶¯»¯¹¹½¨¡¢²âÊԺͰ²ÅÅÁË¡£ÔÚÿ´Î´úÂëÌύʱ£¬GitLab »áƾ֤ CI/CD µÄÉèÖÃ×Ô¶¯´¥·¢Á÷Ë®Ïߣ¬Ö´ÐÐÏìÓ¦µÄʹÃü¡£
×ܽáÀ´Ëµ£¬GitLab ×Ô¶¯»¯°²ÅÅÊÇÒ»¸öÏà¶Ô½ÏÁ¿ÖØ´óµÄÊÖÒÕ£¬µ«Ëü¿ÉÒÔ´ó´óÌá¸ß¿ª·¢Ð§ÂʺʹúÂëÖÊÁ¿£¬ïÔÌÐû²¼Ê±¼äºÍ¹ýʧÂÊ¡£Ö»ÒªÕÆÎÕÁËÏà¹ØÊÖÒÕºÍÒªÁ죬¾Í¿ÉÒÔÇáËÉʵÏÖ×Ô¶¯»¯°²ÅÅ¡£
ÒÔÉϾÍÊÇÏê½âGitLab×Ô¶¯»¯°²ÅŵÄÁ÷³ÌºÍ»ù±¾ÒªÁìµÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡