Explores GitHub Actions in Go Lab from GopherCon 2021

Overview

Gopher A Tweet

An action that tweets.

Gopher A Tweet was created based on GopherCon 2021s Gophers of Microsoft: GitHub Action in Go Lab to explore building a GitHub Action based in Go.

First Build

From the root of your repo, you should be able to run the following to build and test the Go action.

go build ./...
go test ./...

What's in Here?

.
├── Dockerfile
├── LICENSE
├── README.md
├── action.yaml
├── go.mod
├── go.sum
├── main.go
└── pkg
    └── tweeter
        ├── tweeter.go
        └── tweeter_test.go
action.yaml

The action.yml file contains the metadata which describes our action. This includes, but is not limited to the following.

  • name, description and author
  • inputs
  • outputs
  • branding
  • runs

You will see an example structure already exists. The example executes the Dockerfile and provides to it the arguments described in the runs section. We map the sample input to the arguments of the Dockerfile.

By setting runs.using: docker we are telling the Actions runtime to execute the Dockerfile when the Action is used in a workflow.

By setting runs.image: Dockerfile we are telling the Actions runtime to build and then execute the Dockerfile at the entrypoint defined in the Dockerfile. The build for the Dockerfile will happen each time the Action is executed, which can take a considerable amount of time depending on how long it takes to build your Go code. Later, we'll change this to a pre-built image for optimization.

Dockerfile

This Dockerfile should look relatively familiar to folks who use containers to build Go code. We create a builder intermediate image based on Go 1.15.2, pull in the source, and build the application. After the application has been built, the statically linked binary is copied into a thin image, which results in an image of roughly 8 MB.

go.mod / go.sum

Go module definition which will need to be updated with the name of your module.

main.go

This is the Go entrypoint for your GitHub Action. It is a simple command line application which can be executed outside the context of the Action by running the following. This is where you will add your Go code for your Action.

.github/workflows/tweeter-automation.yaml

This is the continuous integration / CLI release automation. The release workflow defined in this automation will is triggered by tags shaped like v1.2.3, and will create a GitHub release for a pushed tag. The release will use the ./github/release.yml to automatically generate structured release notes based on pull request tags.

.github/workflows/action-version.yaml

This action triggers when a new release is published. Upon publication of the new release, this will tag the repository with the shortened major semantic version pointing at the highest semantic version within that major version. For example, v1.2.2 and v1 point to the same ref xyz, a new tag is introduced v1.2.3 which points to ref abc, this action will move the v1 tag to point to abc rather than xyz. This enables consumers of an action to take a "floating" major semantic version dependency, like [email protected].

.github/workflows/image-release.yaml

This action runs on tags shaped like image-v1.2.3, and will build and push a container image to the GitHub Container Registry.

This action is super useful for optimizing the execution time of your action. By pre-building the image used in the Action, each invocation of your action can reference the image and not have to rebuild it for each invocation.

Once you push your first image you will also need to update the Container Registry to allow public access.

Lab Video

TODO: record and post the first lab walking through creation, execution and optimization

Lab Instructions

1) Clone a copy

Click on "Use this template" on https://github.com/the-gophers/go-action, and create a repo of your own. I'm going to call mine "test-gopher-action", make it public, and click "Create repository from template".

Checkout a new branch and let's make this our own GitHub Action. git checkout -b my-action

2) Run it

See your options with: go run . -h

$ go run . --dryRun --message hello
::set-output name=sentMessage::hello
3) Make it your own

Update ./action.yml name, description, and author to something reasonable. The name field needs to be unique to others in the store. Update links to reference your repo (at least the ghcr.io ones -- which will need to be lowercased).

When you are done with your changes, commit them, push your branch to GitHub, and open a pull request. In the PR, you should see the CI action run and complete successfully.

4) Merge it!

LGTM! Let's merge these changes. Click the "Merge pull request" button, then delete the branch.

Check out main and pull down the latest changes from GitHub (git pull).

5) Run the Action

In your test-gopher-action repo on GitHub, click on Actions. Navigate the UI to the running action and see that it built the action, built the Dockerfile and executed the entrypoint Go application.

Also note how long it took to run the action. Using a Dockerfile will cause it to rebuild that image EACH time the action runs!. We can do better than that. More ahead.

6) Tag a release

We'll need use semantic versioning for the action version workflow to pass. Let's tag our first release (git tag v1.0.0) and push the tag:

git tag v1.0.0
git push origin v1.0.0

More info on git tags if you're curious.

This should create our first release in GitHub via the release action workflow.

7) Publish a release

Navigate to the v1.0.0 release and click edit. Within the release edit page, you should see "Publish this Action to the GitHub Marketplace". If you check that box, your action will now be publicly advertised to all of GitHub!

8) Bonus points

PSA: The rest of this is optional. If you don't care about your action going fast, stop right here.

9) Tag an image

Now we are going to make this FAST by pre-baking our container image. Tag the repo with git tag image-v1.0.0 and then push the tag git push origin image-v1.0.0. This will kick off the image release build.

10) Speed boost

Replace image: Dockerfile with image: docker://ghcr.io/your-repo/your-image:1.0.0 replacing the repo and image name. Commit the changes and tag a new release of the Action as done in #7.

11) Speed run

Rerun the continuous integration and see how much faster the action runs now that it doesn't have to rebuild the container image each time.

Contributions

Always welcome! Please open a PR or an issue, and remember to follow the Gopher Code of Conduct.

Issues
  • Bring back speed boost

    Bring back speed boost

    Bring back boost after seeing the impact on pushing a new release without it.

    This action is slow if we build a new docker image every time. We can get a speed boost by using the most recent stable release image, image-v1.0.0, instead of building a new image every run.

    Without speed boost: Screen Shot 2021-12-09 at 8 43 58 PM

    With speed boost: Screen Shot 2021-12-09 at 8 44 48 PM

    opened by Autumn-Martin 0
  • Speed up gophering a tweet

    Speed up gophering a tweet

    Testing the action with a dry run has been slow, because building an image can take a minute. This update uses the docker image for the most recent release, v1.0.0, instead of building a new image every run.

    enhancement 
    opened by Autumn-Martin 0
  • Fix release failure from casing

    Fix release failure from casing

    Fixes an error that appeared during the first release for the tag being invalid because the repo name must be lowercase:

    Error: buildx failed with: error: invalid tag "ghcr.io/Autumn-Martin/gopher-a-tweet:1.0.0": repository name must be lowercase

    🐞 fix 
    opened by Autumn-Martin 0
Releases(v1.1.2)
Owner
Autumn Martin
Software Dev | Neuroscience
Autumn Martin
actionlint is a static checker for GitHub Actions workflow files.

actionlint actionlint is a static checker for GitHub Actions workflow files. Features: Syntax check for workflow files to check unexpected or missing

Linda_pp 858 Jun 29, 2022
GitHub Actions demo for a monorepo Go project

GitHub Actions demo for a monorepo Go project The purpose of this repository is to demonstrate using a GitHub action as a pull request status check in

Kalin Arsov 1 Oct 31, 2021
Golang-action - A template repository for writing custom GitHub Actions in Golang

Golang Action A template repository for writing custom GitHub Actions in Golang.

Taliesin Millhouse 0 Feb 12, 2022
Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://developer.github.com/v4/).

githubv4 Package githubv4 is a client library for accessing GitHub GraphQL API v4 (https://docs.github.com/en/graphql). If you're looking for a client

null 893 Jun 23, 2022
Google 29 May 25, 2021
A GitHub CLI extension that displays collaboration-related information about a GitHub repository.

collab-scanner GitHub CLI extension A GitHub CLI extension that displays collaboration-related information on a repository. Install gh extension insta

Nicolas Kosinski 4 Jun 2, 2022
Go library for accessing the GitHub API

go-github go-github is a Go client library for accessing the GitHub API v3. Currently, go-github requires Go version 1.9 or greater. go-github tracks

Google 8.6k Jun 30, 2022
Go library for accessing trending repositories and developers at Github.

go-trending A package to retrieve trending repositories and developers from Github written in golang. This package were inspired by rochefort/git-tren

Andy Grunwald 120 Jun 19, 2022
:fishing_pole_and_fish: Webhook receiver for GitHub, Bitbucket, GitLab, Gogs

Library webhooks Library webhooks allows for easy receiving and parsing of GitHub, Bitbucket and GitLab Webhook Events Features: Parses the entire pay

Go Playgound 733 Jul 2, 2022
Periodically collect data about my Twitter account and check in to github to preserve an audit trail.

Twitter audit trail backup This repository backs up my follower list, following list, blocked accounts list and muted accounts list periodically using

Ahmet Alp Balkan 59 Jun 18, 2022
Easily manage your github credentials

HUB ADMIN Hub Admin is a command-line tool managing your github credentials Installation go get github.com/crewdevio/HubAdmin How to use Open he

Crew Dev 5 Oct 20, 2021
A demo repo to show KICS Github Action in Action

?? KICS GitHub Actions Demo This repository shows how KICS GitHub Action can be set and was fully inspired by the documentation on KICS GitHub Actions

Checkmarx 0 Nov 23, 2021
A Github action to post to news.ycombinator.com

action-hackernews-post Unofficial A Github action to post to news.ycombinator.com Uses @lukakerr's hkn go module to login and post to HN Guidelines As

justin 2 Mar 12, 2022
A starting point for a GitHub Action based in Go.

GitHub Action Using Go This is a starting point for a GitHub Action based in Go. This repo provides all the structure needed to build a robust GitHub

Manuel Tiago Pereira 0 Dec 11, 2021
A GitHub action for the Go! programming language (by Francis McCabe, 2004)

Setup Go! (GitHub Action) This project is a GitHub action for the Go! programmin

Sean P. Myrick V19.1.7.2 1 Dec 21, 2021
A Github Action to auto approve pull requests that contain only document reviews.

Approve documentation review A Github Action to auto approve pull requests that contain only document reviews. The Cloud Platform team have a document

Ministry of Justice 0 Dec 23, 2021
GitHub Utilities for managing classroom repositories.

ghutil GitHub Utilities for bulk operations. Requirements A ghutil.toml configuration file is needed in the working directory. It should have entries

dbs67 0 Dec 21, 2021
Use go-github sdk in go.

go-github Use go-github sdk in go. Prerequisite GitHub API Token Installation clone the repo git clone [email protected]:siansiansu/go-github.git Simpil

Alex Su 0 Dec 27, 2021
Dynbio - A simple service for dynamic update my bio on github

dynbio A simple service for dynamic update my bio on github Instalation sudo ech

null 0 Jan 3, 2022