Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
76 changes: 76 additions & 0 deletions .github/workflows/js-app.yaml
Comment thread
sudazzle marked this conversation as resolved.
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
name: js-app

on:
workflow_call:
inputs:
build-command:
description: 'NPM project build command. Defaults to build'
type: string
default: 'build'
required: false
skip-unit-tests:
description: 'Set to true if project needs to skip running unit tests'
type: boolean
default: false
required: false
skip-e2e-tests:
description: 'Set to true if project needs to skip running browser tests'
type: boolean
default: false
required: false
app-deploy-env:
type: string
description: 'Use "production", "dev", "test", "staging", etc'
required: false
default: "production"

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read
packages: read

jobs:
js-app-build:
name: Build nextjs app
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v5

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '>=1.24.0'
cache-dependency-path: "**/go.sum"

- name: Install go Tools
run: go install tool

- name: Setup Node.js (for the JS project)
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Build app and Push to docker repository
run: "go tool mage -v buildAndPublish"
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
BUILD_COMMAND: ${{ inputs.build-command }}
DEPLOY_ENV: ${{ inputs.app-deploy-env }}
SKIP_UNIT_TEST: ${{ github.skip-unit-tests }}
SKIP_E2E_TEST: ${{ github.skip-e2e-tests }}

- id: oci-images
name: Output OCI images references
run: |
if [ -f ./var/oci-images.json ]; then
echo "images=$(cat ./var/oci-images.json)" >> $GITHUB_OUTPUT
else
echo "images={}" >> $GITHUB_OUTPUT
fi
- name: Show output
run: tree var/

67 changes: 67 additions & 0 deletions .github/workflows/js-lib.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
name: mage

on:
workflow_call:
inputs:
skip-build:
description: 'Set to true if project do not need to be build'
type: boolean
default: false
required: false
build-command:
description: 'NPM project build command. Defaults to build:library'
type: string
default: 'build:library'
required: false
skip-unit-tests:
description: 'Set to true if want to skip unit tests'
type: boolean
default: false
required: false
private:
description: 'Set to true if npm package to be publish is a private'
type: boolean
required: false
default: false

concurrency:
group: ${{ github.workflow }}-${{ github.ref }}
cancel-in-progress: true

permissions:
contents: read
packages: read

jobs:
publish-js-lib:
name: Publish to github npm repository
runs-on: ubuntu-latest
steps:
- name: Checkout code
uses: actions/checkout@v5

- name: Set up Go
uses: actions/setup-go@v5
with:
go-version: '>=1.24.0'
cache-dependency-path: "**/go.sum"

- name: Install go Tools
run: go install tool

- name: Setup Node.js (for the JS project)
uses: actions/setup-node@v4
with:
node-version: '20'

- name: Build and publish
run: go tool mage -v buildAndPublish
env:
BUILD_COMMAND: ${{ github.build-command }}
SKIP_BUILD: ${{ github.skip-build }}
SKIP_UNIT_TEST: ${{ github.skip-unit-tests }}
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
PRIVATE: ${{ inputs.is-npm-package-private }}
GITHUB_TAGNAME: ${{ github.event.release.tag_name }}


44 changes: 44 additions & 0 deletions internal/core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,9 @@ package core

import (
"fmt"
"io"
"io/fs"
"log"
"os"
"path"
"path/filepath"
Expand Down Expand Up @@ -120,3 +122,45 @@ func CompareChangesToPaths(changes []string, paths []string, additionalGlobs []s
}
return false, nil
}

// FileExists checks if the file in given path exists or not.
func FileExists(path string) bool {
_, err := os.Stat(path)

return err == nil
}

// CreateOrAppendFile creates a file with a given content or appends content to existing file
// at the specified path
func CreateOrAppendFile(path string, content string) error {
file, err := os.OpenFile(path, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)

defer func() {
if err := file.Close(); err != nil {
log.Printf("Failed to close file: %v", err)
}
}()

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

if _, err := io.WriteString(file, content); err != nil {
return fmt.Errorf("failed to write to file: %w", err)
}

return nil
}

// IsDirectoryEmpty checks if the specified directory is empty
// and returns true if it contains no files or subdirectories.
// Also return error if there is any while reading the directory
func IsDirectoryEmpty(dirPath string) (bool, error) {
entries, err := os.ReadDir(dirPath)

if err != nil {
return true, err
}

return len(entries) == 0, nil
}
42 changes: 42 additions & 0 deletions internal/docker/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"os"
"path"
"path/filepath"
"strconv"
"strings"
"time"

Expand All @@ -21,6 +22,12 @@ const (
imageNameBaseFallback = "ocreg.invalid/coopnorge"
)

const (
// PushEnv is the name of the environmental variable used to trigger
// pushing of OCI images. Set PUSH_IMAGE to true to push images.
PushEnv = "PUSH_IMAGE"
)

// Validate the content of a Dockerfile
func Validate(dockerfileContent string) error {
dockerfilePath, cleanup, err := core.WriteTempFile("./var", "Dockerfile", dockerfileContent)
Expand Down Expand Up @@ -90,6 +97,24 @@ func BuildAndPush(dockerfileContent, platforms, image, dockerContext, imagePath,
)
}

githubToken := os.Getenv("GITHUB_TOKEN")

if githubToken != "" {
filename, cleanup, err := core.WriteTempFile(".", "github_token", githubToken)

if err != nil {
return err
}

args = append(
args,
"--secret",
fmt.Sprintf("id=github_token,src=%s", filename),
Comment thread
AtzeDeVries marked this conversation as resolved.
)

defer cleanup()
}

args = append(args,
"-f", dockerfilePath,
dockerContext,
Expand Down Expand Up @@ -192,14 +217,31 @@ func Images(imageDir string) (AppImages, error) {
result[metadata.App][metadata.Binary]["tag"] = metadata.Tag
result[metadata.App][metadata.Binary]["image"] = metadata.ImageName
}

return result, nil
}

// FullyQualifiedlImageName ...
func FullyQualifiedlImageName(app, binary string) string {
if binary == "" {
return fmt.Sprintf("%s/%s", imageBase(), app)
}
return fmt.Sprintf("%s/%s/%s", imageBase(), app, binary)
}

// ShouldPush checks if docker image should be pushed to the repository or not
func ShouldPush() (bool, error) {
val, ok := os.LookupEnv(PushEnv)
if !ok || val == "" {
return false, nil
}
boolValue, err := strconv.ParseBool(val)
if err != nil {
return false, err
}
return boolValue, nil
}

func imageBase() string {
imageBase, ok := os.LookupEnv(imageBaseEnv)
if !ok || imageBase == "" {
Expand Down
18 changes: 18 additions & 0 deletions internal/git/git.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,29 @@ package git
import (
"fmt"
"os"
"path"
"strings"

"github.com/magefile/mage/sh"
)

// RepoNameFromURL returns the repository name
func RepoNameFromURL() (string, error) {
url, err := RepoURL()

if err != nil {
return "", err
}
Comment thread
sudazzle marked this conversation as resolved.

// 1. Get the last element of the URL path
base := path.Base(url)

// 2. Remove the ".git" suffix if it exists
projectName := strings.TrimSuffix(base, ".git")

return projectName, nil
}

// RepoURL returns the remote URL to the git repository
func RepoURL() (string, error) {
remote, err := sh.Output("git", "remote", "get-url", "origin")
Expand Down
Loading
Loading