E

Go Build with Caching Support Example

Summary

The user is looking for an example of how to build Go with caching support for GOCACHE and GOMODCACHE. They provide a script that includes functions for configuring GitHub access for private repositories and setting up environment variables for Go applications. The script includes a function to configure GitHub authentication, either using SSH or plain credentials, and another function to prepare the Go environment with caching. Finally, there is a function to run Go commands with the configured GitHub settings.

Status
open
Tags
    Source
    #earthly
      i

      ingwar

      9/24/2024

      Maybe not canonical.. But works rather well..

      # Its mostly used for go programs that reach private repositories.
      # Call example:
      # DO commons+CONFIGURE_GITHUB --GITHUB_AUTH=ssh
      # DO commons+CONFIGURE_GITHUB --GITHUB_AUTH=plain
      CONFIGURE_GITHUB:
          FUNCTION
          ARG GITHUB_AUTH=ssh
          ENV GITHUB_AUTH=$GITHUB_AUTH
          IF [ "$GITHUB_AUTH" = "plain" ]
              RUN git config --global credential.helper '!p() { printf "username=${GIT_USER}\\npassword=${GIT_PASS}"; }; p'
          ELSE
              RUN git config --global url."<ssh://git@github.com/>".insteadOf <https://github.com/>
              RUN mkdir -p -m 0600 ~/.ssh &amp;&amp; ssh-keyscan <http://github.com|github.com> &gt;&gt; ~/.ssh/known_hosts
          END
      
      # Set all env parameters needed for GO applications, also set up caches to make things faster
      # Call example:
      # DO commons+KTB_GO_PREPARE
      KTB_GO_PREPARE:
          FUNCTION
          ENV GOCACHE=/go-cache
          ENV GOMODCACHE=/go-mod-cache
          ENV GOPRIVATE=<http://github.com/kentik|github.com/kentik>
          CACHE --sharing=shared --id=kbt_go_cache $GOCACHE
          CACHE --sharing=shared --id=kbt_go_mod_cache $GOMODCACHE
      
      # Run FUNCTION with github configured
      # Its mostly used for go programs that reach private repositories.
      # Call example:
      # DO commons+GO_WITH_GITHUB --cmd="go build"
      GO_WITH_GITHUB:
          FUNCTION
          DO +KTB_GO_PREPARE
          ARG --required cmd
          ARG DESTDIR=/build/output
          ARG DEBUG_PIPELINE=false
          ARG GO_TEST_FLAGS
          IF [ "$GITHUB_AUTH" = "plain" ]
              RUN --secret GIT_USER=GITHUB_USER --secret GIT_PASS=GITHUB_TOKEN \
                  eval $cmd
          ELSE
              RUN --ssh eval $cmd
          END```
      
      j

      joshua.gilman

      9/24/2024

      Anyone have a canonical example of building Go with support for mount caching the GOCACHE and GOMODCACHE?