From e237d57c7cf87ab084aa8e03df54c48faf7415c2 Mon Sep 17 00:00:00 2001 From: Ben Hale Date: Sat, 31 Oct 2020 06:22:49 -0700 Subject: [PATCH] Buildpack Info Action This change adds a buildpack-info action that parses a buildpack-info action and exposes the contents of the [buildpack] block as output parameters. Signed-off-by: Ben Hale --- .../create-action-buildpack-info.yml | 73 +++++++++++++++++++ README.md | 24 +++++- go.mod | 1 + go.sum | 17 +++-- info/buildpack_info.go | 50 +++++++++++++ info/buildpack_info_test.go | 55 ++++++++++++++ info/cmd/main.go | 38 ++++++++++ info/testdata/buildpack.toml | 19 +++++ 8 files changed, 270 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/create-action-buildpack-info.yml create mode 100644 info/buildpack_info.go create mode 100644 info/buildpack_info_test.go create mode 100644 info/cmd/main.go create mode 100644 info/testdata/buildpack.toml diff --git a/.github/workflows/create-action-buildpack-info.yml b/.github/workflows/create-action-buildpack-info.yml new file mode 100644 index 0000000..d873ba1 --- /dev/null +++ b/.github/workflows/create-action-buildpack-info.yml @@ -0,0 +1,73 @@ +name: Create Action buildpack-info +"on": + pull_request: + paths: + - info/** + push: + branches: + - main + paths: + - info/** + release: + types: + - published +jobs: + create-action: + name: Create Action + runs-on: + - ubuntu-latest + steps: + - if: ${{ github.event_name != 'pull_request' || ! github.event.pull_request.head.repo.fork }} + name: Docker login ghcr.io + uses: docker/login-action@v1 + with: + password: ${{ secrets.IMPLEMENTATION_GHCR_TOKEN }} + registry: ghcr.io + username: ${{ secrets.IMPLEMENTATION_GHCR_USERNAME }} + - uses: actions/checkout@v2 + - id: version + name: Compute Version + run: | + #!/usr/bin/env bash + + set -euo pipefail + + if [[ ${GITHUB_REF} =~ refs/tags/v([0-9]+\.[0-9]+\.[0-9]+) ]]; then + VERSION=${BASH_REMATCH[1]} + elif [[ ${GITHUB_REF} =~ refs/heads/(.+) ]]; then + VERSION=${BASH_REMATCH[1]} + else + VERSION=$(git rev-parse --short HEAD) + fi + + echo "::set-output name=version::${VERSION}" + echo "Selected ${VERSION} from + * ref: ${GITHUB_REF} + * sha: ${GITHUB_SHA} + " + - name: Create Action + run: | + #!/usr/bin/env bash + + set -euo pipefail + + echo "::group::Building ${TARGET}:${VERSION}" + docker build \ + --file Dockerfile \ + --build-arg "SOURCE=${SOURCE}" \ + --tag "${TARGET}:${VERSION}" \ + . + echo "::endgroup::" + + if [[ "${PUSH}" == "true" ]]; then + echo "::group::Pushing ${TARGET}:${VERSION}" + docker push "${TARGET}:${VERSION}" + echo "::endgroup::" + else + echo "Skipping push" + fi + env: + PUSH: ${{ github.event_name != 'pull_request' }} + SOURCE: info/cmd + TARGET: ghcr.io/buildpacks/actions/buildpack-info + VERSION: ${{ steps.version.outputs.version }} diff --git a/README.md b/README.md index 8bed864..2f541b5 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,27 @@ [gha]: https://docs.github.com/en/free-pro-team@latest/actions -## Registry Action +## Buildpack Info Action +The buildpack-info action parses a `buildpack.toml` and exposes the contents of the `[buildpack]` block as output parameters. + +```yaml +uses: docker://ghcr.io/buildpacks/actions/buildpack-info +``` + +#### Inputs +| Parameter | Description +| :-------- | :---------- +| `path` | Optional path to `buildpack.toml`. Defaults to `/buildpack.toml` + +#### Outputs +| Parameter | Description +| :-------- | :---------- +| `id` | The contents of `buildpack.id` +| `name` | The contents of `buildpack.name` +| `version` | The contents of `buildpack.version` +| `homepage` | The contents of `buildpack.homepage` + +## Registry Action The registry action adds and yanks buildpack releases in the [Buildpack Registry Index][bri]. [bri]: https://github.com/buildpacks/registry-index @@ -18,6 +38,7 @@ with: address: index.docker.io/buildpacksio/test-buildpack@${{ steps.deploy.outputs.digest }} ``` +#### Inputs | Parameter | Description | :-------- | :---------- | `token` | A GitHub token with `public_repo` scope to open an issue against [`buildpacks/registry-index`][bri]. @@ -35,6 +56,7 @@ with: yank: true ``` +#### Inputs | Parameter | Description | :-------- | :---------- | `token` | A GitHub token with `public_repo` scope to open an issue against [`buildpacks/registry-index`][bri]. diff --git a/go.mod b/go.mod index 4de595b..a49d04b 100644 --- a/go.mod +++ b/go.mod @@ -3,6 +3,7 @@ module github.com/buildpacks/github-actions go 1.15 require ( + github.com/buildpacks/libcnb v1.18.0 github.com/google/go-github/v32 v32.1.0 github.com/onsi/gomega v1.10.3 github.com/pelletier/go-toml v1.8.1 diff --git a/go.sum b/go.sum index e9ce8f9..78e7858 100644 --- a/go.sum +++ b/go.sum @@ -31,10 +31,11 @@ cloud.google.com/go/storage v1.6.0/go.mod h1:N7U0C8pVQ/+NIKOBQyamJIeKQKkZ+mxpohl cloud.google.com/go/storage v1.8.0/go.mod h1:Wv1Oy7z6Yz3DshWRJFhqM/UCfaWIRTdp0RXyy7KQOVs= cloud.google.com/go/storage v1.10.0/go.mod h1:FLPqc6j+Ki4BU591ie1oL6qBQGu2Bl/tZ9ullr3+Kg0= dmitri.shuralyov.com/gpu/mtl v0.0.0-20190408044501-666a987793e9/go.mod h1:H6x//7gZCb22OMCxBHrMx7a5I7Hp++hsVxbQ4BYO7hU= +github.com/BurntSushi/toml v0.3.1 h1:WXkYYl6Yr3qBf1K79EBnL4mak0OimBfB0XUf9Vl28OQ= github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= github.com/BurntSushi/xgb v0.0.0-20160522181843-27f122750802/go.mod h1:IVnqGOEym/WlBOVXweHU+Q+/VP0lqqI8lqeDx9IjBqo= -github.com/aws/aws-sdk-go v1.35.17 h1:zhahppAMdPvJ9GP302SMOPW5SNoAbnjdOyaTmxA9WJU= -github.com/aws/aws-sdk-go v1.35.17/go.mod h1:tlPOdRjfxPBpNIwqDj61rmsnA85v9jc0Ps9+muhnW+k= +github.com/buildpacks/libcnb v1.18.0 h1:Q7I+HjQ1Cq02/AqhTOrzecuNESip9xE2axckxnymYLQ= +github.com/buildpacks/libcnb v1.18.0/go.mod h1:yzAQd//jyUXVx6Z/F0cKk/hrl49QZq1OE/hP5+/ZPuQ= github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= github.com/chzyer/logex v1.1.10/go.mod h1:+Ywpsq7O8HXn0nuIou7OrIPyXbp3wmkHB+jjWRnGsAI= github.com/chzyer/readline v0.0.0-20180603132655-2972be24d48e/go.mod h1:nSuG5e5PlCu98SY8svDHJxuZscDgtXS6KTTbou5AhLI= @@ -108,8 +109,6 @@ github.com/hashicorp/golang-lru v0.5.0/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ github.com/hashicorp/golang-lru v0.5.1/go.mod h1:/m3WP610KZHVQ1SGc6re/UDhFvYD7pJ4Ao+sR/qLZy8= github.com/hpcloud/tail v1.0.0/go.mod h1:ab1qPbhIpdTxEkNHXyeSf5vhxWSCs/tWer42PpOxQnU= github.com/ianlancetaylor/demangle v0.0.0-20181102032728-5e5cf60278f6/go.mod h1:aSSvb/t6k1mPoxDqO4vJh6VOCGPwU4O0C2/Eqndh1Sc= -github.com/jmespath/go-jmespath v0.4.0/go.mod h1:T8mJZnbsbmF+m6zOOFylbeCJqk5+pHWvzYPziyZiYoo= -github.com/jmespath/go-jmespath/internal/testify v1.5.1/go.mod h1:L3OGu8Wl2/fWfCI6z80xFu9LTZmf1ZRjMHUOPmWr69U= github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU= github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk= github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck= @@ -124,18 +123,22 @@ github.com/onsi/ginkgo v1.6.0/go.mod h1:lLunBs/Ym6LB5Z9jYTR76FiuTmxDTDusOGeTQH+W github.com/onsi/ginkgo v1.12.1 h1:mFwc4LvZ0xpSvDZ3E+k8Yte0hLOMxXUlP+yXtJqkYfQ= github.com/onsi/ginkgo v1.12.1/go.mod h1:zj2OWP4+oCPe1qIXoGWkgMRwljMUYCdkwsT2108oapk= github.com/onsi/gomega v1.7.1/go.mod h1:XdKZgCCFLUoM/7CFJVPcG8C1xQ1AJ0vpAezJrB7JYyY= +github.com/onsi/gomega v1.10.2/go.mod h1:iN09h71vgCQne3DLsj+A5owkum+a2tYe+TOCB1ybHNo= github.com/onsi/gomega v1.10.3 h1:gph6h/qe9GSUw1NhH1gp+qb+h8rXD8Cy60Z32Qw3ELA= github.com/onsi/gomega v1.10.3/go.mod h1:V9xEwhxec5O8UDM77eCW8vLymOMltsqPVYWrpDsH8xc= github.com/pelletier/go-toml v1.8.1 h1:1Nf83orprkJyknT6h7zbuEGUEjcyVlCxSUGTENmNCRM= github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrapLU/GW4pbc= -github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +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/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= github.com/rogpeppe/go-internal v1.3.0/go.mod h1:M8bDsm7K2OlrFYOpmOWEs/qY81heoFRclV5y23lUDJ4= github.com/sclevine/spec v1.4.0 h1:z/Q9idDcay5m5irkZ28M7PtQM4aOISzOpj4bUPkDee8= github.com/sclevine/spec v1.4.0/go.mod h1:LvpgJaFyvQzRvc1kaDs0bulYwzC70PbiYjC4QnFHkOM= +github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4= 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.6.1 h1:hDPOHmpOpP40lSULcqw7IrRb/u7w6RpDC9399XyoNd0= +github.com/stretchr/testify v1.6.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg= github.com/yuin/goldmark v1.1.25/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= github.com/yuin/goldmark v1.1.32/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74= @@ -203,6 +206,7 @@ golang.org/x/net v0.0.0-20200324143707-d3edc9973b7e/go.mod h1:qpuaurCH72eLCgpAm/ golang.org/x/net v0.0.0-20200501053045-e0ff5e5a1de5/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200506145744-7e3656a0809f/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200513185701-a91f0712d120/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= +golang.org/x/net v0.0.0-20200520004742-59133d7f0dd7/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200520182314-0ba52f642ac2/go.mod h1:qpuaurCH72eLCgpAm/N6yyVIVM9cpaDIP3A8BGJEC5A= golang.org/x/net v0.0.0-20200625001655-4c5254603344/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= golang.org/x/net v0.0.0-20200707034311-ab3426394381/go.mod h1:/O7V0waA8r7cgGh81Ro3o1hOxt32SMVPicZroKQ2sZA= @@ -393,9 +397,10 @@ gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7 h1:uRGJdciOHaEIrze2W8Q3AKkep gopkg.in/tomb.v1 v1.0.0-20141024135613-dd632973f1e7/go.mod h1:dt/ZhP58zS4L8KSrWDmTeBkI65Dw0HsyUHuEVlX15mw= gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.2.4/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= -gopkg.in/yaml.v2 v2.2.8/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= gopkg.in/yaml.v2 v2.3.0 h1:clyUAQHOM3G0M3f5vQj7LuJrETvjVot3Z5el9nffUtU= gopkg.in/yaml.v2 v2.3.0/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c h1:dUUwHk2QECo/6vqA44rthZ8ie2QXMNeKRTHCNY2nXvo= +gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM= honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190106161140-3f1c8253044a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= honnef.co/go/tools v0.0.0-20190418001031-e561f6794a2a/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/info/buildpack_info.go b/info/buildpack_info.go new file mode 100644 index 0000000..99a9f04 --- /dev/null +++ b/info/buildpack_info.go @@ -0,0 +1,50 @@ +/* + * Copyright 2018-2020 the original author or 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 + * + * https://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. + */ + +package info + +import ( + "fmt" + "io" + "io/ioutil" + + "github.com/buildpacks/libcnb" + "github.com/pelletier/go-toml" +) + +type BuildpackInfo struct { + Path string + Writer io.Writer +} + +func (b BuildpackInfo) Inform() error { + c, err := ioutil.ReadFile(b.Path) + if err != nil { + return fmt.Errorf("unable to read %s\n%w", b.Path, err) + } + + var bp libcnb.Buildpack + if err := toml.Unmarshal(c, &bp); err != nil { + return fmt.Errorf("unable to unmarhal %s\n%w", b.Path, err) + } + + _, _ = fmt.Fprintf(b.Writer, "::set-output name=id::%s\n", bp.Info.ID) + _, _ = fmt.Fprintf(b.Writer, "::set-output name=name::%s\n", bp.Info.Name) + _, _ = fmt.Fprintf(b.Writer, "::set-output name=version::%s\n", bp.Info.Version) + _, _ = fmt.Fprintf(b.Writer, "::set-output name=homepage::%s\n", bp.Info.Homepage) + + return nil +} diff --git a/info/buildpack_info_test.go b/info/buildpack_info_test.go new file mode 100644 index 0000000..9b35d92 --- /dev/null +++ b/info/buildpack_info_test.go @@ -0,0 +1,55 @@ +/* + * Copyright 2020 the original author or 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 + * + * https://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. + */ + +package info_test + +import ( + "bytes" + "path/filepath" + "testing" + + . "github.com/onsi/gomega" + "github.com/sclevine/spec" + "github.com/sclevine/spec/report" + + "github.com/buildpacks/github-actions/info" +) + +func TestBuildpackInfo(t *testing.T) { + spec.Run(t, "buildpack-info", func(t *testing.T, when spec.G, it spec.S) { + var ( + Expect = NewWithT(t).Expect + + b = &bytes.Buffer{} + + i = info.BuildpackInfo{ + Path: filepath.Join("testdata", "buildpack.toml"), + Writer: b, + } + ) + + it("informs", func() { + Expect(i.Inform()).To(Succeed()) + + Expect(b.String()).To(Equal(`::set-output name=id::test-id +::set-output name=name::test-name +::set-output name=version::test-version +::set-output name=homepage::test-homepage +`, + )) + }) + }, spec.Report(report.Terminal{})) +} diff --git a/info/cmd/main.go b/info/cmd/main.go new file mode 100644 index 0000000..29de097 --- /dev/null +++ b/info/cmd/main.go @@ -0,0 +1,38 @@ +/* + * Copyright 2018-2020 the original author or 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 + * + * https://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. + */ + +package main + +import ( + "os" + + "github.com/buildpacks/github-actions/info" +) + +func main() { + i := info.BuildpackInfo{ + Path: "buildpack.toml", + Writer: os.Stdout, + } + + if s, ok := os.LookupEnv("INPUT_PATH"); ok { + i.Path = s + } + + if err := i.Inform(); err != nil { + panic(err) + } +} diff --git a/info/testdata/buildpack.toml b/info/testdata/buildpack.toml new file mode 100644 index 0000000..5219058 --- /dev/null +++ b/info/testdata/buildpack.toml @@ -0,0 +1,19 @@ +# Copyright 2018-2020 the original author or 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 +# +# https://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. + +[buildpack] +id = "test-id" +name = "test-name" +version = "test-version" +homepage = "test-homepage"