內容轉載自個人博客java
對於普通的github倉庫,只須要在根目錄建立.github/workflows/
文件夾便可自動使用Actions功能,具體執行的操做能夠建立一個配置文件(命名不限),如build_apk.yml
Github Actions產品對公開倉庫是徹底免費的,對私人倉庫每個月有2000分鐘使用時間,詳細說明見費用。另外,github有許多官方已經實現好的actions能夠供用戶直接調用,用戶只需用設置參數便可
每一個配置文件稱爲一個工做流程(workflow),每一個工做流程能夠包含多個做業(job),每一個做業能夠包含一系列的步驟(steps),每一個step能夠稱爲action,能夠認爲這是三個層級android
這裏以項目WhuHelper爲例,介紹如何使用YAML語法和Github Actions功能git
build test
、build app-debug.apk
)之後的文件版本爲4ef2e90,只須要關注文件build_apk.yml便可,其餘文件無變化app-debug.apk
、建立倉庫的release(只包括代碼,且只在push tag
時觸發)、爲這次release添加apk文件,文件版本爲20fe364,只須要關注文件build_apk.yml便可,其餘文件無變化主要涉及到的操做爲:設置workflow及如下層級的每一個操做名字、設置workflow的觸發條件、建立多個job、job的條件執行、調用別人寫好的actions、本身爲某個step設置輸出參數供其餘步驟調用、持久化build的結果、上傳build的結果供用戶下載、下載build的結果供下一步操做使用、多個job之間傳遞數據、多個step之間傳遞數據、使用環境變量
實例build_apk.yml
文件內容及解析以下:github
name: Auto build debug apk # 設置workflow的觸發條件 # 在pull和push到主分支時觸發workflow # 在push tags時觸發workflow on: pull_request: branches: - 'master' push: branches: - 'master' # 在push tag時觸發 tags: - '*' # workflow的全部做業job jobs: # 單個job的名字:測試Android項目 # 每一個job執行完畢會默認刪除全部文件等 # 可經過cache來保留特定文件夾和文件 # 也可以使用upload-artifact上傳來實現保留文件,再配合download-artifact實現多job之間的數據傳遞 test: # test這個做業的實際名字 # 也是執行build時Actions監控處顯示的名字 name: Run Unit Tests # job的運行平臺,還有windows、macos及不一樣版本可供選擇 runs-on: ubuntu-18.04 # test任務的具體步驟,能夠有不少個步驟,都寫在這裏 steps: # 使用別人寫好的指定版本的actions腳本,名稱是checkout # 這是步驟1,即每一個'-'符號到下一個'-'符號之間的部分是一個步驟 - uses: actions/checkout@v2 # 這是步驟2,建立java環境,with裏面填寫actions的輸入參數 - name: set up JDK 1.8 uses: actions/setup-java@v1 # 設置setup-java腳本的輸入參數 with: java-version: 1.8 # 步驟3,執行shell命令 - name: Unit tests run: bash ./gradlew test --stacktrace apk: name: Generate APK runs-on: ubuntu-18.04 steps: - uses: actions/checkout@v2 - name: set up JDK 1.8 uses: actions/setup-java@v1 with: java-version: 1.8 - name: Build debug APK run: bash ./gradlew assembleDebug --stacktrace # 利用upload-artifact實現build結果的保存(能夠在Actions的控制檯下載壓縮文件) - name: Upload APK uses: actions/upload-artifact@v2 with: # 設置壓縮文件的名稱,在控制檯會獲得WhuHelper-debug.zip文件的下載連接 # 下載後解壓縮,裏面直接能夠看到app-debug.apk,沒有其餘東西 name: WhuHelper-debug path: app/build/outputs/apk/debug/app-debug.apk deploy: name: Upload Release Asset # 依賴上一個job needs: apk runs-on: ubuntu-latest # 只在tag時執行,即在本身終端運行如下代碼後纔會觸發 # git tag -a v0.1.0 -m "release 0.1.0 version" # git push origin –-tags if: contains(github.ref, 'tags/') steps: # 本身編寫的shell命令 # 學習如何設置單個任務的輸出來被其餘任務調用 - name: Prepare Release # 設置id通常是爲了其餘step調用本步驟的輸出 id: prepare_release run: | TAG_NAME=`echo $GITHUB_REF | cut -d / -f3` echo ::set-output name=tag_name::$TAG_NAME - name: Download build result for job apk # 只有上一步獲取到tag_name才繼續,下載前面apk任務裏面的WhuHelper-debug.zip文件 # 自動解壓縮到當前文件夾,自動刪除原壓縮文件 # 多任務之間的數據交換 if: steps.prepare_release.outputs.tag_name uses: actions/download-artifact@v2 with: name: WhuHelper-debug - shell: bash # 手動更改apk名字 run: | mv app-debug.apk app-debug-${{steps.prepare_release.outputs.tag_name}}.apk # 發佈release,版本號是用戶git push的tag裏面的版本號,發佈的只有代碼壓縮包(與手動默認發佈一致) - name: Create Release id: create_release # 只有上一步獲取到tag_name才繼續 if: steps.prepare_release.outputs.tag_name uses: actions/create-release@v1 env: # GitHub 會自動建立 GITHUB_TOKEN 密碼以在工做流程中使用 GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} # 設置時區,默認是格林尼治時間 # TZ: Asia/Shanghai with: tag_name: ${{steps.prepare_release.outputs.tag_name}} release_name: Release ${{steps.prepare_release.outputs.tag_name}} by zfb draft: false prerelease: false # 這一步是對上一步發佈的release文件的補充,調用github api上傳一個apk文件 - name: Upload Release Asset id: upload-release-asset # 只有create_release成功獲得輸出才繼續 if: steps.create_release.outputs.upload_url uses: actions/upload-release-asset@v1 env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} with: upload_url: ${{ steps.create_release.outputs.upload_url }} asset_path: ./app-debug-${{steps.prepare_release.outputs.tag_name}}.apk asset_name: app-debug-${{steps.prepare_release.outputs.tag_name}}.apk asset_content_type: application/vnd.android.package-archive
coding.net
主要包括如下步驟:docker
訪問令牌
,授予倉庫權限,假設名字爲GITHUB_AUTO_DEPLOY
,複製顯示的token備用設置-->倉庫設置
,便可看到設置本倉庫地址的方法,本例子顯示爲(格式爲https://e.coding.net/團隊名/項目名/倉庫名.git
)git remote set-url origin https://e.coding.net/zfbin/zfbin/zfbin.git
使用token來讀寫遠程倉庫(格式爲https://用戶名:token@e.coding.net/團隊名/項目名/倉庫名.git
),使用以下命令本身測試一下,用戶名(默認是手機號碼)爲13677888877
,上一步獲得的token是cdd0b865cdd0b865cdd0b865cdd0b865cf87ce84
,團隊名稱是zfbin
,項目的名稱是zfbin
,代碼倉庫的名稱是zfbin
(這裏全部配置的敏感信息都是示例):shell
~/work/github/zfbin > git push "https://13677888877:cdd0b865cdd0b865cdd0b865cdd0b865cf87ce84@e.coding.net/zfbin/zfbin/zfbin.git" master:master Everything up-to-date ~/work/github/zfbin >
github
打開Github的此倉庫的Secrets選項,新建如下祕鑰:express
DEPLOY_CODING cdd0b865cdd0b865cdd0b865cdd0b865cf87ce84 CODING_USERNAME 13677888877 CODING_REF e.coding.net/zfbin/zfbin/zfbin.git
coding.net
github的倉庫的原始文件版本爲8570167,最終添加github actions以後的文件版本爲0462a89e,不須要關注其餘文件,只考慮.github/workflows/deploy_to_coding.yml
文件,其內容以下:macos
name: Auto deploy to coding pages # 在push主分支時觸發構建 on: push: branches: - 'master' jobs: # job的名字:推送到coding deploy: name: Deploy to Coding # job的運行平臺 runs-on: ubuntu-18.04 # test任務的步驟 steps: # 使用別人寫好的指定版本的actions腳本,名稱是checkout,下載本倉庫 - uses: actions/checkout@v2 - name: 設置提交者的我的信息 # 這三個變量的值都放在 https://github.com/zfb132/zfb132.github.com/settings/secrets env: # 設置時區 TZ: Asia/Shanghai # 在coding.net的某個倉庫新建訪問令牌出現的祕鑰 coding_token: ${{ secrets.DEPLOY_CODING }} # 團隊中的某我的的用戶名,通常默認是本人手機號碼 coding_username: ${{ secrets.CODING_USERNAME }} # 格式爲:e.coding.net/組織名/項目名/倉庫名.git coding_ref: ${{ secrets.CODING_REF }} run: | export message=$(git log --pretty=format:"%s" -1) [ -f CNAME ] && rm CNAME || echo "CNAME doesn't exist" rm -rf .github rm -rf .git git clone https://${coding_username}:${coding_token}@${coding_ref} coding_dir cd coding_dir && mv .git ../ && cd ../ && rm -rf coding_dir git config --local user.email "zfb132@gmail.com" git config --local user.name "zfb" git config core.filemode false git remote set-url origin https://${coding_ref} git add . git commit -m "$message" git push --force --quiet "https://${coding_username}:${coding_token}@${coding_ref}" master:master
具體運行的命令的解釋:ubuntu
export message=$(git log --pretty=format:"%s" -1)
是獲取github的提交的messagerm CNAME
是刪除github倉庫的CNAME
文件,由於coding.net不須要此文件rm -rf .github
是刪除github actions的配置文件,由於coding.net不須要進行CIrm -rf .git
是刪除github倉庫時的git信息,爲後面使用coding.net的git清理空間git clone https://${coding_username}:${coding_token}@${coding_ref} coding_dir
是克隆coding.net的對應倉庫,主要爲了.git
文件夾,因此只是把此倉庫下載到一個臨時文件夾coding_dir
cd coding_dir && mv .git ../ && cd ../ && rm -rf coding_dir
是把coding.net的.git
文件夾替換掉原來的,而且刪除臨時文件夾git config --local user.email "zfb132@gmail.com"
是設置提交者的電子郵箱地址git config --local user.name "zfb"
是設置提交者的名字git config core.filemode false
忽略文件屬性的問題,由於github的文件模式(權限)不必定與coding.net的相同git remote set-url origin https://${coding_ref}
是設置遠程倉庫的地址爲coding.net的倉庫git add .
是添加文件到暫存區git commit -m "$message"
設置commit的信息與github一致git push --force --quiet "https://${coding_username}:${coding_token}@${coding_ref}" master:master
是強制推送到遠程倉庫