Skip to content

Commit 67c690b

Browse files
committed
init CI
1 parent 52996e2 commit 67c690b

File tree

5 files changed

+397
-0
lines changed

5 files changed

+397
-0
lines changed

.github/workflows/e2e.yml

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
name: E2E Test Check
2+
on:
3+
pull_request:
4+
branches:
5+
- master
6+
- main
7+
types: [ 'opened', 'synchronize' ]
8+
paths:
9+
- '.github/**'
10+
- '.github/workflows/**'
11+
- '**/*.tf'
12+
13+
jobs:
14+
e2e-check:
15+
# if: github.event.review.state == 'approved' || github.event.review.body == 'approved'
16+
runs-on: ubuntu-latest
17+
name: 'e2e check'
18+
steps:
19+
- name: checkout
20+
uses: actions/checkout@v3
21+
- name: set id
22+
id: set-job-id
23+
uses: ayachensiyuan/[email protected]
24+
env:
25+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
26+
with:
27+
job-name: 'e2e check'
28+
- name: Get pull request info
29+
run: |
30+
echo "repo name is" ${{github.event.pull_request.head.repo.full_name}}
31+
echo "branch is" ${{github.event.pull_request.head.ref}}
32+
echo "The current job id is ${{ steps.set-job-id.outputs.jobId }}"
33+
- name: e2e test
34+
run: |
35+
objectPath="github-action/${{github.repository}}/e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}"
36+
go run scripts/curl_fc_trigger.go ${{github.event.pull_request.head.ref}} ${{github.event.pull_request.head.repo.full_name}} ${objectPath}
37+
go run scripts/e2e_check.go ${objectPath}

.github/workflows/weekly_e2e.yml

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
name: Weekly E2E Test Check
2+
on:
3+
workflow_dispatch:
4+
schedule:
5+
- cron: '0 0 * * 0'
6+
7+
jobs:
8+
weekly-e2e-check:
9+
if: github.repository_owner == 'alibabacloud-automation'
10+
name: 'weekly e2e check'
11+
runs-on: ubuntu-latest
12+
permissions: write-all
13+
steps:
14+
- name: checkout
15+
uses: actions/checkout@v3
16+
- name: set id
17+
id: set-job-id
18+
uses: ayachensiyuan/[email protected]
19+
env:
20+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
with:
22+
job-name: 'weekly e2e check'
23+
- name: Get job id
24+
run: |
25+
echo "The current job id is ${{ steps.set-job-id.outputs.jobId }}"
26+
- name: Extract branch name
27+
shell: bash
28+
run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})"
29+
id: extract_branch
30+
- name: weekly e2e test
31+
run: |
32+
objectPath="github-action/${{github.repository}}/weekly-e2e/Action-${{github.run_number}}-${{github.run_id}}-${{ steps.set-job-id.outputs.jobId }}"
33+
echo "default branch: ${{ steps.extract_branch.outputs.branch }}"
34+
go run scripts/curl_fc_trigger.go ${{ steps.extract_branch.outputs.branch }} ${{github.repository}} ${objectPath}
35+
go run scripts/e2e_check.go ${objectPath}
36+
- name: whether to upload test record
37+
id: whether-to-upload-test-record
38+
run: |
39+
REPO_NAME=$(echo $GITHUB_REPOSITORY | cut -d'/' -f2)
40+
modules=$(curl -sL "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com/testRecordReleased.json")
41+
if echo "$modules" | jq --arg key "$REPO_NAME" -re 'has($key)' | grep -q true; then
42+
echo "The key exists at the JSON object."
43+
echo "::set-output name=output_value::1"
44+
else
45+
echo "The key does not exist at the JSON object."
46+
echo "::set-output name=output_value::0"
47+
fi
48+
- name: update test record
49+
if: steps.whether-to-upload-test-record.outputs.output_value == 1
50+
run: |
51+
git add TestRecord.md
52+
cd .git
53+
sudo chmod -R a+rwX .
54+
sudo find . -type d -exec chmod g+s '{}' +
55+
- name: Commit & Push changes
56+
if: steps.whether-to-upload-test-record.outputs.output_value == 1
57+
uses: actions-js/push@master
58+
with:
59+
github_token: ${{ secrets.GITHUB_TOKEN }}
60+
message: 'Update TestRecord'
61+
branch: ${{ steps.extract_branch.outputs.branch }}

scripts/curl_fc_trigger.go

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package main
2+
3+
import (
4+
"crypto/rand"
5+
"encoding/json"
6+
"fmt"
7+
"io"
8+
"log"
9+
"math/big"
10+
"net/http"
11+
"os"
12+
"strings"
13+
)
14+
15+
var urlPrefix = "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com"
16+
17+
func main() {
18+
if len(os.Args)!=4{
19+
log.Println("[ERROR] invalid args")
20+
return
21+
}
22+
branch := strings.TrimSpace(os.Args[1])
23+
repoName := strings.TrimSpace(os.Args[2])
24+
ossObjectPath := strings.TrimSpace(os.Args[3])
25+
26+
// get trigger url
27+
fcTriggerUrl := urlPrefix + "/fcUrls.json"
28+
response, err := http.Get(fcTriggerUrl)
29+
if err != nil {
30+
log.Println("[ERROR] get fc trigger url failed")
31+
}
32+
defer response.Body.Close()
33+
34+
content, _ := io.ReadAll(response.Body)
35+
var data interface{}
36+
json.Unmarshal(content, &data)
37+
triggerMap := data.(map[string]interface{})
38+
39+
n, _ := rand.Int(rand.Reader, big.NewInt(100))
40+
index := int(n.Int64()) % len(triggerMap)
41+
triggerUrl := triggerMap[fmt.Sprintf("%d", index)]
42+
fmt.Println(triggerUrl)
43+
44+
// curl
45+
client := &http.Client{}
46+
req, err := http.NewRequest("GET", triggerUrl.(string),
47+
nil)
48+
if err != nil {
49+
panic(err)
50+
}
51+
req.Header.Add("X-Fc-Invocation-Type", "Async")
52+
53+
query := req.URL.Query()
54+
query.Add("branch", branch)
55+
query.Add("repo_name", repoName)
56+
query.Add("oss_object_path", ossObjectPath)
57+
req.URL.RawQuery = query.Encode()
58+
59+
if _, err := client.Do(req); err != nil {
60+
log.Printf("[ERROR] fail to trigger fc test, err: %s", err)
61+
}
62+
63+
}

scripts/e2e_check.go

Lines changed: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
package main
2+
3+
import (
4+
"fmt"
5+
"io"
6+
"log"
7+
"net/http"
8+
"os"
9+
"strings"
10+
"time"
11+
)
12+
13+
var urlPrefix = "https://terraform-fc-test-for-example-module.oss-ap-southeast-1.aliyuncs.com"
14+
15+
func main() {
16+
ossObjectPath := strings.TrimSpace(os.Args[1])
17+
log.Println("run log path:", ossObjectPath)
18+
runLogFileName := "terraform.run.log"
19+
runResultFileName := "terraform.run.result.log"
20+
runLogUrl := urlPrefix + "/" + ossObjectPath + "/" + runLogFileName
21+
runResultUrl := urlPrefix + "/" + ossObjectPath + "/" + runResultFileName
22+
lastLineNum := 0
23+
deadline := time.Now().Add(time.Duration(24) * time.Hour)
24+
finish := false
25+
exitCode := 0
26+
log.Println(runLogUrl)
27+
errResultMessage := ""
28+
for !time.Now().After(deadline) {
29+
runLogResponse, err := http.Get(runLogUrl)
30+
if err != nil || runLogResponse.StatusCode != 200 {
31+
log.Println("waiting for job running...")
32+
time.Sleep(5 * time.Second)
33+
continue
34+
}
35+
defer runLogResponse.Body.Close()
36+
37+
s, er := io.ReadAll(runLogResponse.Body)
38+
if er != nil && fmt.Sprint(er) != "EOF" {
39+
log.Println("[ERROR] reading run log response failed:", err)
40+
}
41+
lineNum := len(s)
42+
if runLogResponse.StatusCode == 200 {
43+
if lineNum > lastLineNum {
44+
fmt.Printf("%s", s[lastLineNum:lineNum])
45+
lastLineNum = lineNum
46+
}
47+
}
48+
if finish {
49+
log.Println("run log path:", ossObjectPath)
50+
log.Println("run log url:", runLogUrl)
51+
if strings.Contains(ossObjectPath, "weekly") {
52+
updateTestRecord(ossObjectPath)
53+
exitCode = 0
54+
}
55+
if errResultMessage != "" {
56+
log.Println("[ERROR] run result:", errResultMessage)
57+
}
58+
os.Exit(exitCode)
59+
}
60+
runResultResponse, err := http.Get(runResultUrl)
61+
if err != nil || runResultResponse.StatusCode != 200 {
62+
time.Sleep(5 * time.Second)
63+
continue
64+
}
65+
defer runResultResponse.Body.Close()
66+
runResultContent := make([]byte, 100000)
67+
_, err = runResultResponse.Body.Read(runResultContent)
68+
if err != nil && fmt.Sprint(err) != "EOF" {
69+
log.Println("[ERROR] reading run result response failed:", err)
70+
}
71+
finish = true
72+
if !strings.HasPrefix(string(runResultContent), "PASS") {
73+
errResultMessage = string(runResultContent)
74+
exitCode = 1
75+
}
76+
}
77+
log.Println("[ERROR] Timeout: waiting for job finished timeout after 24 hours.")
78+
}
79+
80+
func updateTestRecord(ossObjectPath string) {
81+
currentTestRecordFileName := "TestRecord.md"
82+
currentTestRecordFileUrl := urlPrefix + "/" + ossObjectPath + "/" + currentTestRecordFileName
83+
response, err := http.Get(currentTestRecordFileUrl)
84+
if err != nil {
85+
log.Println("[ERROR] failed to get test record from oss")
86+
return
87+
}
88+
defer response.Body.Close()
89+
data, _ := io.ReadAll(response.Body)
90+
if response.StatusCode != 200 || len(data) == 0 {
91+
return
92+
}
93+
currentTestRecord := string(data) + "\n"
94+
95+
testRecordFileName := "TestRecord.md"
96+
var testRecordFile *os.File
97+
oldTestRecord := ""
98+
if _, err := os.Stat(testRecordFileName); os.IsNotExist(err) {
99+
testRecordFile, err = os.Create(testRecordFileName)
100+
if err != nil {
101+
log.Println("[ERROR] failed to create test record file")
102+
}
103+
} else {
104+
data, err := os.ReadFile(testRecordFileName)
105+
if err != nil {
106+
log.Println("[ERROR] failed to read test record file")
107+
return
108+
}
109+
oldTestRecord = string(data)
110+
111+
testRecordFile, err = os.OpenFile(testRecordFileName, os.O_TRUNC|os.O_RDWR, 0666)
112+
if err != nil {
113+
log.Println("[ERROR] failed to open test record file")
114+
}
115+
}
116+
defer testRecordFile.Close()
117+
118+
currentTestRecord += oldTestRecord
119+
testRecordFile.WriteString(currentTestRecord)
120+
}

0 commit comments

Comments
 (0)