From da3c38b0729d792d9e8f242d338c1ea09cb1884b Mon Sep 17 00:00:00 2001 From: rick Date: Sun, 18 Dec 2022 18:36:34 +0800 Subject: [PATCH] feat: support concurrency setting --- README.md | 4 ++ cmd/convert_test.go | 1 - cmd/data/combine-workflow.yaml | 2 - cmd/data/one-workflow-env.yaml | 1 - cmd/data/one-workflow.yaml | 1 - pkg/argo-workflow.go | 6 +- pkg/argo-workflow_test.go | 4 ++ pkg/data/argo-workflows-complex-event.yaml | 1 - pkg/data/argo-workflows-concurrency.yaml | 76 ++++++++++++++++++++++ pkg/data/argo-workflows-image.yaml | 1 - pkg/data/argo-workflows.yaml | 1 - pkg/data/github-actions-concurrency.yaml | 26 ++++++++ pkg/github.go | 7 +- 13 files changed, 119 insertions(+), 12 deletions(-) create mode 100644 pkg/data/argo-workflows-concurrency.yaml create mode 100644 pkg/data/github-actions-concurrency.yaml diff --git a/README.md b/README.md index 9897167..c144daa 100644 --- a/README.md +++ b/README.md @@ -82,9 +82,13 @@ spec: Considering [GitHub Workflows](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#jobsjob_idstepsuses) has a complex syntax. Currently, we support the following ones: +* [Event filter](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#on) + * Support `on.push` and `on.merge_request` * keyword `uses` * support `actions/checkout`, `actions/setup-go`, `goreleaser/goreleaser-action` and `docker://` * keyword `run` * keyword `env` +* [concurrency](https://docs.github.com/en/actions/using-workflows/workflow-syntax-for-github-actions#concurrency) + * Not support `cancel-in-progress` yet There are some limitations. For example, only the first job could be recognized in each file. diff --git a/cmd/convert_test.go b/cmd/convert_test.go index 08f1f2e..2bf859c 100644 --- a/cmd/convert_test.go +++ b/cmd/convert_test.go @@ -201,7 +201,6 @@ spec: resources: requests: storage: 64Mi - templates: - name: main dag: diff --git a/cmd/data/combine-workflow.yaml b/cmd/data/combine-workflow.yaml index ec3dc80..aabec2f 100644 --- a/cmd/data/combine-workflow.yaml +++ b/cmd/data/combine-workflow.yaml @@ -20,7 +20,6 @@ spec: resources: requests: storage: 64Mi - templates: - name: main dag: @@ -58,7 +57,6 @@ spec: resources: requests: storage: 64Mi - templates: - name: main dag: diff --git a/cmd/data/one-workflow-env.yaml b/cmd/data/one-workflow-env.yaml index 1be74c8..12366c2 100644 --- a/cmd/data/one-workflow-env.yaml +++ b/cmd/data/one-workflow-env.yaml @@ -18,7 +18,6 @@ spec: resources: requests: storage: 64Mi - templates: - name: main dag: diff --git a/cmd/data/one-workflow.yaml b/cmd/data/one-workflow.yaml index 3a46dff..c843ba0 100644 --- a/cmd/data/one-workflow.yaml +++ b/cmd/data/one-workflow.yaml @@ -18,7 +18,6 @@ spec: resources: requests: storage: 64Mi - templates: - name: main dag: diff --git a/pkg/argo-workflow.go b/pkg/argo-workflow.go index 612430b..2193373 100644 --- a/pkg/argo-workflow.go +++ b/pkg/argo-workflow.go @@ -258,7 +258,11 @@ spec: resources: requests: storage: 64Mi - + {{- if .Concurrency}} + synchronization: + mutex: + name: {{.Concurrency}} + {{- end}} templates: - name: main dag: diff --git a/pkg/argo-workflow_test.go b/pkg/argo-workflow_test.go index a0684ed..fd311d2 100644 --- a/pkg/argo-workflow_test.go +++ b/pkg/argo-workflow_test.go @@ -25,6 +25,10 @@ func TestWorkflow_ConvertToArgoWorkflow(t *testing.T) { name: "complex event", githubActions: "data/github-action-complex-event.yaml", argoWorkflows: "data/argo-workflows-complex-event.yaml", + }, { + name: "with concurrency", + githubActions: "data/github-actions-concurrency.yaml", + argoWorkflows: "data/argo-workflows-concurrency.yaml", }} for _, tt := range tests { t.Run(tt.name, func(t *testing.T) { diff --git a/pkg/data/argo-workflows-complex-event.yaml b/pkg/data/argo-workflows-complex-event.yaml index 0557b30..533fc14 100644 --- a/pkg/data/argo-workflows-complex-event.yaml +++ b/pkg/data/argo-workflows-complex-event.yaml @@ -18,7 +18,6 @@ spec: resources: requests: storage: 64Mi - templates: - name: main dag: diff --git a/pkg/data/argo-workflows-concurrency.yaml b/pkg/data/argo-workflows-concurrency.yaml new file mode 100644 index 0000000..bd0ffdf --- /dev/null +++ b/pkg/data/argo-workflows-concurrency.yaml @@ -0,0 +1,76 @@ +apiVersion: argoproj.io/v1alpha1 +kind: WorkflowTemplate +metadata: + name: build +spec: + entrypoint: main + arguments: + parameters: + - name: branch + default: master + - name: pr + default: -1 + volumeClaimTemplates: + - metadata: + name: work + spec: + accessModes: ["ReadWriteOnce"] + resources: + requests: + storage: 64Mi + synchronization: + mutex: + name: test + templates: + - name: main + dag: + tasks: + - name: clone + template: clone + - name: test + template: test + depends: clone + - name: goreleaser + template: goreleaser + depends: test + - name: clone + script: + image: alpine/git:v2.26.2 + command: [sh] + source: | + branch=$(echo {{workflow.parameters.branch}} | sed -e 's/refs\/heads\///g') + git clone --branch $branch https://gitee.com/LinuxSuRen/yaml-readme . + if [ {{workflow.parameters.pr}} != -1 ]; then + git fetch origin merge-requests/{{workflow.parameters.pr}}/head:mr-{{workflow.parameters.pr}} + git checkout mr-{{workflow.parameters.pr}} + fi + volumeMounts: + - mountPath: /work + name: work + workingDir: /work + - name: test + script: + image: golang:1.18 + command: [sh] + env: + - name: GOPROXY + value: https://goproxy.io,direct + source: | + go test ./... -coverprofile coverage.out + volumeMounts: + - mountPath: /work + name: work + workingDir: /work + - name: goreleaser + script: + image: goreleaser/goreleaser:v1.13.1 + command: [sh] + env: + - name: GOPROXY + value: https://goproxy.io,direct + source: | + goreleaser release --skip-publish --rm-dist + volumeMounts: + - mountPath: /work + name: work + workingDir: /work \ No newline at end of file diff --git a/pkg/data/argo-workflows-image.yaml b/pkg/data/argo-workflows-image.yaml index e7875c6..278a70a 100644 --- a/pkg/data/argo-workflows-image.yaml +++ b/pkg/data/argo-workflows-image.yaml @@ -18,7 +18,6 @@ spec: resources: requests: storage: 64Mi - templates: - name: main dag: diff --git a/pkg/data/argo-workflows.yaml b/pkg/data/argo-workflows.yaml index dab219f..6cd3e0e 100644 --- a/pkg/data/argo-workflows.yaml +++ b/pkg/data/argo-workflows.yaml @@ -18,7 +18,6 @@ spec: resources: requests: storage: 64Mi - templates: - name: main dag: diff --git a/pkg/data/github-actions-concurrency.yaml b/pkg/data/github-actions-concurrency.yaml new file mode 100644 index 0000000..98d4abb --- /dev/null +++ b/pkg/data/github-actions-concurrency.yaml @@ -0,0 +1,26 @@ +name: build +concurrency: test +jobs: + build: + name: build + runs-on: ubuntu-20.04 + steps: + - name: Set up Go 1.18 + uses: actions/setup-go@v3 + with: + go-version: 1.18 + id: go + - name: clone + uses: actions/checkout@v3.0.0 + - name: test + env: + GOPROXY: https://goproxy.io,direct + run: | + go test ./... -coverprofile coverage.out + - name: GoReleaser + uses: goreleaser/goreleaser-action@v2.9.1 + env: + GOPROXY: https://goproxy.io,direct + with: + version: latest + args: release --skip-publish --rm-dist diff --git a/pkg/github.go b/pkg/github.go index 765ae73..1acc219 100644 --- a/pkg/github.go +++ b/pkg/github.go @@ -5,9 +5,10 @@ import ( ) type Workflow struct { - Name string - On interface{} // could be: string, []string, Event - Jobs map[string]Job + Name string + On interface{} // could be: string, []string, Event + Jobs map[string]Job + Concurrency string // extra fields GitRepository string