使用 Bitbucket Pipelines 持續交付托管項目


簡介

Bitbucket Pipelines 是Atlassian公司為Bitbucket Cloud產品添加的一個新功能, 它為托管在Bitbucket上的項目提供了一個良好的持續集成/交付的服務。

 

目錄
   簡介
   前提
   例子
Demo項目介紹 配置
工作流程
Overview
參考

 

前提

申請 Bitbucket 賬號
Java 8
Gradle 2.6
Git

 

例子

Demo

准備一個小項目以便於更好的展示,用Vert.x創建一個簡單的Restful web service, 另外再添加一個integration test,這里用的是rest-assured library,在我其它多篇文章都有介紹這個第三方庫,專業做Restful API test, 大家可以參考。

項目結構如下圖

##轉載注明出處:http://www.cnblogs.com/wade-xu/p/6480319.html 

 

App.java

 1 package com.wadeshop.tutorial;
 2 
 3 import java.time.LocalDateTime;
 4 
 5 import io.vertx.core.Vertx;
 6 import io.vertx.core.http.HttpServer;
 7 
 8 public class App {
 9     private HttpServer listen;
10 
11     public static void main(String[] args) {
12         new App().run();
13 
14     }
15 
16     public void run() {
17         listen = Vertx.vertx().createHttpServer()
18                 .requestHandler(req -> req.response().putHeader("content-type", "application/json").end("{\"message\":\"" + LocalDateTime.now() + "\"}")).listen(8080, handler -> {
19                     if (handler.succeeded()) {
20                         System.out.println("server is running: http://localhost:8080/");
21                     } else {
22                         System.err.println("server startup failed trying to listen on port 8080");
23                     }
24                 });
25     }
26 
27     public void shutdown() {
28         if (listen != null)
29             listen.close();
30     }
31 
32 }

 

AppIntegrationTest.java

package integration;

import static io.restassured.RestAssured.given;
import static org.hamcrest.Matchers.notNullValue;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import com.wadeshop.tutorial.App;

import io.restassured.http.ContentType;

public class AppIntegrationTest {
    App app = new App();

    @Before
    public void setup() {
        app.run();
    }

    @After
    public void teardown() {
        app.shutdown();
    }

    @Test
    public void shouldReturnValidDate() throws Exception {
        given().contentType(ContentType.JSON).when().get("http://localhost:8080/").then().body("message", notNullValue());
    }
}

 此外,項目使用Gradle 作為Build 工具

[sts] -----------------------------------------------------
[sts] Starting Gradle build for the following tasks: 
[sts]      :cleanEclipse
[sts]      :eclipse
[sts] -----------------------------------------------------
:cleanEclipseClasspath
:cleanEclipseJdt
:cleanEclipseProject
:cleanEclipse
:eclipseClasspath
:eclipseJdt
:eclipseProject
:eclipse

BUILD SUCCESSFUL

Total time: 1.345 secs
[sts] -----------------------------------------------------
[sts] Build finished succesfully!
[sts] Time taken: 0 min, 1 sec
[sts] -----------------------------------------------------

 

配置

A. 在Bitbucket 建repository, put your project to bucket

  1. Step 1: Switch to your repository's directory

    cd /path/to/your/repo
  2. Step 2: Connect your existing repository to Bitbucket

    git remote add origin https://xxx/xxx.git
          gitpush -u origin master

B. 激活 Bitbucket Pipelines 

接下來選擇Java - Gradle 作為Template

配置yml 文件如下

bitbucket-pipelines.yml

# using gradle as build tool ..
image: qlik/gradle
  
pipelines:
  default:
    - step:
        script:
          - gradle --version
          - gradle test

注意 image: qlik/gradle 是一個安裝了Java和Gradle的Docker鏡像文件, 來自於DockerHub

##轉載注明出處:http://www.cnblogs.com/wade-xu/p/6480319.html 

 

Bitbucket Pipelines 的工作流程:

每當項目里有commit 被push 的時候, Pipelines會做如下步驟:

  • 創建一個安裝並配置了Java和Gradle的環境
  • Check out 項目
  • 解決依賴
  • 運行測試

 

Bitbucket Pipelines Overview

隨便提交一個啥,然后看看結果, Successful了

 

點擊看詳細結果

 

##轉載注明出處:http://www.cnblogs.com/wade-xu/p/6480319.html 

 

接下來 故意把Test 斷言改錯 讓case failed

 

Pipelines 也Failed了

 詳細Log

 

 

 

 參考 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM