Giter VIP home page Giter VIP logo

go-githubactions's Introduction

Hi there ๐Ÿ‘‹

I'm an engineer at Google. Previously I worked at HashiCorp, Chef Software, CustomInk, and some Pittsburgh-based startups.

  • ๐Ÿ’ฌ Ask me about: Go, Ruby
  • ๐Ÿ˜„ Pronouns: he/him
  • ๐ŸŒ Website

go-githubactions's People

Contributors

abhinav avatar aleksi avatar ashutoshgngwr avatar christiansch avatar dhermes avatar guilhem avatar ianlewis avatar mtharp avatar myitcv avatar ribice avatar sethvargo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

go-githubactions's Issues

[DISCUSSION] Is it worth it to provide a `GetInput()` equivalent of `os.LookupEnv()`?

I am using action.GetInput("foo") == "" right now, but there may be a desired for something like this as well:

// LookupInput checks for an input by the given name.
func (c *Action) LookupInput(i string) (string, bool) {
	e := strings.ReplaceAll(i, " ", "_")
	e = strings.ToUpper(e)
	e = "INPUT_" + e

	value, exists := os.LookupEnv(e)  // c.lookupEnv?
	return strings.TrimSpace(value), exists
}

Proposal: rename module and package

Firstly, thank you very much for starting this project. Your blog post inspired me to try and take the idea further:

https://blog.myitcv.io/2020/02/04/portable-ci-cd-with-pure-go-github-actions.html

I wonder, however, whether in these v0 days we should consider a different module path and package name:

github.com/sethvargo/go-githubactions has the disadvantage that:

  • the base of the import path does not match the package name
  • the package name, githubactions, is rather long

Options we have:

  1. Change the module (and package) path to github.com/sethvargo/actions
  2. Switch to a vanity URL (so that the backing VCS repo can be named whatever we like): sethvargo.com/actions

Thoughts?

Set ENV and Add Path commands deprecated

Getting commit message from GITHUB_CONTEXT

I'm looking for a way to get the commit message. Is this possible?

Currently I do the following, but it's verbose in yaml

      - run: echo "COMMIT_MESSAGE=${{ github.event.workflow_run.head_commit.message }}" >> $GITHUB_ENV
        shell: bash
        if: github.event_name == 'workflow_run'

      - run: echo "COMMIT_MESSAGE=${{ github.head_ref }}" >> $GITHUB_ENV
        shell: bash
        if: github.event_name == 'pull_request'

      - run: echo "COMMIT_MESSAGE=${{ github.event.commits[0].message }}" >> $GITHUB_ENV
        shell: bash
        if: github.ref == 'refs/heads/main'

Support for getting the "context that the current action is being run in"

Your article was a great resource for helping get started with GitHub actions using Go! Thanks!

At the moment this package mainly supports the features that @actions/core has.

I was wondering if you had plans on supporting the features that @actions/github provides? From what I read, the context information this package provides can mainly be found in the events.json file (The path of it you can get with in the environment variable GITHUB_EVENT_PATH)

I would be happy to take a stab at it and make a pull request, but thought I'd ask first to see what your plans were. The way that GitHub has the toolkit repository structured is having multiple "packages" within one repo, so wasn't sure if that is what you were envisioning in copying as well. Also probably using another name then "context" to describe the events payload might be a good idea to prevent overloading the meaning of that word.

Thanks!

Testing `SetOutput` calls

I'm writing GitHub actions which make use of this library and want to include the SetOutput calls within the tests, so that the key/value being outputted can be asserted on. Do you know if there's any libraries that can parse the GitHub environment file which include <<? Or if a helper function can be added to the library to parse out a GitHub environment file?

Expose a field in `Action` with `getenvFunc`.

I am happy to do the work here, I just wanted sign off before sending a PR. I'm also happy to conform to your preferences, some possible design choices:

  • Make Action.GetEnv an exported field OR make Action.getEnv unexported
  • Support an options pattern in New() so that GetEnv can be optionally set OR provide a NewWithGetEnv() function similar to NewWithWriter() OR provide a Action.WithGetEnv() method for setting the field
  • Check if Action.GetEnv is nil before usage and fallback to os.Getenv (would require a wrapper method) OR trust that Action.GetEnv: os.Getenv will be the default set in New() and leave it at that

Motivation: This makes it somewhat easier to write unit tests for GitHub actions.

Just now I am testing an "input parsing" function and ended up defining

type getInputer interface {
	GetInput(string) string
}

func NewFromInputs(action getInputer) (*Config, error) {
	// ...
}

so that in my tests I can use

type GetInputMap struct {
	Map map[string]string
}

func (gim GetInputMap) GetInput(i string) string {
	return gim.Map[i]
}

[FEATURE] Add support for `Action.GitHubContext()`

My original motivation was to have a method like

action.GitHubContext().Labels()

and have this method check GITHUB_EVENT_NAME == "pull_request" and then (if equal) would parse GITHUB_EVENT_PATH and return the equivalent of ${{ github.event.pull_request.labels }}.

In general for an action it may be nice to have access to things like GITHUB_REF to provide programmatic defaults vs. hardcoded values in action.yml.


I captured an example create event (also a push event) and we have environment variables:

GITHUB_ACTION=run1
GITHUB_ACTIONS=true  # Not in `${{ github }}` context
GITHUB_ACTION_REF=
GITHUB_ACTION_REPOSITORY=
GITHUB_ACTOR=dhermes
GITHUB_API_URL=https://api.github.com
GITHUB_BASE_REF=
GITHUB_ENV=/home/runner/work/_temp/_runner_file_commands/set_env_735e47f8-a348-42e3-ba30-cafcd54f1c9b
GITHUB_EVENT_NAME=create
GITHUB_EVENT_PATH=/home/runner/work/_temp/_github_workflow/event.json
GITHUB_GRAPHQL_URL=https://api.github.com/graphql
GITHUB_HEAD_REF=
GITHUB_JOB=ci
GITHUB_PATH=/home/runner/work/_temp/_runner_file_commands/add_path_735e47f8-a348-42e3-ba30-cafcd54f1c9b
GITHUB_REF=refs/heads/trigger-many
GITHUB_REPOSITORY=dhermes/bezier
GITHUB_REPOSITORY_OWNER=dhermes
GITHUB_RETENTION_DAYS=90
GITHUB_RUN_ID=818468044
GITHUB_RUN_NUMBER=2
GITHUB_SERVER_URL=https://github.com
GITHUB_SHA=be377e49cf9c2bb751374cc6ee770fee98df3293
GITHUB_WORKFLOW=Trigger Many
GITHUB_WORKSPACE=/home/runner/work/bezier/bezier

where the equivalent ${{ github }} context (keys re-ordered alphabetically) is

{
    "action": "run2",
    "action_ref": "",
    "action_repository": "",
    "actor": "dhermes",
    "api_url": "https://api.github.com",
    "base_ref": "",
    "env": "/home/runner/work/_temp/_runner_file_commands/set_env_735e47f8-a348-42e3-ba30-cafcd54f1c9b",
    "event": {...},
    "event_name": "create",
    "event_path": "/home/runner/work/_temp/_github_workflow/event.json",
    "graphql_url": "https://api.github.com/graphql",
    "head_ref": "",
    "job": "ci",
    "path": "/home/runner/work/_temp/_runner_file_commands/add_path_735e47f8-a348-42e3-ba30-cafcd54f1c9b",
    "ref": "refs/heads/trigger-many",
    "repository": "dhermes/bezier",
    "repositoryUrl": "git://github.com/dhermes/bezier.git", // Not in GITHUB_* env vars
    "repository_owner": "dhermes",
    "retention_days": "90",
    "run_id": "818468044",
    "run_number": "2",
    "server_url": "https://github.com",
    "sha": "be377e49cf9c2bb751374cc6ee770fee98df3293",
    "token": "***", // Not in GITHUB_* env vars
    "workflow": "Trigger Many",
    "workspace": "/home/runner/work/bezier/bezier"
}

We can try to leverage something like https://github.com/google/go-github to parse the contents of the file in GITHUB_EVENT_PATH.

Can't get input value.

Hi,

I'm barely new to Github action and try to make your awesome project work. I tried to make it as simple as possible.

First, I have a repo with my Go code which contains a Go project, simple main.go (and go.mod / go.sum). Here is my main, pretty straightforward, similar as your project's README. It takes an input and set a time value as output.

package main

import (
	"fmt"
	"time"

	"github.com/sethvargo/go-githubactions"
)

func main() {

	user := githubactions.GetInput("who")
	fmt.Printf("User : %s\n", user)

	time := time.Now()
	githubactions.SetOutput("time", time.String())
	fmt.Printf("Time : %s", time)
}

In the same repo I have an action.yml file which describe how to run it :

name: "Say hello"
description: "Github Action to say hello"

inputs:
  who:
    description: 'User to say hello'
    required: true 
outputs:
  time:
    description: 'The time we said hello'

runs:
  using: composite
  steps:
  - name: Build 
    working-directory: src/
    run: go build -o ../bin/main
    shell: bash
  - name: Run
    working-directory: bin/
    run: ./main
    shell: bash

In another repo I try to make it works with a workflow file like this :

name: Go

on:
  push:
    branches: [ main ]
  pull_request:
    branches: [ main ]

jobs:

  build:
    name: Build
    runs-on: ubuntu-latest
    steps:
    - name: Check out code into the Go module directory
      uses: actions/checkout@v3

    - name: Set up Go 1.19
      uses: actions/setup-go@v3
      with:
        go-version: 1.19
    
    - name: Checkout code action
      uses: actions/checkout@v2
      with:
        repository: me/myrepo
    
    - name: Go example
      uses: me/myrepo@main
      with:
        who: 'John Doe'

All steps run fine, but "who" input can't be retrieved.

Here are the logs for Go example build step :

Run me/repo@main
Run go build -o ../bin/main
go: downloading github.com/sethvargo/go-githubactions v1.1.0
go: downloading github.com/sethvargo/go-envconfig v0.8.0
Run ./main
User : 
Time : 2023-01-27 08:15:35.300536781 +0000 UTC m=+0.000578302

Output is nicely set but I can't figure out why user is empty.

Composite actions are not mentioned

README says:

By default, GitHub Actions expects actions to be written in Node.js. For other languages like Go, you need to provide a Dockerfile

There is a third option now โ€“ composite actions. Given that all GitHub runners have Go preinstalled, that provides an easier alternative to building and publishing Docker images or using nodejs shims.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.