Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
build: replace GoReleaser with Dagger
Browse files Browse the repository at this point in the history
Signed-off-by: Yann Soubeyrand <yann.soubeyrand@camptocamp.com>
yann-soubeyrand committed Jul 24, 2024
1 parent 0dccbce commit f90d9a7
Showing 11 changed files with 439 additions and 100 deletions.
52 changes: 26 additions & 26 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
@@ -1,38 +1,38 @@
name: goreleaser
name: "Release"

on:
push:
# run when new tag is pushed
tags:
- v*
# manually trigger the release
- "v*"

workflow_dispatch:
inputs:
tag:
type: "string"
required: true

jobs:
goreleaser:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@v3
with:
fetch-depth: 0
release:
name: "Release"
runs-on: "ubuntu-22.04"

- name: Set up Go
uses: actions/setup-go@v4
with:
go-version-file: ${{ github.workspace }}/go.mod
permissions:
contents: "read"
packages: "write"

- name: Login to GitHub Container Registry
uses: docker/login-action@v2
with:
registry: ghcr.io
username: ${{ github.actor }}
password: ${{ secrets.GITHUB_TOKEN }}
steps:
- name: "Check out repository"
id: "checkout"
uses: "actions/checkout@v4"

- name: Run GoReleaser
uses: goreleaser/goreleaser-action@v4
- name: "Set up Dagger"
id: "setup-dagger"
uses: "camptocamp/setup-dagger@v1"
with:
version: latest
args: release --clean
cloud-token: "${{ secrets.DAGGER_CLOUD_TOKEN }}"

- name: "Run Dagger"
id: "run-dagger"
run: "dagger call --version '${{ github.event_name == 'push' && github.ref || inputs.tag }}' release --github-token 'env:GITHUB_TOKEN'"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
17 changes: 17 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
name: "Test"

on:
workflow_dispatch:
inputs:
tag:
type: "string"
required: true

jobs:
one:
runs-on: "ubuntu-22.04"

steps:
- name: "Check out repository"
id: "checkout"
uses: "actions/checkout@v4"
46 changes: 0 additions & 46 deletions .goreleaser.yml

This file was deleted.

17 changes: 0 additions & 17 deletions Dockerfile

This file was deleted.

11 changes: 0 additions & 11 deletions Dockerfile.releaser

This file was deleted.

20 changes: 20 additions & 0 deletions dagger.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"name": "rds-exporter",
"sdk": "go",
"dependencies": [
{
"name": "github",
"source": "github.com/camptocamp/daggerverse/github@7e39d856e1626d94a012f2f1b05e238beeba1004"
},
{
"name": "golang",
"source": "github.com/camptocamp/daggerverse/golang@16a6c4a8c337532d2c8a5dd8bb2969aedb3cb845"
},
{
"name": "redhat",
"source": "github.com/camptocamp/daggerverse/redhat@f21280f6ba7a0d17c20b88776affcf8fba5cfd87"
}
],
"source": "dagger",
"engineVersion": "v0.12.2"
}
4 changes: 4 additions & 0 deletions dagger/.gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dagger.gen.go linguist-generated
/internal/dagger/** linguist-generated
/internal/querybuilder/** linguist-generated
/internal/telemetry/** linguist-generated
4 changes: 4 additions & 0 deletions dagger/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
/dagger.gen.go
/internal/dagger
/internal/querybuilder
/internal/telemetry
40 changes: 40 additions & 0 deletions dagger/go.mod
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module dagger/prometheus-rds-exporter

go 1.22.1

require (
github.com/99designs/gqlgen v0.17.49
github.com/Khan/genqlient v0.7.0
github.com/vektah/gqlparser/v2 v2.5.16
go.opentelemetry.io/otel v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240724073307-3e17ef99d67f
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.4.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0
go.opentelemetry.io/otel/log v0.4.0
go.opentelemetry.io/otel/sdk v1.28.0
go.opentelemetry.io/otel/sdk/log v0.4.0
go.opentelemetry.io/otel/trace v1.28.0
go.opentelemetry.io/proto/otlp v1.3.1
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56
golang.org/x/sync v0.7.0
google.golang.org/grpc v1.65.0
)

require (
github.com/cenkalti/backoff/v4 v4.3.0 // indirect
github.com/go-logr/logr v1.4.2 // 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.20.0 // indirect
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3 // indirect
github.com/sosodev/duration v1.3.1 // indirect
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 // indirect
go.opentelemetry.io/otel/metric v1.28.0 // indirect
golang.org/x/net v0.27.0 // indirect
golang.org/x/sys v0.22.0 // indirect
golang.org/x/text v0.16.0 // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a // indirect
google.golang.org/protobuf v1.34.2 // indirect
)
87 changes: 87 additions & 0 deletions dagger/go.sum
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
github.com/99designs/gqlgen v0.17.49 h1:b3hNGexHd33fBSAd4NDT/c3NCcQzcAVkknhN9ym36YQ=
github.com/99designs/gqlgen v0.17.49/go.mod h1:tC8YFVZMed81x7UJ7ORUwXF4Kn6SXuucFqQBhN8+BU0=
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.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
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.2 h1:6pFjapn8bFcIbiKo3XT4j/BhANplGihG6tvd+8rYgrY=
github.com/go-logr/logr v1.4.2/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.20.0 h1:bkypFPDjIYGfCYD5mRBvpqxfYX1YCS1PXdKYWi8FsN0=
github.com/grpc-ecosystem/grpc-gateway/v2 v2.20.0/go.mod h1:P+Lt/0by1T8bfcF3z737NnSbmxQAppXMRziHUxPOC8k=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
github.com/kr/text v0.1.0/go.mod h1:4Jbv+DJW3UT/LiOwJeYQe1efqtUx/iVham/4vfdArNI=
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.2-0.20230802210424-5b0b94c5c0d3 h1:n661drycOFuPLCN3Uc8sB6B/s6Z4t2xvBgU1htSHuq8=
github.com/sergi/go-diff v1.3.2-0.20230802210424-5b0b94c5c0d3/go.mod h1:A0bzQcvG0E7Rwjx0REVgAGH58e96+X0MeOfepqsbeW4=
github.com/sosodev/duration v1.3.1 h1:qtHBDMQ6lvMQsL15g4aopM4HEfOaYuhWBw3NPTtlqq4=
github.com/sosodev/duration v1.3.1/go.mod h1:RQIBBX0+fMLc/D9+Jb/fwvVmo0eZvDDEERAikUR6SDg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4=
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.16 h1:1gcmLTvs3JLKXckwCwlUagVn/IlV2bwqle0vJ0vy5p8=
github.com/vektah/gqlparser/v2 v2.5.16/go.mod h1:1lz1OeCqgQbQepsGxPVywrjdBHW2T08PUS3pJqepRww=
go.opentelemetry.io/otel v1.28.0 h1:/SqNcYk+idO0CxKEUOtKQClMK/MimZihKYMruSMViUo=
go.opentelemetry.io/otel v1.28.0/go.mod h1:q68ijF8Fc8CnMHKyzqL6akLO46ePnjkgfIMIjUIX9z4=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240724073307-3e17ef99d67f h1:aUt57tYTGKvJRrUtGhVbCqhKpnzUG3Pyc8TFFdexNTo=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploggrpc v0.0.0-20240724073307-3e17ef99d67f/go.mod h1:QTOeqAXGd+67ZhTD7UhOmRBQmdUQEU2XxoRP7RBTCug=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.4.0 h1:zBPZAISA9NOc5cE8zydqDiS0itvg/P/0Hn9m72a5gvM=
go.opentelemetry.io/otel/exporters/otlp/otlplog/otlploghttp v0.4.0/go.mod h1:gcj2fFjEsqpV3fXuzAA+0Ze1p2/4MJ4T7d77AmkvueQ=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0 h1:3Q/xZUyC1BBkualc9ROb4G8qkH90LXEIICcs5zv1OYY=
go.opentelemetry.io/otel/exporters/otlp/otlptrace v1.28.0/go.mod h1:s75jGIWA9OfCMzF0xr+ZgfrB5FEbbV7UuYo32ahUiFI=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0 h1:R3X6ZXmNPRR8ul6i3WgFURCHzaXjHdm0karRG/+dj3s=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracegrpc v1.28.0/go.mod h1:QWFXnDavXWwMx2EEcZsf3yxgEKAqsxQ+Syjp+seyInw=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0 h1:j9+03ymgYhPKmeXGk5Zu+cIZOlVzd9Zv7QIiyItjFBU=
go.opentelemetry.io/otel/exporters/otlp/otlptrace/otlptracehttp v1.28.0/go.mod h1:Y5+XiUG4Emn1hTfciPzGPJaSI+RpDts6BnCIir0SLqk=
go.opentelemetry.io/otel/log v0.4.0 h1:/vZ+3Utqh18e8TPjuc3ecg284078KWrR8BRz+PQAj3o=
go.opentelemetry.io/otel/log v0.4.0/go.mod h1:DhGnQvky7pHy82MIRV43iXh3FlKN8UUKftn0KbLOq6I=
go.opentelemetry.io/otel/metric v1.28.0 h1:f0HGvSl1KRAU1DLgLGFjrwVyismPlnuU6JD6bOeuA5Q=
go.opentelemetry.io/otel/metric v1.28.0/go.mod h1:Fb1eVBFZmLVTMb6PPohq3TO9IIhUisDsbJoL/+uQW4s=
go.opentelemetry.io/otel/sdk v1.28.0 h1:b9d7hIry8yZsgtbmM0DKyPWMMUMlK9NEKuIG4aBqWyE=
go.opentelemetry.io/otel/sdk v1.28.0/go.mod h1:oYj7ClPUA7Iw3m+r7GeEjz0qckQRJK2B8zjcZEfu7Pg=
go.opentelemetry.io/otel/sdk/log v0.4.0 h1:1mMI22L82zLqf6KtkjrRy5BbagOTWdJsqMY/HSqILAA=
go.opentelemetry.io/otel/sdk/log v0.4.0/go.mod h1:AYJ9FVF0hNOgAVzUG/ybg/QttnXhUePWAupmCqtdESo=
go.opentelemetry.io/otel/trace v1.28.0 h1:GhQ9cUuQGmNDd5BTCP2dAvv75RdMxEfTmYejp+lkx9g=
go.opentelemetry.io/otel/trace v1.28.0/go.mod h1:jPyXzNPg6da9+38HEwElrQiHlVMTnVfM3/yv2OlIHaI=
go.opentelemetry.io/proto/otlp v1.3.1 h1:TrMUixzpM0yuc/znrFTP9MMRh8trP93mkCiDVeXrui0=
go.opentelemetry.io/proto/otlp v1.3.1/go.mod h1:0X1WI4de4ZsLrrJNLAQbFeLCm3T7yBkR0XqQ7niQU+8=
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-20240719175910-8a7402abbf56 h1:2dVuKD2vS7b0QIHQbpyTISPd0LeHDbnYEryqj5Q1ug8=
golang.org/x/exp v0.0.0-20240719175910-8a7402abbf56/go.mod h1:M4RDyNAINzryxdtnbRXRL/OHtkFuWGRjvuhBJpk2IlY=
golang.org/x/net v0.27.0 h1:5K3Njcw06/l2y9vpGCSdcxWOYHOUk3dVNGDXN+FvAys=
golang.org/x/net v0.27.0/go.mod h1:dDi0PyhWNoiUOrAS8uXv/vnScO4wnHQO4mj9fn/RytE=
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.22.0 h1:RI27ohtqKCnwULzJLqkv897zojh5/DwS/ENaMzUOaWI=
golang.org/x/sys v0.22.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
golang.org/x/text v0.16.0 h1:a94ExnEXNtEwYLGJSIUxnWoxoRz/ZcCsV63ROupILh4=
golang.org/x/text v0.16.0/go.mod h1:GhwF1Be+LQoKShO3cGOHzqOgRrGaYc9AvblQOmPVHnI=
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a h1:YIa/rzVqMEokBkPtydCkx1VLmv3An1Uw7w1P1m6EhOY=
google.golang.org/genproto/googleapis/api v0.0.0-20240723171418-e6d459c13d2a/go.mod h1:AHT0dDg3SoMOgZGnZk29b5xTbPHMoEC8qthmBLJCpys=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a h1:hqK4+jJZXCU4pW7jsAdGOVFIfLHQeV7LaizZKnZ84HI=
google.golang.org/genproto/googleapis/rpc v0.0.0-20240723171418-e6d459c13d2a/go.mod h1:Ue6ibwXGpU+dqIcODieyLOcgj7z8+IcskoNIgZxtrFY=
google.golang.org/grpc v1.65.0 h1:bs/cUb4lp1G5iImFFd3u5ixQzweKizoZJAwBNLR42lc=
google.golang.org/grpc v1.65.0/go.mod h1:WgYC2ypjlB0EiQi6wdKixMqukr6lBc0Vo+oOgjrM5ZQ=
google.golang.org/protobuf v1.34.2 h1:6xV6lTsCfpGD21XK49h7MhtcApnLqkfYgPcdHftf6hg=
google.golang.org/protobuf v1.34.2/go.mod h1:qYOHts0dSfpeUzUFpOMr/WGzszTmLH+DiWniOlNbLDw=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/check.v1 v1.0.0-20190902080502-41f04d3bba15/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI=
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
241 changes: 241 additions & 0 deletions dagger/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,241 @@
package main

import (
"context"
"dagger/prometheus-rds-exporter/internal/dagger"
"fmt"
"strings"
"time"
)

const (
RdsExporterBinaryName string = "prometheus-rds-exporter"

githubCliVersion string = "2.53.0"
githubRepository string = "camptocamp/prometheus-rds-exporter"
)

type RdsExporter struct {
// +private
Version string
// +private
Tag string

Source *dagger.GitRef
}

func New(
tag string,
) *RdsExporter {
source := dag.Git("https://github.com/camptocamp/rds_exporter.git").
Tag(tag)

version := strings.TrimPrefix(tag, "v")

rdsExporter := &RdsExporter{
Version: version,
Tag: tag,
Source: source,
}

return rdsExporter
}

func (rdsExporter *RdsExporter) Binary(
ctx context.Context,
// +optional
platform dagger.Platform,
) (*dagger.File, error) {
if platform == "" {
defaultPlatform, err := dag.DefaultPlatform(ctx)

if err != nil {
return nil, fmt.Errorf("failed to get platform: %s", err)
}

platform = defaultPlatform
}

platformElements := strings.Split(string(platform), "/")

os := platformElements[0]
arch := platformElements[1]

commit, err := rdsExporter.Source.Commit(ctx)

if err != nil {
return nil, fmt.Errorf("failed to get commit hash: %s", err)
}

binary := dag.Golang().
RedhatContainer().
WithEnvVariable("GOOS", os).
WithEnvVariable("GOARCH", arch).
WithMountedDirectory(".", rdsExporter.Source.Tree()).
WithExec([]string{
"go", "build", "-o", RdsExporterBinaryName, "-ldflags", "-s -w " +
fmt.Sprintf("-X 'github.com/prometheus/common/version.Version=%s'", rdsExporter.Version) + " " +
fmt.Sprintf("-X 'github.com/prometheus/common/version.Revision=%s'", commit) + " " +
fmt.Sprintf("-X 'github.com/prometheus/common/version.BuildDate=%s'", time.Now().Format("2006-01-02 15:04:05 -07:00")),
}).
File(RdsExporterBinaryName)

return binary, nil
}

func (rdsExporter *RdsExporter) Overlay(
ctx context.Context,
// +optional
platform dagger.Platform,
// +optional
prefix string,
) (*dagger.Directory, error) {
if prefix == "" {
prefix = "/usr/local"
}

binary, err := rdsExporter.Binary(ctx, platform)

if err != nil {
return nil, fmt.Errorf("failed to get binary: %s", err)
}

overlay := dag.Directory().
WithDirectory(prefix, dag.Directory().
WithDirectory("bin", dag.Directory().
WithFile(RdsExporterBinaryName, binary),
),
)

return overlay, nil
}

func (rdsExporter *RdsExporter) Container(
ctx context.Context,
// +optional
platform dagger.Platform,
) (*dagger.Container, error) {
overlay, err := rdsExporter.Overlay(ctx, platform, "")

if err != nil {
return nil, fmt.Errorf("failed to get overlay: %s", err)
}

container := dag.Redhat().Micro().Container(dagger.RedhatMicroContainerOpts{Platform: platform}).
WithDirectory("/", overlay).
WithEntrypoint([]string{RdsExporterBinaryName}).
WithDefaultArgs([]string{"--config.file=/etc/rds_exporter/config.yml"}).
WithExposedPort(9042)

return container, nil
}

func (rdsExporter *RdsExporter) Archive(
ctx context.Context,
// +optional
platform dagger.Platform,
) (*dagger.File, error) {
binary, err := rdsExporter.Binary(ctx, platform)

if err != nil {
return nil, fmt.Errorf("failed to get binary: %s", err)
}

archiveName := RdsExporterBinaryName + ".tar.gz"

archive := dag.Redhat().Container().
WithMountedDirectory(".", rdsExporter.Source.Tree()).
WithMountedFile(RdsExporterBinaryName, binary).
WithExec([]string{"tar", "-czvf", archiveName, RdsExporterBinaryName, "LICENSE", "CHANGELOG.md", "README.md"}).
File(archiveName)

return archive, nil
}

func (rdsExporter *RdsExporter) Release(
ctx context.Context,
githubToken *dagger.Secret,
) error {
oses := []string{
"linux",
"darwin",
}

arches := []string{
"amd64",
"arm64",
}

archives := dag.Directory()

for _, os := range oses {
for _, arch := range arches {
platform := dagger.Platform(os + "/" + arch)

archive, err := rdsExporter.Archive(ctx, platform)

if err != nil {
return fmt.Errorf("failed to get archive for platform %s: %s", platform, err)
}

archives = archives.WithFile(fmt.Sprintf("%s-%s-%s.tar.gz", RdsExporterBinaryName, os, arch), archive)
}
}

containers := make([]*dagger.Container, 0, len(oses)*len(arches))

for _, arch := range arches {
platform := dagger.Platform("linux/" + arch)

container, err := rdsExporter.Container(ctx, platform)

if err != nil {
return fmt.Errorf("failed to get container for platform %s: %s", platform, err)
}

containers = append(containers, container)
}

archiveNames, err := archives.Entries(ctx)

if err != nil {
return fmt.Errorf("failed to get archive names: %s", err)
}

checksums, err := dag.Redhat().Container().
WithMountedDirectory(".", archives).
WithExec(append([]string{"sha256sum"}, archiveNames...)).
Stdout(ctx)

if err != nil {
return fmt.Errorf("failed to compute checksums: %s", err)
}

_, err = dag.Container().
WithRegistryAuth("ghcr.io", "dagger", githubToken).
Publish(ctx, fmt.Sprintf("ghcr.io/%s:%s", githubRepository, rdsExporter.Version), dagger.ContainerPublishOpts{
PlatformVariants: containers,
})

if err != nil {
return fmt.Errorf("failed to publish container image manifest list: %s", err)
}

checksumsName := "checksums.txt"
assetNames := append(archiveNames, checksumsName)

_, err = dag.Github(githubCliVersion).RedhatContainer().
WithEnvVariable("GH_REPO", githubRepository).
WithSecretVariable("GH_TOKEN", githubToken).
WithMountedDirectory(".", archives).
WithNewFile(checksumsName, checksums).
WithExec([]string{"gh", "release", "create", "--title", rdsExporter.Tag, rdsExporter.Tag}).
WithExec(append([]string{"gh", "release", "upload", rdsExporter.Tag}, assetNames...)).
Sync(ctx)

if err != nil {
return fmt.Errorf("failed to create release: %s", err)
}

return nil
}

0 comments on commit f90d9a7

Please sign in to comment.