Use variables in pipeline (#1026)

use yaml aliases (https://yaml.org/spec/1.2.2/#3222-anchors-and-aliases) to have pipeline `variables`

Co-authored-by: qwerty287 <80460567+qwerty287@users.noreply.github.com>
Co-authored-by: Anbraten <anton@ju60.de>
This commit is contained in:
6543 2022-07-17 18:25:56 +02:00 committed by GitHub
parent 31bad81979
commit 8da0ee47f7
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
8 changed files with 173 additions and 184 deletions

View file

@ -2,153 +2,94 @@ depends_on:
- test - test
- web - web
variables:
- &golang_image 'golang:1.18'
- &node_image 'node:16-alpine'
- &when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
pipeline: pipeline:
build-web: build-web:
group: prepare group: prepare
image: node:16-alpine image: *node_image
commands: commands:
- cd web/ - cd web/
- yarn install --frozen-lockfile - yarn install --frozen-lockfile
- yarn build - yarn build
when: when:
path: path: *when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
vendor: vendor:
group: prepare group: prepare
image: golang:1.18 image: *golang_image
commands: commands:
- go mod vendor - go mod vendor
when: when:
path: path: *when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
build-server: build-server:
group: build group: build
image: golang:1.18 image: *golang_image
commands: commands:
- make release-server - make release-server
when: when:
path: path: *when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
build-agent: build-agent:
group: build group: build
image: golang:1.18 image: *golang_image
commands: commands:
- make release-agent - make release-agent
when: when:
path: path: *when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
build-cli: build-cli:
group: build group: build
image: golang:1.18 image: *golang_image
commands: commands:
- make release-cli - make release-cli
when: when:
path: path: *when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
build-deb-rpm: build-deb-rpm:
group: bundle group: bundle
image: golang:1.18 image: *golang_image
commands: commands:
- make bundle - make bundle
when: when:
path: path: *when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
build-tarball: build-tarball:
group: bundle group: bundle
image: golang:1.18 image: *golang_image
commands: commands:
- make release-tarball - make release-tarball
when: when:
path: path: *when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
checksums: checksums:
image: golang:1.18 image: *golang_image
commands: commands:
- make release-checksums - make release-checksums
when: when:
path: path: *when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
# TODO: upload build artifacts for pushes to ${CI_REPO_DEFAULT_BRANCH} # TODO: upload build artifacts for pushes to ${CI_REPO_DEFAULT_BRANCH}
release-dryrun: release-dryrun:
image: golang:1.18 image: *golang_image
commands: commands:
- ls -la dist/*.* - ls -la dist/*.*
- cat dist/checksums.txt - cat dist/checksums.txt
when: when:
path: path: *when_path
# related config files
- ".woodpecker/binaries.yml"
- "nfpm/*.yml"
# go source code
- "**/*.go"
- "go.*"
# web source code
- "web/**"
release: release:
image: plugins/github-release image: plugins/github-release

View file

@ -2,9 +2,15 @@ depends_on:
- test - test
- web - web
variables:
- &golang_image 'golang:1.18'
- &node_image 'node:16-alpine'
- &xgo_image 'techknowlogick/xgo:go-1.18.x'
- &xgo_version 'go-1.18.x'
pipeline: pipeline:
vendor: vendor:
image: golang:1.18 image: *golang_image
commands: commands:
- go mod vendor - go mod vendor
@ -12,14 +18,14 @@ pipeline:
# S e r v e r # # S e r v e r #
############### ###############
build-web: build-web:
image: node:16-alpine image: *node_image
commands: commands:
- cd web/ - cd web/
- yarn install --frozen-lockfile - yarn install --frozen-lockfile
- yarn build - yarn build
cross-compile-server: cross-compile-server:
image: techknowlogick/xgo:go-1.18.x image: *xgo_image
commands: commands:
- apt update - apt update
- apt install -y tree - apt install -y tree
@ -27,6 +33,7 @@ pipeline:
environment: environment:
PLATFORMS: linux|arm/v7;linux|arm64/v8;linux|amd64;linux|ppc64le PLATFORMS: linux|arm/v7;linux|arm64/v8;linux|amd64;linux|ppc64le
TAGS: bindata sqlite sqlite_unlock_notify TAGS: bindata sqlite sqlite_unlock_notify
XGO_VERSION: *xgo_version
publish-server-dryrun: publish-server-dryrun:
image: woodpeckerci/plugin-docker-buildx image: woodpeckerci/plugin-docker-buildx

View file

@ -1,63 +1,56 @@
pipeline: variables:
lint-pipeline: - &golang_image 'golang:1.18'
image: woodpeckerci/woodpecker-cli:next-alpine - &node_image 'node:16-alpine'
commands: - &when_path
- woodpecker-cli lint
when:
path:
- ".woodpecker/**"
vendor:
image: golang:1.18
group: prepare
commands:
- go mod vendor
when:
path:
# related config files
- ".woodpecker/test.yml"
# go source code
- "**/*.go"
- "go.*"
# schema changes
- "pipeline/schema/**"
dummy-web:
image: golang:1.18
group: prepare
commands:
- mkdir -p web/dist/
- echo "test" > web/dist/index.html
when:
path:
# related config files
- ".woodpecker/test.yml"
# go source code
- "**/*.go"
- "go.*"
# schema changes
- "pipeline/schema/**"
lint:
image: golang:1.18
group: test
commands:
- make lint
when:
path:
# related config files # related config files
- ".woodpecker/test.yml" - ".woodpecker/test.yml"
- ".golangci.yml" - ".golangci.yml"
# go source code # go source code
- "**/*.go" - "**/*.go"
- "go.*" - "go.*"
# schema changes
- "pipeline/schema/**"
pipeline:
lint-pipeline:
image: *golang_image
commands:
- go run github.com/woodpecker-ci/woodpecker/cmd/cli lint
when:
path:
- ".woodpecker/**"
vendor:
image: *golang_image
group: prepare
commands:
- go mod vendor
when:
path: *when_path
dummy-web:
image: *golang_image
group: prepare
commands:
- mkdir -p web/dist/
- echo "test" > web/dist/index.html
when:
path: *when_path
lint:
image: *golang_image
group: test
commands:
- make lint
when:
path: *when_path
lint-editorconfig: lint-editorconfig:
image: mstruebing/editorconfig-checker image: mstruebing/editorconfig-checker
group: test group: test
test: test:
image: golang:1.18 image: *golang_image
group: test group: test
commands: commands:
- make test-agent - make test-agent
@ -65,32 +58,20 @@ pipeline:
- make test-cli - make test-cli
- make test-lib - make test-lib
when: when:
path: path: *when_path
# related config files
- ".woodpecker/test.yml"
# go source code
- "**/*.go"
- "go.*"
# schema changes
- "pipeline/schema/**"
sqlite: sqlite:
image: golang:1.18 image: *golang_image
group: test group: test
environment: environment:
- WOODPECKER_DATABASE_DRIVER=sqlite3 - WOODPECKER_DATABASE_DRIVER=sqlite3
commands: commands:
- make test-server-datastore-coverage - make test-server-datastore-coverage
when: when:
path: path: *when_path
# related config files
- ".woodpecker/test.yml"
# go source code
- "**/*.go"
- "go.*"
postgres: postgres:
image: golang:1.18 image: *golang_image
group: test group: test
environment: environment:
- WOODPECKER_DATABASE_DRIVER=postgres - WOODPECKER_DATABASE_DRIVER=postgres
@ -98,15 +79,10 @@ pipeline:
commands: commands:
- make test-server-datastore - make test-server-datastore
when: when:
path: path: *when_path
# related config files
- ".woodpecker/test.yml"
# go source code
- "**/*.go"
- "go.*"
mysql: mysql:
image: golang:1.18 image: *golang_image
group: test group: test
environment: environment:
- WOODPECKER_DATABASE_DRIVER=mysql - WOODPECKER_DATABASE_DRIVER=mysql
@ -114,12 +90,7 @@ pipeline:
commands: commands:
- make test-server-datastore - make test-server-datastore
when: when:
path: path: *when_path
# related config files
- ".woodpecker/test.yml"
# go source code
- "**/*.go"
- "go.*"
codecov: codecov:
pull: true pull: true
@ -133,14 +104,7 @@ pipeline:
token: token:
from_secret: codecov_token from_secret: codecov_token
when: when:
path: path: *when_path
# related config files
- ".woodpecker/test.yml"
# go source code
- "**/*.go"
- "go.*"
# schema changes
- "pipeline/schema/**"
services: services:
service-postgres: service-postgres:
@ -150,12 +114,7 @@ services:
- POSTGRES_USER=postgres - POSTGRES_USER=postgres
- POSTGRES_HOST_AUTH_METHOD=trust - POSTGRES_HOST_AUTH_METHOD=trust
when: when:
path: path: *when_path
# related config files
- ".woodpecker/test.yml"
# go source code
- "**/*.go"
- "go.*"
service-mysql: service-mysql:
image: mysql:5.6.27 image: mysql:5.6.27
@ -164,9 +123,4 @@ services:
- MYSQL_DATABASE=test - MYSQL_DATABASE=test
- MYSQL_ALLOW_EMPTY_PASSWORD=yes - MYSQL_ALLOW_EMPTY_PASSWORD=yes
when: when:
path: path: *when_path
# related config files
- ".woodpecker/test.yml"
# go source code
- "**/*.go"
- "go.*"

View file

@ -1,45 +1,51 @@
variables:
- &node_image 'node:16-alpine'
- &when_path
# web source code
- "web/**"
pipeline: pipeline:
deps: deps:
image: node:16-alpine image: *node_image
commands: commands:
- cd web/ - cd web/
- yarn install --frozen-lockfile - yarn install --frozen-lockfile
when: when:
path: "web/**" path: *when_path
lint: lint:
group: test group: test
image: node:16-alpine image: *node_image
commands: commands:
- cd web/ - cd web/
- yarn lint - yarn lint
when: when:
path: "web/**" path: *when_path
formatcheck: formatcheck:
group: test group: test
image: node:16-alpine image: *node_image
commands: commands:
- cd web/ - cd web/
- yarn formatcheck - yarn formatcheck
when: when:
path: "web/**" path: *when_path
typecheck: typecheck:
group: test group: test
image: node:16-alpine image: *node_image
commands: commands:
- cd web/ - cd web/
- yarn typecheck - yarn typecheck
when: when:
path: "web/**" path: *when_path
test: test:
group: test group: test
image: node:16-alpine image: *node_image
commands: commands:
- cd web/ - cd web/
- yarn test - yarn test
when: when:
path: "web/**" path: *when_path

View file

@ -26,7 +26,8 @@ func lint(c *cli.Context) error {
} }
func lintDir(c *cli.Context, dir string) error { func lintDir(c *cli.Context, dir string) error {
return filepath.Walk(dir, func(path string, info os.FileInfo, e error) error { var errorStrings []string
if err := filepath.Walk(dir, func(path string, info os.FileInfo, e error) error {
if e != nil { if e != nil {
return e return e
} }
@ -34,13 +35,22 @@ func lintDir(c *cli.Context, dir string) error {
// check if it is a regular file (not dir) // check if it is a regular file (not dir)
if info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".yml") { if info.Mode().IsRegular() && strings.HasSuffix(info.Name(), ".yml") {
fmt.Println("#", info.Name()) fmt.Println("#", info.Name())
_ = lintFile(c, path) // TODO: should we drop errors or store them and report back? if err := lintFile(c, path); err != nil {
errorStrings = append(errorStrings, err.Error())
}
fmt.Println("") fmt.Println("")
return nil return nil
} }
return nil return nil
}) }); err != nil {
return err
}
if len(errorStrings) != 0 {
return fmt.Errorf("ERRORS: %s", strings.Join(errorStrings, "; "))
}
return nil
} }
func lintFile(_ *cli.Context, file string) error { func lintFile(_ *cli.Context, file string) error {

View file

@ -664,6 +664,12 @@ Example configuration to clone Mercurial repository:
+ path: bitbucket.org/foo/bar + path: bitbucket.org/foo/bar
``` ```
## `variables`
Woodpecker supports [YAML anchors & aliases](https://yaml.org/spec/1.2.2/#3222-anchors-and-aliases) in the pipeline configuration. These can be used as variables to not repeat yourself.
For more details and examples check the [Advanced YAML syntax docs](/docs/usage/advanced-yaml-syntax)
### Git Submodules ### Git Submodules
To use the credentials that cloned the repository to clone it's submodules, update `.gitmodules` to use `https` instead of `git`: To use the credentials that cloned the repository to clone it's submodules, update `.gitmodules` to use `https` instead of `git`:

View file

@ -0,0 +1,62 @@
# Advanced YAML syntax
## Anchors & aliases
You can use [YAML anchors & aliases](https://yaml.org/spec/1.2.2/#3222-anchors-and-aliases) as variables in your pipeline config.
To convert this:
```yml
pipeline:
test:
image: golang:1.18
command: go test ./...
build:
image: golang:1.18
command: build
```
Just add a new section called **variables** like this:
```diff
+variables:
+ - &golang_image 'golang:1.18'
pipeline:
test:
- image: golang:1.18
+ image: *golang_image
command: go test ./...
build:
- image: golang:1.18
+ image: *golang_image
command: build
```
## Example of YAML override and extension
```yml
variables:
&some-plugin-settings
settings:
target: dist
recursive: false
try: true
pipelines:
develop:
name: Build and test
image: some-plugin
settings: *some-plugin-settings
when:
branch: develop
main
name: Build and test
image: some-plugin
settings:
<<: *some-plugin-settings
try: false # replacing original value from `some-plugin-settings`
ongoing: false # adding a new value to `some-plugin-settings`
when:
branch: main
```

View file

@ -11,6 +11,9 @@
"type": "string", "type": "string",
"format": "uri" "format": "uri"
}, },
"variables": {
"description": "Use yaml aliases to define variables. Read more: https://woodpecker-ci.org/docs/usage/advanced-yaml-syntax"
},
"clone": { "$ref": "#/definitions/clone" }, "clone": { "$ref": "#/definitions/clone" },
"branches": { "$ref": "#/definitions/branches" }, "branches": { "$ref": "#/definitions/branches" },
"pipeline": { "$ref": "#/definitions/pipeline" }, "pipeline": { "$ref": "#/definitions/pipeline" },