Skip to content

Commit

Permalink
fix(github): missing or build step present where is should not have b…
Browse files Browse the repository at this point in the history
…een present or missing and avoid nodejs static configuration without any main

Signed-off-by: kilianpaquier <[email protected]>
  • Loading branch information
kilianpaquier committed Nov 20, 2024
1 parent 21a822b commit 6c7c547
Show file tree
Hide file tree
Showing 22 changed files with 207 additions and 175 deletions.
35 changes: 0 additions & 35 deletions examples/golang_github/.github/workflows/golang.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,10 +25,6 @@ on:
- v[0-9]+.x
workflow_call:
inputs:
build:
description: Whether to run build job or not
required: false
type: boolean
version:
description: Build version to use in build metadata
required: true
Expand Down Expand Up @@ -121,37 +117,6 @@ jobs:
path: reports
retention-days: 1

go-build:
name: Go Build
runs-on: ubuntu-latest
if: ${{ inputs.build }}
needs:
- run-workflow
- go-test
steps:
- uses: actions/checkout@v4
- uses: actions/setup-go@v5
with:
check-latest: true
go-version-file: go.mod
token: ${{ secrets.GITHUB_TOKEN }}
# https://github.com/marketplace/actions/goreleaser-action
- uses: goreleaser/goreleaser-action@v6
with:
args: release --clean --config .goreleaser.yml --skip=validate --skip=announce --skip=publish --snapshot
env:
VERSION: ${{ inputs.version }}
- uses: actions/upload-artifact@v4
with:
name: build
# order is important to filter unwanted globs after the filter or desired globs
path: |
dist/*
!dist/*.json
!dist/*.yaml
!dist/*/
retention-days: 1

sonar-analysis:
name: Sonar Analysis
runs-on: ubuntu-latest
Expand Down
11 changes: 11 additions & 0 deletions examples/golang_github/.github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,16 @@ jobs:
env:
INPUT_MODE: ${{ inputs.mode }}

golang:
name: Golang
needs: version
uses: ./.github/workflows/golang.yml
permissions:
checks: write
with:
version: ${{ needs.version.outputs.version }}
secrets: inherit

docker:
name: Docker
needs: version
Expand All @@ -70,6 +80,7 @@ jobs:
pull-requests: write
needs:
- version
- golang
- docker
steps:
- uses: actions/checkout@v4
Expand Down
4 changes: 2 additions & 2 deletions pkg/generate/fsys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ func TestExec_Golang(t *testing.T) {
CI: &craft.CI{Name: tc},
Docker: &craft.Docker{Port: helpers.ToPtr(uint16(5000)), Registry: helpers.ToPtr("example.com")},
License: helpers.ToPtr("mit"),
NoGoreleaser: true,
NoGoreleaser: false,
Platform: tc,
},
Crons: map[string]struct{}{"cron-name": {}},
Expand Down Expand Up @@ -695,7 +695,7 @@ func TestExec_Nodejs(t *testing.T) {
Platform: tc.CI,
},
Languages: map[string]any{
"nodejs": generate.PackageJSON{PackageManager: "[email protected]"},
"nodejs": generate.PackageJSON{Main: helpers.ToPtr("index.js"), PackageManager: "[email protected]"},
},
})
destdir := t.TempDir()
Expand Down
9 changes: 7 additions & 2 deletions pkg/generate/nodejs.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@ import (
"github.com/kilianpaquier/craft/pkg/craft"
)

var errMissingPackageManager = errors.New("package.json packageManager isn't valid")
var (
ErrInvalidStaticDeployment = errors.New("package.json 'main' isn't provided but a static deployment is configured")

Check failure on line 19 in pkg/generate/nodejs.go

View workflow job for this annotation

GitHub Actions / Go Lint

exported: exported var ErrInvalidStaticDeployment should have comment or be unexported (revive)
ErrMissingPackageManager = errors.New("package.json 'packageManager' isn't valid")
)

var packageManagerRegexp = regexp.MustCompile(`^(npm|pnpm|yarn|bun)@\d+\.\d+\.\d+(-.+)?$`)

Expand Down Expand Up @@ -50,7 +53,7 @@ func (p *PackageJSON) Validate() error {

if p.PackageManager != "" && !packageManagerRegexp.MatchString(p.PackageManager) {
// json schema takes care of saying which regexp must be validated
errs = append(errs, errMissingPackageManager)
errs = append(errs, ErrMissingPackageManager)
}

if err := validator.New().Struct(p); err != nil {
Expand All @@ -77,6 +80,8 @@ func DetectNodejs(_ context.Context, destdir string, metadata *Metadata) ([]Exec
metadata.ProjectName = pkg.Name
if pkg.Main != nil {
metadata.Binaries++
} else if metadata.CI != nil && metadata.CI.Static != nil {
return nil, ErrInvalidStaticDeployment
}

// deactivate makefile because commands are facilitated by package.json scripts
Expand Down
27 changes: 25 additions & 2 deletions pkg/generate/nodejs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func TestDetectNodejs(t *testing.T) {
assert.Empty(t, exec)
})

t.Run("error_validation_packagejson", func(t *testing.T) {
t.Run("error_validation_packageManager", func(t *testing.T) {
// Arrange
destdir := t.TempDir()

Expand All @@ -56,7 +56,30 @@ func TestDetectNodejs(t *testing.T) {
exec, err := generate.DetectNodejs(ctx, destdir, &generate.Metadata{})

// Assert
assert.ErrorContains(t, err, "read package.json")
assert.ErrorIs(t, err, generate.ErrMissingPackageManager)
assert.Empty(t, exec)
})

t.Run("error_invalid_static_option", func(t *testing.T) {
// Arrange
destdir := t.TempDir()

packagejson := filepath.Join(destdir, craft.PackageJSON)
err := os.WriteFile(packagejson, []byte(`{ "name": "craft", "packageManager": "[email protected]", "private": true }`), cfs.RwRR)
require.NoError(t, err)

config := generate.Metadata{
Configuration: craft.Configuration{
CI: &craft.CI{Static: &craft.Static{}},
},
Languages: map[string]any{},
}

// Act
exec, err := generate.DetectNodejs(ctx, destdir, &config)

// Assert
assert.ErrorIs(t, err, generate.ErrInvalidStaticDeployment)
assert.Empty(t, exec)
})

Expand Down
2 changes: 2 additions & 0 deletions pkg/generate/templates/.github/workflows/netlify.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ jobs:
node:
name: Node
uses: ./.github/workflows/nodejs.yml
with:
build: true
<<- end >>

netlify:
Expand Down
2 changes: 2 additions & 0 deletions pkg/generate/templates/.github/workflows/pages.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ jobs:
node:
name: Node
uses: ./.github/workflows/nodejs.yml
with:
build: true
<<- end >>

pages:
Expand Down
13 changes: 9 additions & 4 deletions pkg/generate/templates/.github/workflows/release.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ run-name: Release
<<- $golang := hasKey .Languages "golang" >>

<<- $gocli := and $golang (gt (len .Clis) 0) (not .NoGoreleaser) >>
<<- $nodebuild := and $nodejs ((get .Languages "nodejs").Main) >>
<<- $nodepublish := and $nodejs (not (get .Languages "nodejs").Private) >>
<<- $docker := and .Docker (gt .Binaries 0) >>

Expand Down Expand Up @@ -102,7 +103,7 @@ jobs:
env:
INPUT_MODE: ${{ inputs.mode }}

<<- if $gocli >>
<<- if $golang >>

golang:
name: Golang
Expand All @@ -111,18 +112,22 @@ jobs:
permissions:
checks: write
with:
<<- if $gocli >>
build: true
<<- end >>
version: ${{ needs.version.outputs.version }}
secrets: inherit
<<- end >>

<<- if $nodepublish >>
<<- if $nodejs >>

node:
name: Node
uses: ./.github/workflows/nodejs.yml
<<- if $nodebuild >>
with:
build: true
<<- end >>
secrets: inherit
<<- end >>

Expand Down Expand Up @@ -184,10 +189,10 @@ jobs:
<<- end >>
needs:
- version
<<- if $gocli >>
<<- if $golang >>
- golang
<<- end >>
<<- if $nodepublish >>
<<- if $nodejs >>
- node
<<- end >>
<<- if $docker >>
Expand Down
7 changes: 5 additions & 2 deletions pkg/generate/templates/.gitlab/workflows/.gitlab-ci.yml.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -184,8 +184,11 @@ go-build:
artifacts:
name: "$CI_JOB_NAME artifacts from $CI_PROJECT_NAME on $CI_COMMIT_REF_SLUG"
paths:
- dist
- checksums.txt
- dist/
exclude:
- dist/*.json
- dist/*.yaml
- dist/*/
expire_in: 1 day
<<- else if $golang >>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
name: Golang
run-name: Golang

<<- $gocli := and (gt (len .Clis) 0) (not .NoGoreleaser) >>
<<- $docker := and .Docker (gt .Binaries 0) >>

on:
Expand All @@ -29,10 +30,12 @@ on:
<<- end >>
workflow_call:
inputs:
<<- if $gocli >>
build:
description: Whether to run build job or not
required: false
type: boolean
<<- end >>
version:
description: Build version to use in build metadata
required: true
Expand Down Expand Up @@ -131,6 +134,8 @@ jobs:
retention-days: 1
<<- end >>

<<- if $gocli >>

go-build:
name: Go Build
runs-on: ubuntu-latest
Expand Down Expand Up @@ -161,6 +166,7 @@ jobs:
!dist/*.yaml
!dist/*/
retention-days: 1
<<- end >>

<<- if has "sonar" .CI.Options >>

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ name: Node
run-name: Node

<<- $specifics := get .Languages "nodejs" >>
<<- $nodebuild := $specifics.Main >>
<<- $docker := and .Docker (gt .Binaries 0) >>
<<- $manager := cutAfter $specifics.PackageManager "@" >>

Expand All @@ -23,18 +24,20 @@ on:
- development
- next
- staging
<<- if not .IsAutoRelease >>
<<- if and (not .IsAutoRelease) (or (not .CI.Static) (not .CI.Static.Auto)) >>
- main
- master
- v[0-9]+.[0-9]+.x
- v[0-9]+.x
<<- end >>
workflow_call:
<<- if $nodebuild >>
inputs:
build:
description: Whether to run build job or not
required: false
type: boolean
<<- end >>
workflow_dispatch:

jobs:
Expand Down Expand Up @@ -163,6 +166,8 @@ jobs:
retention-days: 1
<<- end >>

<<- if $nodebuild >>

node-build:
name: Node Build
runs-on: ubuntu-latest
Expand Down Expand Up @@ -203,6 +208,7 @@ jobs:
name: build
path: dist
retention-days: 1
<<- end >>

<<- if has "sonar" .CI.Options >>

Expand Down
51 changes: 51 additions & 0 deletions testdata/golang/success_binary_all_github/.goreleaser.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
# Code generated by craft; DO NOT EDIT.

version: 2

builds:
- main: cmd/cli-name/main.go
env:
- CGO_ENABLED=0
ldflags:
- -X example.com/kilianpaquier/craft/internal/cobra.version={{ .Env.VERSION }}
goos:
- linux
- windows
- darwin
goarch:
- amd64
- arm64

announce:
skip: true

changelog:
disable: true

archives:
- format: tar.gz
wrap_in_directory: false
name_template: >-
{{- .ProjectName }}_
{{- .Os }}_
{{- .Arch }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
format_overrides:
- goos: windows
format: zip

checksum:
name_template: checksums.txt

nfpms:
- maintainer: maintainer name
license: MIT
file_name_template: >-
{{- .ProjectName }}_
{{- .Os }}_
{{- .Arch }}
{{- if .Arm }}v{{ .Arm }}{{ end }}
formats:
- apk
- deb
- rpm
Loading

0 comments on commit 6c7c547

Please sign in to comment.