Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Build and commit binaries in CI #1

Merged
merged 7 commits into from
Dec 10, 2023
Merged
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
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ jobs:

- name: Build Binary
run: |
task build
task build-all

- uses: stefanzweifel/git-auto-commit-action@v5
if: github.event_name == 'pull_request'
Expand Down
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
bin
# Need bin/
# bin
41 changes: 33 additions & 8 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,67 +4,92 @@ vars:
GOLANG_CI_LINT_VERSION: v1.55.2
GO_VERSION:
sh: sed -En 's/^go (.*)$/\1/p' go.mod
GO_MODULE_CACHE_VOLUME: go-module-cache
GO_MOD_CACHE_VOLUME: go-module-cache
GIT_REPO_NAME:
sh: basename $(git rev-parse --show-toplevel)

tasks:
create-go-module-cache-volume:
create-go-mod-cache-volume:
internal: true
silent: true
desc: Create the module cache volume
cmds:
- |
docker volume create {{ .GO_MODULE_CACHE_VOLUME }} > /dev/null 2>&1
docker volume create {{ .GO_MOD_CACHE_VOLUME }} > /dev/null 2>&1

_go-docker: &go-docker
internal: true
deps:
- task: create-go-module-cache-volume
- task: create-go-mod-cache-volume
cmds:
- |
docker run \
--rm \
-e GOOS={{ .GOOS }} \
-e GOARCH={{ .GOARCH }} \
-v $(pwd):/app \
-v {{ .GO_MODULE_CACHE_VOLUME }}:/go/pkg/mod \
-v {{ .GO_MOD_CACHE_VOLUME }}:/go/pkg/mod \
-w /app \
golang:{{ .GO_VERSION }} \
{{ .COMMAND }}

build:
<<: *go-docker
internal: false
desc: Build the application
env:
GOOS: "{{OS}}"
GOARCH: "{{ARCH}}"
vars:
COMMAND: go build -o bin/{{ .GIT_REPO_NAME }}-{{ .GOOS }}-{{ .GOARCH }} *.go {{ .CLI_ARGS }}

build-all:
desc: Build the application for all platforms
vars:
COMMAND: go build -o bin/{{ .GIT_REPO_NAME }} *.go
ALL_OS: linux darwin windows
ALL_ARCH: amd64 arm64
cmds:
- |
for os in {{ .ALL_OS }}; do
for arch in {{ .ALL_ARCH }}; do
echo "Building binary for $os/$arch..."
GOOS=$os GOARCH=$arch task build &
done
done
wait

run:
<<: *go-docker
internal: false
desc: Run the application
vars:
COMMAND: go run *.go

format:
<<: *go-docker
internal: false
desc: Format the application
vars:
COMMAND: go fmt ./...

check-format:
<<: *go-docker
internal: false
desc: Format the application
vars:
COMMAND: bash -c 'if [ "$(gofmt -s -l . | wc -l)" -gt 0 ]; then exit 1; fi'

lint:
desc: Lint the application
deps:
- task: create-go-module-cache-volume
- task: create-go-mod-cache-volume
cmds:
- |
docker run \
--rm \
-v $(pwd):/app \
-w /app \
-v {{ .GO_MODULE_CACHE_VOLUME }}:/go/pkg/mod \
-v {{ .GO_MOD_CACHE_VOLUME }}:/go/pkg/mod \
golangci/golangci-lint:{{ .GOLANG_CI_LINT_VERSION }} \
golangci-lint run -v

Expand Down
6 changes: 6 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
name: gha-docker-image-exists
description: Check if a docker image exists

runs:
using: node16
main: shim.js
Binary file added bin/gha-docker-image-exists-darwin-amd64
Binary file not shown.
Binary file added bin/gha-docker-image-exists-darwin-arm64
Binary file not shown.
Binary file added bin/gha-docker-image-exists-linux-amd64
Binary file not shown.
Binary file added bin/gha-docker-image-exists-linux-arm64
Binary file not shown.
Binary file added bin/gha-docker-image-exists-windows-amd64
Binary file not shown.
Binary file added bin/gha-docker-image-exists-windows-arm64
Binary file not shown.
2 changes: 2 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,10 @@ func main() {
imageExists := doesImageExist(ctx, client, config.image, authStr)

if imageExists {
fmt.Println("Image exists")
os.Exit(0)
}

fmt.Println("Image does not exist")
os.Exit(1)
}
45 changes: 45 additions & 0 deletions shim.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
const childProcess = require('child_process')
const os = require('os')
const process = require('process')

function chooseBinary() {
const platform = os.platform()
const arch = os.arch()

if (platform === 'linux' && arch === 'x64') {
return `gha-docker-image-exists-linux-amd64`
}
if (platform === 'linux' && arch === 'arm64') {
return `gha-docker-image-exists-linux-arm64`
}
if (platform === 'windows' && arch === 'x64') {
return `gha-docker-image-exists-windows-amd64`
}
if (platform === 'windows' && arch === 'arm64') {
return `gha-docker-image-exists-windows-arm64`
}
if (platform === 'darwin' && arch === 'x64') {
return `gha-docker-image-exists-darwin-amd64`
}
if (platform === 'darwin' && arch === 'arm64') {
return `gha-docker-image-exists-darwin-arm64`
}

console.error(`Unsupported platform (${platform}) and architecture (${arch})`)
process.exit(1)
}

function main() {
const binary = chooseBinary()
const mainScript = `${__dirname}/bin/${binary}`
const spawnSyncReturns = childProcess.spawnSync(mainScript, { stdio: 'inherit' })
const status = spawnSyncReturns.status
if (typeof status === 'number') {
process.exit(status)
}
process.exit(1)
}

if (require.main === module) {
main()
}