Skip to content

Commit

Permalink
Update node install and github actions.
Browse files Browse the repository at this point in the history
  • Loading branch information
beornf committed Jan 22, 2023
1 parent cb95779 commit 8a1a87e
Show file tree
Hide file tree
Showing 10 changed files with 98 additions and 74 deletions.
12 changes: 6 additions & 6 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ jobs:
pack: [go, node, php, python, ruby]
steps:
- name: Checkout
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # pin@v3
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # pin@v3
with:
fetch-depth: 0
- name: Install Go
uses: actions/setup-go@b22fbbc2921299758641fab08929b4ac52b32923 # pin@v3
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # pin@v3
with:
go-version: 1.18
- name: Test
Expand All @@ -23,21 +23,21 @@ jobs:
runs-on: ubuntu-latest
steps:
- name: Checkout
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b # pin@v3
uses: actions/checkout@ac593985615ec2ede58e132d2e21d2b1cbd6127c # pin@v3
with:
fetch-depth: 0
- name: Install Go
uses: actions/setup-go@b22fbbc2921299758641fab08929b4ac52b32923 # pin@v3
uses: actions/setup-go@6edd4406fa81c3da01a34fa6f6343087c207a568 # pin@v3
with:
go-version: 1.18
- name: Generate Token
id: generate-token
uses: tibdex/github-app-token@586e1a624db6a5a4ac2c53daeeded60c5e3d50fe # pin@v1
uses: tibdex/github-app-token@021a2405c7f990db57f5eae5397423dcc554159c # pin@v1
with:
app_id: ${{ secrets.APP_ID }}
private_key: ${{ secrets.PRIVATE_KEY }}
- name: Run GoReleaser
uses: goreleaser/goreleaser-action@68acf3b1adf004ac9c2f0a4259e85c5f66e99bef # pin@v3
uses: goreleaser/goreleaser-action@b508e2e3ef3b19d4e4146d4f8fb3ba9db644a757 # pin@v3
with:
args: release --rm-dist
env:
Expand Down
28 changes: 11 additions & 17 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Jet

![Jet mascot](jet-mascot.png)

[![Build Status](https://img.shields.io/github/workflow/status/lade-io/jet/Release.svg)](https://github.com/lade-io/jet/actions/workflows/release.yml)
[![Build Status](https://img.shields.io/github/actions/workflow/status/lade-io/jet/release.yml)](https://github.com/lade-io/jet/actions/workflows/release.yml)
[![Go.Dev Reference](https://img.shields.io/badge/go.dev-reference-blue.svg)](https://pkg.go.dev/github.com/lade-io/jet/pack)
[![Release](https://img.shields.io/github/v/release/lade-io/jet.svg)](https://github.com/lade-io/jet/releases/latest)

Expand Down Expand Up @@ -72,6 +70,8 @@ Debug Node.js app:
$ jet debug testdata/node/node12/
FROM node:12

ENV PATH=/home/node/app/node_modules/.bin:$PATH

USER node
RUN mkdir -p /home/node/app/
WORKDIR /home/node/app/
Expand All @@ -88,10 +88,9 @@ Debug Python and Django app:

```console
$ jet debug testdata/python/django/
FROM python:3.5
FROM python:3.9

ENV PATH=/home/web/.local/bin:$PATH
ENV PIP_USER=true

RUN groupadd --gid 1000 web \
&& useradd --uid 1000 --gid web --shell /bin/bash --create-home web
Expand All @@ -112,17 +111,13 @@ Debug Ruby on Rails app:

```console
$ jet debug testdata/ruby/rails5/
FROM ruby:2.6.5

RUN set -ex \
&& echo "deb http://deb.nodesource.com/node_11.x stretch main" > /etc/apt/sources.list.d/nodesource.list \
&& curl -fsSL https://deb.nodesource.com/gpgkey/nodesource.gpg.key | apt-key add - \
&& echo "deb http://dl.yarnpkg.com/debian/ stable main" > /etc/apt/sources.list.d/yarn.list \
&& curl -fsSL https://dl.yarnpkg.com/debian/pubkey.gpg | apt-key add - \
&& apt-get update && apt-get install -y \
nodejs \
yarn \
&& rm -rf /var/lib/apt/lists/*
FROM ruby:2.7.7

RUN wget -qO node.tar.gz "https://nodejs.org/dist/v18.13.0/node-v18.13.0-linux-x64.tar.gz" \
&& tar -xzf node.tar.gz -C /usr/local --strip-components=1 \
&& rm node.tar.gz

RUN corepack enable

RUN groupadd --gid 1000 web \
&& useradd --uid 1000 --gid web --shell /bin/bash --create-home web
Expand All @@ -146,4 +141,3 @@ CMD ["sh", "-c", "puma -p ${PORT-3000}"]

* Test cases imported from [Cloud Foundry Buildpacks](https://github.com/cloudfoundry-community/cf-docs-contrib/wiki/Buildpacks)
licensed under [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0)
* Graphic designed by brgfx on [Freepik](http://www.freepik.com)
Binary file removed jet-mascot.png
Binary file not shown.
9 changes: 1 addition & 8 deletions pack/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@ type Metadata struct {
Path string
Process []string
Root Root
Sources []*Source
Tools []*Tool
User string
Variant string
Expand All @@ -53,17 +52,11 @@ type Root struct {
Key string
}

type Source struct {
Entry string
File string
Key string
}

type Tool struct {
Copy map[string][]string
Name string
Owner string
Archive bool
Archive string
Binary bool
Download string
Files []string
Expand Down
72 changes: 59 additions & 13 deletions pack/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ type cacheTransport struct {
rt http.RoundTripper
}

type nodeVersion struct {
Version string `json:"version"`
LTS interface{} `json:"lts"`
}

func (c *cacheTransport) RoundTrip(req *http.Request) (resp *http.Response, err error) {
req.Header.Set("Cache-Control", fmt.Sprintf("max-age=%d", c.maxAge))
resp, err = c.rt.RoundTrip(req)
Expand All @@ -45,6 +50,20 @@ func (c *cacheTransport) RoundTrip(req *http.Request) (resp *http.Response, err
return
}

func (n *nodeVersion) Download() string {
return fmt.Sprintf("https://nodejs.org/dist/%[1]s/node-%[1]s-linux-x64.tar.gz", n.Version)
}

func (n *nodeVersion) IsLTS() bool {
switch data := n.LTS.(type) {
case bool:
return data
case string:
return true
}
return false
}

func init() {
cache := diskcache.New(filepath.Join(os.TempDir(), "jet-cache"))
transport = &cacheTransport{
Expand All @@ -65,13 +84,25 @@ func init() {
}
}

func getDownload(tool *Tool) error {
func getDownload(tool *Tool) (err error) {
name := tool.Name
owner := tool.Owner
if owner == "" {
return nil
}

defer func() {
if err == nil && tool.Download == "" {
err = fmt.Errorf("%s tool not found", name)
}
}()

if name == "node" {
tool.Archive = "/usr/local"
tool.Download, err = getNodeDownload()
return err
}

client := github.NewClient(httpClient)
ctx := context.Background()
release, _, err := client.Repositories.GetLatestRelease(ctx, owner, name)
Expand All @@ -88,19 +119,36 @@ func getDownload(tool *Tool) error {
if binary.MatchString(asset.GetName()) {
tool.Download = asset.GetBrowserDownloadURL()
if strings.HasSuffix(tool.Download, ".tar.gz") {
tool.Archive = true
tool.Archive = "/usr/local/bin"
} else {
tool.Binary = true
}
break
}
}
if tool.Download == "" {
return fmt.Errorf("%s tool not found", name)
}
return nil
}

func getNodeDownload() (string, error) {
resp, err := httpClient.Get("https://nodejs.org/dist/index.json")
if err != nil {
return "", err
}
defer resp.Body.Close()

var versions []nodeVersion
if err = json.NewDecoder(resp.Body).Decode(&versions); err != nil {
return "", err
}

for _, version := range versions {
if version.IsLTS() {
return version.Download(), nil
}
}
return "", nil
}

func getPath(dir string, meta *Metadata) error {
defer func() {
current := filepath.Clean(meta.Path) == "."
Expand Down Expand Up @@ -300,17 +348,15 @@ func fileRead(dir, file string) ([]byte, error) {
const dockerString = `FROM {{.Name}}:{{.Version}}
{{if .Packages}}
RUN set -ex \
{{range .Sources}} && echo "{{.Entry}}" > /etc/apt/sources.list.d/{{.File}} \
&& curl -fsSL {{.Key}} | apt-key add - \
{{end}} && apt-get update && apt-get install -y \
&& apt-get update && apt-get install -y \
{{range .Packages}} {{.}} \
{{end}} && rm -rf /var/lib/apt/lists/*
{{end}}{{range .Tools}}{{if .Download}}{{if .Archive}}
RUN wget -O {{.Name}}.tar.gz "{{.Download}}" \
&& tar -xzf {{.Name}}.tar.gz -C /usr/local/bin --strip-components=1 \
RUN wget -qO {{.Name}}.tar.gz "{{.Download}}" \
&& tar -xzf {{.Name}}.tar.gz -C {{.Archive}} --strip-components=1 \
&& rm {{.Name}}.tar.gz
{{else if .Binary}}
RUN wget -O {{.Name}} "{{.Download}}" \
RUN wget -qO {{.Name}} "{{.Download}}" \
&& chmod +x {{.Name}} && mv {{.Name}} /usr/local/bin
{{else}}
RUN {{.Download}}
Expand All @@ -327,12 +373,12 @@ RUN groupadd --gid 1000 {{.User}} \
USER {{.User}}
RUN mkdir -p {{.Path}}
WORKDIR {{.Path}}
{{range $t := .Tools}}{{range $dir, $files := .Copy}}
{{range $t := .Tools}}{{if or .Copy .Install}}{{range $dir, $files := .Copy}}
COPY {{if $.User}}--chown={{$.User}}:{{$.User}} {{end}}
{{- range $files}}{{.}} {{end}}{{$dir}}/{{end}}{{if .Install}}
RUN {{range $i, $e := .Install}}{{if $i}} \
&& {{end}}{{if $t.Name}}{{$t.Name}} {{end}}{{$e}}{{end}}{{end}}
{{end}}{{if .Process}}
{{end}}{{end}}{{if .Process}}
CMD [{{range $i, $e := .Process}}{{if $i}}, {{end}}"{{$e}}"{{end}}]
{{end -}}
`
12 changes: 5 additions & 7 deletions pack/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,16 +16,14 @@ func (n *NodePack) Detect() bool {
}

func (n *NodePack) Metadata() *Metadata {
user := "node"
meta := &Metadata{
User: "node",
Env: map[string]string{
"PATH": "/home/" + user + "/app/node_modules/.bin:$PATH",
},
User: user,
}
if fileExists(n.WorkDir, "yarn.lock") {
meta.Packages = append(meta.Packages, "yarn")
meta.Sources = append(meta.Sources, &Source{
Entry: "deb http://dl.yarnpkg.com/debian/ stable main",
Key: "https://dl.yarnpkg.com/debian/pubkey.gpg",
File: "yarn.list",
})
meta.Tools = append(meta.Tools, &Tool{
Name: "yarn",
Files: []string{"**/package.json", "yarn.lock"},
Expand Down
4 changes: 2 additions & 2 deletions pack/python.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ func (p *PythonPack) Metadata() *Metadata {
case fileExists(p.WorkDir, "setup.py"):
meta.Tools = append(meta.Tools, &Tool{
Name: "pip-compile",
Download: "pip install pip-tools==5.5.0",
Files: []string{"setup.py"},
Download: "pip install pip-tools",
Files: []string{"."},
Install: []string{"setup.py"},
})
case fileExists(p.WorkDir, "environment.yml"):
Expand Down
21 changes: 7 additions & 14 deletions pack/ruby.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,18 @@ func (r *RubyPack) Metadata() *Metadata {
}

if node {
meta.Packages = append(meta.Packages, "nodejs")
meta.Sources = append(meta.Sources, &Source{
Entry: "deb http://deb.nodesource.com/node_11.x stretch main",
Key: "https://deb.nodesource.com/gpgkey/nodesource.gpg.key",
File: "nodesource.list",
meta.Tools = append(meta.Tools, &Tool{
Name: "node",
Owner: "nodejs",
})
}

if yarn {
meta.Packages = append(meta.Packages, "yarn")
meta.Sources = append(meta.Sources, &Source{
Entry: "deb http://dl.yarnpkg.com/debian/ stable main",
Key: "https://dl.yarnpkg.com/debian/pubkey.gpg",
File: "yarn.list",
})
meta.Tools = append(meta.Tools, &Tool{
Name: "yarn",
Files: []string{"package.json", "yarn.lock"},
Install: []string{"install"},
Name: "yarn",
Download: "corepack enable",
Files: []string{"package.json", "yarn.lock"},
Install: []string{"install"},
})
}
return meta
Expand Down
2 changes: 1 addition & 1 deletion testdata/php/composer/composer.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"require": {
"php": ">=7.2",
"php": "~8.1",
"ext-amqp": "*",
"ext-apcu": "*",
"ext-bz2": "*",
Expand Down
12 changes: 6 additions & 6 deletions testdata/ruby/sinatra/Gemfile.lock
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
GEM
remote: https://rubygems.org/
specs:
rack (1.5.2)
rack-protection (1.5.2)
rack (1.6.5)
rack-protection (1.5.3)
rack
sinatra (1.4.4)
rack (~> 1.4)
sinatra (1.4.8)
rack (~> 1.5)
rack-protection (~> 1.4)
tilt (~> 1.3, >= 1.3.4)
tilt (1.4.1)
tilt (>= 1.3, < 3)
tilt (2.0.6)

PLATFORMS
ruby
Expand Down

0 comments on commit 8a1a87e

Please sign in to comment.