CI

Github Actions

Posted by API Caller on September 11, 2019

GO

依旧以一个 go 可执行文件为例, 试一下 Github Actions.

为方便自动化, 不考虑在 /actions 页面手动操作.

  • 创建 .github/workflows/go.yml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# https://github.com/actions/starter-workflows/blob/master/ci/go.yml

name: Go
on: [push]
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:

    - name: Set up Go 1.12
      uses: actions/[email protected]
      with:
        go-version: 1.12
      id: go

    - name: Check out code into the Go module directory
      uses: actions/[email protected]

    - name: Get dependencies
      run: |
        go get -v -t -d ./...
        if [ -f Gopkg.toml ]; then
            curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
            dep ensure
        fi
    - name: Build
      run: go build -v .

  • 获取 tag name
1
2
    - name: Get Tag Name
      run: echo ::set-env name=TAG_NAME::$(echo $GITHUB_REF | sed "s/refs\/tags\///")
  • 计时
1
2
3
4
5
6
7
8
9
10
11
    - name: Begin Time
      run: echo ::set-env name=TIME_BEGIN::$(date +%s.%N) 

    # 其它步骤

    - name: End Time
      run: echo ::set-env name=TIME_END::$(date +%s.%N)
      
    - name: Cost Time
      run: TIME_BEGIN_S=$(echo $TIME_BEGIN | cut -d '.' -f 1) && TIME_BEGIN_NS=$(echo $TIME_BEGIN | cut -d '.' -f 2) && TIME_END_S=$(echo $TIME_END | cut -d '.' -f 1) && TIME_END_NS=$(echo $TIME_END | cut -d '.' -f 2) && echo ::set-env name=TIME_COST::$(( ( 10#$TIME_END_S - 10#$TIME_BEGIN_S ) * 1000 + ( 10#$TIME_END_NS / 1000000 - 10#$TIME_BEGIN_NS / 1000000 ) )) 
          

比如我添加一个 notify 功能, 而需要保密对应的 webhook 链接, 那么在 secrets 中添加 PUSH_URL 再在 actions 中使用:

1
2
    - name: Notify
      run: echo -e "${GITHUB_REPOSITORY} done.\n${TIME_COST} ms." | curl --silent -X POST $ --data-binary @- 

go.yml 最终效果

监听 tags 的创建, 执行 build.sh 编译, 将编译出来的文件自动发布到同名的 Release Assets, 并且成功后将耗时通知给设定的 webhook.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
name: Go
on:
  push:
    tags:
      - 'v*.*.*'
      
jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
      
    steps:
    
    - name: Begin Time
      run: echo ::set-env name=TIME_BEGIN::$(date +%s.%N) 

    - name: Set up Go 1.12
      uses: actions/[email protected]
      with:
        go-version: 1.12
      id: go

    - name: Check out code into the Go module directory
      uses: actions/[email protected]

    - name: Get dependencies
      run: |
        go get -v -t -d ./...
        if [ -f Gopkg.toml ]; then
            curl https://raw.githubusercontent.com/golang/dep/master/install.sh | sh
            dep ensure
        fi

    - name: Build
      run: sh build.sh
      
    - name: Generate Changelog
      run:  echo "# Generated by Actions" > ${{ github.workflow }}-Notes.txt
    
    - name: GH Release
      uses: softprops/[email protected]
      with:
        files: builds/*
        body_path: ${{ github.workflow }}-Notes.txt
      env:
        GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
        
        
    - name: End Time
      run: echo ::set-env name=TIME_END::$(date +%s.%N)
      
    - name: Cost Time
      run: TIME_BEGIN_S=$(echo $TIME_BEGIN | cut -d '.' -f 1) && TIME_BEGIN_NS=$(echo $TIME_BEGIN | cut -d '.' -f 2) && TIME_END_S=$(echo $TIME_END | cut -d '.' -f 1) && TIME_END_NS=$(echo $TIME_END | cut -d '.' -f 2) && echo ::set-env name=TIME_COST::$(( ( 10#$TIME_END_S - 10#$TIME_BEGIN_S ) * 1000 + ( 10#$TIME_END_NS / 1000000 - 10#$TIME_BEGIN_NS / 1000000 ) )) 
      
    - name: Notify
      run: echo -e "${GITHUB_REPOSITORY} done.\n${TIME_COST} ms." | curl --silent -X POST ${{ secrets.PUSH_URL }} --data-binary @- 

Rust

Android

比较简单

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
name: Build & Publish Debug APK

on:
  push:
    branches:
      - master

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - uses: actions/[email protected]
        with:
          submodules: true
      - name: set up JDK 1.8
        uses: actions/[email protected]
        with:
          java-version: 1.8
      - name: Make Gradle executable
        run: chmod +x ./gradlew
      - name: Build with Gradle
        run: ./gradlew build
      - name: Build Debug APK
        run: ./gradlew assembleDebug
      - name: Releasing using Hub
        uses: ShaunLWM/[email protected]
        env:
          GITHUB_TOKEN: $
          APP_FOLDER: app
          RELEASE_TITLE: New Build

Other

by @GNU Guix
学 vps2arch 把 archlinux 的 docker 镜像解压到 / 并运行

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
name: CI

on: [push]

jobs:
  build:
    runs-on: ubuntu-18.04
    steps:
    - uses: actions/[email protected]
    - name: Extract Archlinux
      run: |
        wget -O /tmp/undocker.py https://github.com/larsks/undocker/raw/c951f021e701b4ce61de03eb668a440e69646889/undocker.py
        sudo mkdir -p /rootfs
        docker pull archlinux/base
        docker save archlinux/base | sudo python3 /tmp/undocker.py -o /rootfs archlinux/base
        docker rmi archlinux/base
        rm -fr /tmp/undocker.py
        sudo sh -c "
        systemctl stop docker
        mkdir /rootfs/old.rootfs
        mount --bind / /rootfs/old.rootfs
        chroot /rootfs /bin/sh -c '
        cd /old.rootfs &&
        rm -fr sbin bin lib usr lib64 var/mail var/spool/mail &&
        cd /old.rootfs/etc/ &&
        cp -Pfr passwd group shadow hosts resolv.conf hostname sudoers sudoers.d /etc/ &&
        cd / &&
        cp -Pfr bin etc home lib lib64 mnt opt root run sbin srv usr var /old.rootfs
        ' &&
        umount /rootfs/old.rootfs &&
        rm -fr /rootfs/ &&
        pacman --noconfirm --force -Syu gzip pacman sed systemd base sudo docker git
        "

定时任务

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
name: CI

on:
  schedule:
    # * is a special character in YAML so you have to quote this string
    # 注意是 UTC
    - cron:  '30 4 * * *'

jobs:
  build:

    runs-on: ubuntu-latest

    steps:
    - name: Notify
      env:
        TZ: Asia/Shanghai
      run: echo -e "${GITHUB_REPOSITORY} $(date "+%Y-%m-%d %H:%M:%S")" | curl --silent -X POST $ --data-binary @-