From be37473662d5bb20344130c7a4c8393af8f9a857 Mon Sep 17 00:00:00 2001 From: Marcos Lilljedahl Date: Fri, 7 Jun 2024 15:22:06 -0300 Subject: [PATCH] ci: daggerize go.yml pipeline Signed-off-by: Marcos Lilljedahl --- .github/workflows/go.yml | 127 +++++++++++++--------------- .github/workflows/golangci-lint.yml | 14 +-- .golangci.yml | 4 + Makefile.common | 2 +- dagger.json | 12 +++ dagger/.gitattributes | 4 + dagger/.gitignore | 4 + dagger/go.mod | 35 ++++++++ dagger/go.sum | 71 ++++++++++++++++ dagger/main.go | 83 ++++++++++++++++++ 10 files changed, 279 insertions(+), 77 deletions(-) create mode 100644 dagger.json create mode 100644 dagger/.gitattributes create mode 100644 dagger/.gitignore create mode 100644 dagger/go.mod create mode 100644 dagger/go.sum create mode 100644 dagger/main.go diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml index fa161c8..dfff260 100644 --- a/.github/workflows/go.yml +++ b/.github/workflows/go.yml @@ -1,66 +1,61 @@ ---- -name: Go -on: - pull_request: - push: - branches: - - main - - "release-*" - -concurrency: - group: ${{ github.workflow }}-${{ (github.event.pull_request && github.event.pull_request.number) || github.ref || github.run_id }} - cancel-in-progress: true - -# Minimal permissions to be inherited by any job that don't declare it's own permissions -permissions: - contents: read - -jobs: - supportedVersions: - name: Fetch supported Go versions - runs-on: ubuntu-latest - outputs: - supported_versions: ${{ steps.matrix.outputs.supported_versions }} - steps: - - name: Checkout code - uses: actions/checkout@v4 - - name: Read supported_go_versions.txt - id: matrix - run: | - versions=$(cat supported_go_versions.txt) - matrix="[$(echo "$versions" | sed 's/\(.*\)/"\1"/' | paste -s -d,)]" - echo "supported_versions=$matrix" >> $GITHUB_OUTPUT - - test: - name: Tests - runs-on: ubuntu-latest - needs: supportedVersions - - strategy: - matrix: - go_version: ${{ fromJSON(needs.supportedVersions.outputs.supported_versions) }} - - steps: - - name: Checkout code - uses: actions/checkout@v4.1.1 - - - name: Set up Go ${{ matrix.go_version }} - uses: actions/setup-go@v5.0.0 - with: - go-version: ${{ matrix.go_version }} - - - name: Cache Go modules - id: cache - uses: actions/cache@v4 - with: - path: ~/go/pkg/mod - key: v1-go${{ matrix.go_version }} - - - name: Run tests and check license - run: make check_license test - env: - CI: true - - - name: Run style and unused - if: ${{ matrix.go_version == '1.20' }} - run: make style unused +--- +name: Go +on: + pull_request: + push: + branches: + - main + - "release-*" + +concurrency: + group: ${{ github.workflow }}-${{ (github.event.pull_request && github.event.pull_request.number) || github.ref || github.run_id }} + cancel-in-progress: true + +# Minimal permissions to be inherited by any job that don't declare it's own permissions +permissions: + contents: read + +jobs: + + supportedVersions: + name: Fetch supported Go versions + runs-on: ubuntu-latest + outputs: + supported_versions: ${{ steps.matrix.outputs.supported_versions }} + steps: + - name: Checkout code + uses: actions/checkout@v4 + - name: Read supported_go_versions.txt + id: matrix + run: | + versions=$(cat supported_go_versions.txt) + matrix="[$(echo "$versions" | sed 's/\(.*\)/"\1"/' | paste -s -d,)]" + echo "supported_versions=$matrix" >> $GITHUB_OUTPUT + + test: + name: Tests + runs-on: ubuntu-latest + needs: supportedVersions + + strategy: + matrix: + go_version: ${{ fromJSON(needs.supportedVersions.outputs.supported_versions) }} + + steps: + - name: Checkout code + uses: actions/checkout@v4.1.1 + + - name: Run tests and check license + uses: dagger/dagger-for-github@v5 + with: + version: "latest" + verb: call + args: -vvv --src . make --go-version ${{matrix.go_version}} --args 'check_license test' + + - name: Run style and unused + uses: dagger/dagger-for-github@v5 + if: ${{ matrix.go_version == '1.20' }} + with: + version: "latest" + verb: call + args: -vvv --src . make --args 'check_license test' diff --git a/.github/workflows/golangci-lint.yml b/.github/workflows/golangci-lint.yml index 5ceb590..36ab60d 100644 --- a/.github/workflows/golangci-lint.yml +++ b/.github/workflows/golangci-lint.yml @@ -25,15 +25,9 @@ jobs: steps: - name: Checkout repository uses: actions/checkout@a5ac7e51b41094c92402da3b24376905380afc29 # v4.1.6 - - name: Install Go - uses: actions/setup-go@0c52d547c9bc32b1aa3301fd7a9cb496313a4491 # v5.0.0 - with: - go-version: 1.22.x - - name: Install snmp_exporter/generator dependencies - run: sudo apt-get update && sudo apt-get -y install libsnmp-dev - if: github.repository == 'prometheus/snmp_exporter' - name: Lint - uses: golangci/golangci-lint-action@a4f60bb28d35aeee14e6880718e0c85ff1882e64 # v6.0.1 + uses: dagger/dagger-for-github@v5 with: - args: --verbose - version: v1.59.0 + version: "latest" + verb: call + args: -vvv --src . make --args lint diff --git a/.golangci.yml b/.golangci.yml index 8043d57..0d938da 100644 --- a/.golangci.yml +++ b/.golangci.yml @@ -4,6 +4,10 @@ run: skip-files: # Skip autogenerated files. - ^.*\.(pb|y)\.go$ + - dagger/dagger.gen.go + skip-dirs: + - dagger/internal + output: sort-results: true diff --git a/Makefile.common b/Makefile.common index 1617292..1a6e3d2 100644 --- a/Makefile.common +++ b/Makefile.common @@ -121,7 +121,7 @@ common-style: .PHONY: common-check_license common-check_license: @echo ">> checking license header" - @licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*') ; do \ + @licRes=$$(for file in $$(find . -type f -iname '*.go' ! -path './vendor/*' ! -path './dagger/internal/*') ; do \ awk 'NR<=3' $$file | grep -Eq "(Copyright|generated|GENERATED)" || echo $$file; \ done); \ if [ -n "$${licRes}" ]; then \ diff --git a/dagger.json b/dagger.json new file mode 100644 index 0000000..2ff709e --- /dev/null +++ b/dagger.json @@ -0,0 +1,12 @@ +{ + "name": "client_golang", + "sdk": "go", + "dependencies": [ + { + "name": "golang", + "source": "github.com/kpenfound/dagger-modules/golang@fd1a6e75721454d8ee3bcb0bd1a94d6bb4be1737" + } + ], + "source": "dagger", + "engineVersion": "v0.11.6" +} diff --git a/dagger/.gitattributes b/dagger/.gitattributes new file mode 100644 index 0000000..3a45493 --- /dev/null +++ b/dagger/.gitattributes @@ -0,0 +1,4 @@ +/dagger.gen.go linguist-generated +/internal/dagger/** linguist-generated +/internal/querybuilder/** linguist-generated +/internal/telemetry/** linguist-generated diff --git a/dagger/.gitignore b/dagger/.gitignore new file mode 100644 index 0000000..7ebabcc --- /dev/null +++ b/dagger/.gitignore @@ -0,0 +1,4 @@ +/dagger.gen.go +/internal/dagger +/internal/querybuilder +/internal/telemetry diff --git a/dagger/go.mod b/dagger/go.mod new file mode 100644 index 0000000..8a76b5e --- /dev/null +++ b/dagger/go.mod @@ -0,0 +1,35 @@ +module dagger + +go 1.21.7 + +require ( + github.com/99designs/gqlgen v0.17.44 + github.com/Khan/genqlient v0.7.0 + github.com/vektah/gqlparser/v2 v2.5.11 + go.opentelemetry.io/otel v1.26.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.26.0 + go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.26.0 + go.opentelemetry.io/otel/sdk v1.26.0 + go.opentelemetry.io/otel/trace v1.26.0 + golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa + golang.org/x/sync v0.7.0 + google.golang.org/grpc v1.63.2 +) + +require ( + github.com/cenkalti/backoff/v4 v4.3.0 // indirect + github.com/go-logr/logr v1.4.1 // indirect + github.com/go-logr/stdr v1.2.2 // indirect + github.com/google/uuid v1.6.0 // indirect + github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 // indirect + github.com/sosodev/duration v1.2.0 // indirect + go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 // indirect + go.opentelemetry.io/otel/metric v1.26.0 // indirect + go.opentelemetry.io/proto/otlp v1.2.0 // indirect + golang.org/x/net v0.23.0 // indirect + golang.org/x/sys v0.19.0 // indirect + golang.org/x/text v0.14.0 // indirect + google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de // indirect + google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda // indirect + google.golang.org/protobuf v1.33.0 // indirect +) diff --git a/dagger/go.sum b/dagger/go.sum new file mode 100644 index 0000000..ac31010 --- /dev/null +++ b/dagger/go.sum @@ -0,0 +1,71 @@ +github.com/99designs/gqlgen v0.17.44 h1:OS2wLk/67Y+vXM75XHbwRnNYJcbuJd4OBL76RX3NQQA= +github.com/99designs/gqlgen v0.17.44/go.mod h1:UTCu3xpK2mLI5qcMNw+HKDiEL77it/1XtAjisC4sLwM= +github.com/Khan/genqlient v0.7.0 h1:GZ1meyRnzcDTK48EjqB8t3bcfYvHArCUUvgOwpz1D4w= +github.com/Khan/genqlient v0.7.0/go.mod h1:HNyy3wZvuYwmW3Y7mkoQLZsa/R5n5yIRajS1kPBvSFM= +github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883 h1:bvNMNQO63//z+xNgfBlViaCIJKLlCJ6/fmUseuG0wVQ= +github.com/andreyvit/diff v0.0.0-20170406064948-c7f18ee00883/go.mod h1:rCTlJbsFo29Kk6CurOXKm700vrz8f0KW0JNfpkRJY/8= +github.com/cenkalti/backoff/v4 v4.3.0 h1:MyRJ/UdXutAwSAT+s3wNd7MfTIcy71VQueUuFK343L8= +github.com/cenkalti/backoff/v4 v4.3.0/go.mod h1:Y3VNntkOUPxTVeUxJ/G5vcM//AlwfmyYozVcomhLiZE= +github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c= +github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/go-logr/logr v1.2.2/go.mod h1:jdQByPbusPIv2/zmleS9BjJVeZ6kBagPoEUsqbVz/1A= +github.com/go-logr/logr v1.4.1 h1:pKouT5E8xu9zeFC39JXRDukb6JFQPXM5p5I91188VAQ= +github.com/go-logr/logr v1.4.1/go.mod h1:9T104GzyrTigFIr8wt5mBrctHMim0Nb2HLGrmQ40KvY= +github.com/go-logr/stdr v1.2.2 h1:hSWxHoqTgW2S2qGc0LTAI563KZ5YKYRhT3MFKZMbjag= +github.com/go-logr/stdr v1.2.2/go.mod h1:mMo/vtBO5dYbehREoey6XUKy/eSumjCCveDpRre4VKE= +github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI= +github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY= +github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0= +github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1 h1:/c3QmbOGMGTOumP2iT/rCwB7b0QDGLKzqOmktBjT+Is= +github.com/grpc-ecosystem/grpc-gateway/v2 v2.19.1/go.mod h1:5SN9VR2LTsRFsrEC6FHgRbTWrTHu6tqPeKxEQv15giM= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/sergi/go-diff v1.3.1 h1:xkr+Oxo4BOQKmkn/B9eMK0g5Kg/983T9DqqPHwYqD+8= +github.com/sergi/go-diff v1.3.1/go.mod h1:aMJSSKb2lpPvRNec0+w3fl7LP9IOFzdc9Pa4NFbPK1I= +github.com/sosodev/duration v1.2.0 h1:pqK/FLSjsAADWY74SyWDCjOcd5l7H8GSnnOGEB9A1Us= +github.com/sosodev/duration v1.2.0/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg= +github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg= +github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY= +github.com/vektah/gqlparser/v2 v2.5.11 h1:JJxLtXIoN7+3x6MBdtIP59TP1RANnY7pXOaDnADQSf8= +github.com/vektah/gqlparser/v2 v2.5.11/go.mod h1:1rCcfwB2ekJofmluGWXMSEnPMZgbxzwj6FaZ/4OT8Cc= +go.opentelemetry.io/otel v1.26.0 h1:LQwgL5s/1W7YiiRwxf03QGnWLb2HW4pLiAhaA5cZXBs= +go.opentelemetry.io/otel v1.26.0/go.mod h1:UmLkJHUAidDval2EICqBMbnAd0/m2vmpf/dAM+fvFs4= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0 h1:1u/AyyOqAWzy+SkPxDpahCNZParHV8Vid1RnI2clyDE= +go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.26.0/go.mod h1:z46paqbJ9l7c9fIPCXTqTGwhQZ5XoTIsfeFYWboizjs= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.26.0 h1:Waw9Wfpo/IXzOI8bCB7DIk+0JZcqqsyn1JFnAc+iam8= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.26.0/go.mod h1:wnJIG4fOqyynOnnQF/eQb4/16VlX2EJAHhHgqIqWfAo= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.26.0 h1:1wp/gyxsuYtuE/JFxsQRtcCDtMrO2qMvlfXALU5wkzI= +go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.26.0/go.mod h1:gbTHmghkGgqxMomVQQMur1Nba4M0MQ8AYThXDUjsJ38= +go.opentelemetry.io/otel/metric v1.26.0 h1:7S39CLuY5Jgg9CrnA9HHiEjGMF/X2VHvoXGgSllRz30= +go.opentelemetry.io/otel/metric v1.26.0/go.mod h1:SY+rHOI4cEawI9a7N1A4nIg/nTQXe1ccCNWYOJUrpX4= +go.opentelemetry.io/otel/sdk v1.26.0 h1:Y7bumHf5tAiDlRYFmGqetNcLaVUZmh4iYfmGxtmz7F8= +go.opentelemetry.io/otel/sdk v1.26.0/go.mod h1:0p8MXpqLeJ0pzcszQQN4F0S5FVjBLgypeGSngLsmirs= +go.opentelemetry.io/otel/trace v1.26.0 h1:1ieeAUb4y0TE26jUFrCIXKpTuVK7uJGN9/Z/2LP5sQA= +go.opentelemetry.io/otel/trace v1.26.0/go.mod h1:4iDxvGDQuUkHve82hJJ8UqrwswHYsZuWCBllGV2U2y0= +go.opentelemetry.io/proto/otlp v1.2.0 h1:pVeZGk7nXDC9O2hncA6nHldxEjm6LByfA2aN8IOkz94= +go.opentelemetry.io/proto/otlp v1.2.0/go.mod h1:gGpR8txAl5M03pDhMC79G6SdqNV26naRm/KDsgaHD8A= +go.uber.org/goleak v1.3.0 h1:2K3zAYmnTNqV73imy9J1T3WC+gmCePx2hEGkimedGto= +go.uber.org/goleak v1.3.0/go.mod h1:CoHD4mav9JJNrW/WLlf7HGZPjdw8EucARQHekz1X6bE= +golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa h1:FRnLl4eNAQl8hwxVVC17teOw8kdjVDVAiFMtgUdTSRQ= +golang.org/x/exp v0.0.0-20231110203233-9a3e6036ecaa/go.mod h1:zk2irFbV9DP96SEBUUAy67IdHUaZuSnrz1n472HUCLE= +golang.org/x/net v0.23.0 h1:7EYJ93RZ9vYSZAIb2x3lnuvqO5zneoD6IvWjuhfxjTs= +golang.org/x/net v0.23.0/go.mod h1:JKghWKKOSdJwpW2GEx0Ja7fmaKnMsbu+MWVZTokSYmg= +golang.org/x/sync v0.7.0 h1:YsImfSBoP9QPYL0xyKJPq0gcaJdG3rInoqxTWbfQu9M= +golang.org/x/sync v0.7.0/go.mod h1:Czt+wKu1gCyEFDUtn0jG5QVvpJ6rzVqr5aXyt9drQfk= +golang.org/x/sys v0.19.0 h1:q5f1RH2jigJ1MoAWp2KTp3gm5zAGFUTarQZ5U386+4o= +golang.org/x/sys v0.19.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA= +golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ= +golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU= +google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de h1:F6qOa9AZTYJXOUEr4jDysRDLrm4PHePlge4v4TGAlxY= +google.golang.org/genproto v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:VUhTRKeHn9wwcdrk73nvdC9gF178Tzhmt/qyaFcPLSo= +google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de h1:jFNzHPIeuzhdRwVhbZdiym9q0ory/xY3sA+v2wPg8I0= +google.golang.org/genproto/googleapis/api v0.0.0-20240227224415-6ceb2ff114de/go.mod h1:5iCWqnniDlqZHrd3neWVTOwvh/v6s3232omMecelax8= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda h1:LI5DOvAxUPMv/50agcLLoo+AdWc1irS9Rzz4vPuD1V4= +google.golang.org/genproto/googleapis/rpc v0.0.0-20240401170217-c3f982113cda/go.mod h1:WtryC6hu0hhx87FDGxWCDptyssuo68sk10vYjF+T9fY= +google.golang.org/grpc v1.63.2 h1:MUeiw1B2maTVZthpU5xvASfTh3LDbxHd6IJ6QQVU+xM= +google.golang.org/grpc v1.63.2/go.mod h1:WAX/8DgncnokcFUldAxq7GeB5DXHDbMF+lLvDomNkRA= +google.golang.org/protobuf v1.33.0 h1:uNO2rsAINq/JlFpSdYEKIZ0uKD/R9cpdv0T+yoGwGmI= +google.golang.org/protobuf v1.33.0/go.mod h1:c6P6GXX6sHbq/GpV6MGZEdwhWPcYBgnhAHhKbcUYpos= +gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA= +gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= diff --git a/dagger/main.go b/dagger/main.go new file mode 100644 index 0000000..63361b0 --- /dev/null +++ b/dagger/main.go @@ -0,0 +1,83 @@ +// Copyright 2024 The Prometheus Authors +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +// A minimal example of how to include Prometheus instrumentation. + +package main + +import ( + "context" + "strings" + + "golang.org/x/sync/errgroup" +) + +type ClientGolang struct { + Source *Directory // +private +} + +func New(src *Directory) *ClientGolang { + return &ClientGolang{Source: src} +} + +// runs `make` with the given arguments +func (m *ClientGolang) Make( + // +optional + args string, + // +default="1.20" + goVersion string, + // +optional + env []string, +) (string, error) { + return dag.Golang(). + Base(goVersion). + Container(). + WithMountedDirectory("/src", m.Source). + WithWorkdir("/src"). + WithMountedCache("/go/bin", dag.CacheVolume("gobincache")). + WithExec([]string{"sh", "-c", "make " + args}). + Stdout(context.Background()) +} + +// runs `make` with the given arguments for all supported go versions +func (m *ClientGolang) MakeAll( + ctx context.Context, + // +optional, + args string, +) error { + c, err := m.Source.File("supported_go_versions.txt").Contents(ctx) + if err != nil { + return err + } + goVersions := strings.Split(c, "\n") + + eg := new(errgroup.Group) + + for _, version := range goVersions { + version := version + if len(version) > 0 { + eg.Go(func() error { + _, err := dag.Golang(). + Base(version). + Container(). + WithMountedDirectory("/src", m.Source). + WithWorkdir("/src"). + WithMountedCache("/go/bin", dag.CacheVolume("gobincache")). + WithExec([]string{"sh", "-c", "make " + args}).Sync(ctx) + return err + }) + } + } + + return eg.Wait() +}