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

Add simple logging #2

Merged
merged 12 commits into from
Dec 11, 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
41 changes: 41 additions & 0 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,44 @@ jobs:
with:
commit_message: Update compiled binaries
file_pattern: "bin/*"

gen-tests:
needs: [lint, build]
runs-on: ubuntu-latest
permissions:
contents: write
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
token: ${{ secrets.GHA_TOKEN }}

- name: Install Task
uses: arduino/setup-task@v1

- name: Generate tests
run: |
ACTION_VERSION=${{ github.head_ref }} task gen-tests

- uses: stefanzweifel/git-auto-commit-action@v5
if: github.event_name == 'pull_request'
with:
commit_message: "[skip ci] Update tests"
file_pattern: ".github/workflows/test.yaml"

trigger-tests:
needs: [gen-tests]
runs-on: ubuntu-latest
permissions:
contents: write
actions: write
steps:
- name: Trigger generated tests
uses: convictional/[email protected]
with:
owner: Y0sh1dk
repo: gha-docker-image-exists
github_token: ${{ secrets.GITHUB_TOKEN }}
workflow_file_name: test.yml
ref: ${{ github.head_ref}}
wait_workflow: true
41 changes: 41 additions & 0 deletions .github/workflows/test.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: gha-docker-image-exists Test

on:
workflow_dispatch:

jobs:
success:
runs-on: ubuntu-latest
steps:
- name: GHA Docker Image Exists
id: gha-docker-image-exists
continue-on-error: true
uses: Y0sh1dk/gha-docker-image-exists@feature/logging
with:
image: nginx:latest

- name: Check output
run: |
if [ ${{ steps.gha-docker-image-exists.outcome == 'success' }} ]; then
exit 0
else
exit 1
fi

fail:
runs-on: ubuntu-latest
steps:
- name: GHA Docker Image Exists
id: gha-docker-image-exists
continue-on-error: true
uses: Y0sh1dk/gha-docker-image-exists@feature/logging
with:
image: nginx:invalid-tag

- name: Check output
run: |
if [ ${{ steps.gha-docker-image-exists.outcome == 'failure' }} ]; then
exit 0
else
exit 1
fi
41 changes: 41 additions & 0 deletions .github/workflows/test.yaml.tmpl
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
name: [[[ .Env.WORKFLOW_NAME ]]] Test

on:
workflow_dispatch:

jobs:
success:
runs-on: ubuntu-latest
steps:
- name: GHA Docker Image Exists
id: gha-docker-image-exists
continue-on-error: true
uses: Y0sh1dk/gha-docker-image-exists@[[[ .Env.ACTION_VERSION ]]]
with:
image: nginx:latest

- name: Check output
run: |
if [ ${{ steps.gha-docker-image-exists.outcome == 'success' }} ]; then
exit 0
else
exit 1
fi

fail:
runs-on: ubuntu-latest
steps:
- name: GHA Docker Image Exists
id: gha-docker-image-exists
continue-on-error: true
uses: Y0sh1dk/gha-docker-image-exists@[[[ .Env.ACTION_VERSION ]]]
with:
image: nginx:invalid-tag

- name: Check output
run: |
if [ ${{ steps.gha-docker-image-exists.outcome == 'failure' }} ]; then
exit 0
else
exit 1
fi
24 changes: 24 additions & 0 deletions Taskfile.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,19 @@ tasks:
golang:{{ .GO_VERSION }} \
{{ .COMMAND }}

_gomplate-docker: &gomplate-docker
internal: true
cmds:
- |
docker run \
--rm \
-e WORKFLOW_NAME={{ .WORKFLOW_NAME }} \
-e ACTION_VERSION={{ .ACTION_VERSION }} \
-v $(pwd):/app \
-w /app \
hairyhenderson/gomplate:stable \
{{ .COMMAND }}

build:
<<: *go-docker
internal: false
Expand All @@ -43,6 +56,17 @@ tasks:
vars:
COMMAND: go build -o bin/{{ .GIT_REPO_NAME }}-{{ .GOOS }}-{{ .GOARCH }} *.go {{ .CLI_ARGS }}

gen-tests:
<<: *gomplate-docker
internal: false
desc: Generate test workflows
requires:
vars: [ACTION_VERSION]
vars:
WORKFLOW_NAME: "{{ .GIT_REPO_NAME }}"
ACTION_VERSION: "{{ .ACTION_VERSION }}"
COMMAND: -f .github/workflows/test.yaml.tmpl --left-delim "[[[" --right-delim "]]]" > .github/workflows/test.yaml

build-all:
desc: Build the application for all platforms
vars:
Expand Down
Binary file modified bin/gha-docker-image-exists-darwin-amd64
Binary file not shown.
Binary file modified bin/gha-docker-image-exists-darwin-arm64
Binary file not shown.
Binary file modified bin/gha-docker-image-exists-linux-amd64
Binary file not shown.
Binary file modified bin/gha-docker-image-exists-linux-arm64
Binary file not shown.
Binary file modified bin/gha-docker-image-exists-windows-amd64
Binary file not shown.
Binary file modified bin/gha-docker-image-exists-windows-arm64
Binary file not shown.
17 changes: 17 additions & 0 deletions config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package main

import (
"fmt"

"github.com/docker/docker/api/types/registry"
"github.com/sethvargo/go-githubactions"
)
Expand All @@ -10,9 +12,17 @@ type Config struct {
password string
serverAddress string
image string
action *githubactions.Action
}

func (c Config) String() string {
return fmt.Sprintf("Config{username: %s, password: #####, serverAddress: %s, image: %s}", c.username, c.serverAddress, c.image)
}

func (c *Config) GetAuthString() (string, error) {
c.action.Debugf("Getting auth string...")
defer c.action.Debugf("Got auth string")

authConfig := registry.AuthConfig{
Username: c.username,
Password: c.password,
Expand All @@ -28,6 +38,9 @@ func (c *Config) GetAuthString() (string, error) {
}

func NewFromInputs(action *githubactions.Action) (*Config, error) {
action.Debugf("Getting inputs...")
defer action.Debugf("Got inputs")

username := getInputDefault(action, "username", "")
password := getInputDefault(action, "password", "")
serverAddress := getInputDefault(action, "server_address", "")
Expand All @@ -38,12 +51,16 @@ func NewFromInputs(action *githubactions.Action) (*Config, error) {
password: password,
serverAddress: serverAddress,
image: image,
action: action,
}

return &c, nil
}

func getInputDefault(action *githubactions.Action, key, fallback string) string {
action.Debugf("Getting input %s...", key)
defer action.Debugf("Got input %s", key)

if value := action.GetInput(key); value != "" {
return value
}
Expand Down
15 changes: 7 additions & 8 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,29 +25,28 @@ func main() {

config, err := NewFromInputs(action)
if err != nil {
panic(err)
action.Fatalf("Failed to get inputs: %s", err)
}

fmt.Println(config)
action.Debugf(config.String())

client, err := getDockerClient()
if err != nil {
panic(err)
action.Fatalf("Failed to get docker client: %s", err)
}
defer client.Close()

authStr, err := config.GetAuthString()
if err != nil {
panic(err)
action.Fatalf("Failed to get auth string: %s", err)
}

imageExists := doesImageExist(ctx, client, config.image, authStr)

client.Close()

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

fmt.Println("Image does not exist")
os.Exit(1)
action.Fatalf("Image does not exist")
}
Loading