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

feat(docker): rework docker on the spot #16

Merged
merged 3 commits into from
Nov 2, 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
2 changes: 1 addition & 1 deletion .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ jobs:
steps:
# Check out code
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
# This is the a separate action that sets up buildx runner
- name: Login to DockerHub
uses: docker/login-action@v1
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
steps:
# Check out code
- name: Checkout
uses: actions/checkout@v3
uses: actions/checkout@v4
- name: Docker meta
id: meta
uses: docker/metadata-action@v3
Expand Down Expand Up @@ -51,4 +51,4 @@ jobs:
release_name: Release ${{ github.ref }}
body: Latest Release
prerelease: false
draft: false
draft: false
25 changes: 25 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
name: Test
on: [push, pull_request]

jobs:
build:
runs-on: ubuntu-latest
env:
PROJECT_NAME: test-template
steps:
- uses: actions/checkout@v4

- name: Build Docker image
run: docker build -t test-cargo-generate .

- name: Run list
run: docker run -v ${PWD}/test:/test -w /test test-cargo-generate --path /test --name ${{ env.PROJECT_NAME }}

- uses: dtolnay/rust-toolchain@stable
# we need to move the generated project to a temp folder, away from the template project
# otherwise `cargo` runs would fail
# see https://github.com/rust-lang/cargo/issues/9922
- run: |
mv ${PWD}/test/${{ env.PROJECT_NAME }} ${{ runner.temp }}/
cd ${{ runner.temp }}/$PROJECT_NAME
cargo check
4 changes: 2 additions & 2 deletions .github/workflows/versioning.yml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,6 @@ jobs:
steps:
- uses: Actions-R-Us/actions-tagger@latest
env:
GITHUB_TOKEN: "${{secrets.GITHUB_TOKEN}}"
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
publish_latest_tag: true
publish_latest_tag: true
19 changes: 11 additions & 8 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
FROM rust:latest as builder
RUN cargo install cargo-generate
FROM rust:1.73.0-alpine3.18

FROM debian:stable-slim
ENV CARGO_GEN_VERSION="0.18.4"

RUN apt-get update \
&& apt-get install -y ca-certificates curl openssl libssl1.1 libcurl4-openssl-dev
# downloads from: https://github.com/cargo-generate/cargo-generate/releases/download/v0.18.4/cargo-generate-v0.18.4-x86_64-unknown-linux-musl.tar.gz
RUN set -eux; \
apk update; \
apk add bash curl git tar; \
curl --silent -L https://github.com/cargo-generate/cargo-generate/releases/download/v$CARGO_GEN_VERSION/cargo-generate-v$CARGO_GEN_VERSION-x86_64-unknown-linux-musl.tar.gz | tar xzv -C /usr/bin/;

COPY --from=builder /usr/local/cargo/bin/cargo-generate /usr/bin
# Ensure rustup is up to date.
RUN bash -c "sh <(curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs) -y"

# in order to avoid permission issues
# match here the GH runner UID and GID, so that generated files
Expand All @@ -16,8 +19,8 @@ ARG UID=1001
ARG GID=121
ARG USERNAME=runner
ARG GROUPNAME=docker
RUN groupadd -g $GID -o $GROUPNAME
RUN useradd -m -u $UID -g $GID -o -s /bin/bash $USERNAME
RUN addgroup -g $GID $GROUPNAME
RUN adduser -D -H -u $UID -s /bin/bash $USERNAME $GROUPNAME
USER $USERNAME

COPY entrypoint.sh /entrypoint.sh
Expand Down
30 changes: 8 additions & 22 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# cargo-generate GitHub Action

This action is used to run the `cargo-generate` tool in order to expand a template.

This is pushed to [docker hub cargogenerate/cargo-generate-action](https://hub.docker.com/r/cargogenerate/cargo-generate-action).
This action runs `cargo-generate` to expand a template.

## Inputs

Expand All @@ -16,28 +14,16 @@ This is the name of the project being expanded.

This is the path to the repository holding the template. Defaults to `.`

### `branch`

Branch for cloning the template.

### `subfolder`

Subfolder within the template repository that'll be used as the template.

### `template_values_file`

Specifies a file holding the values required for template expansion.

> NOTE: `cargo-generate` is being run with the `--silent` option, so it will fail if any values are undefined during expansion.
### `arguments`

### `other`
All other arguments that cargo-generate accepts

This can be used to specify any other option for `cargo-generate`.
> NOTE: `cargo-generate` runs with the `--silent` option, so it will fail if any values are undefined during expansion.

Example:

```yml
other: "--define foo=\"value\" --define bar=42"
arguments: "--branch x --define foo=\"value\" --define bar=42"
```

## Example
Expand All @@ -51,10 +37,10 @@ jobs:
build:
runs-on: ubuntu-latest
env:
PROJECT_NAME: mytemplate
PROJECT_NAME: project-foo
steps:
- uses: actions/checkout@v3
- uses: cargo-generate/cargo-generate-action@v0.18
- uses: actions/checkout@v4
- uses: cargo-generate/cargo-generate-action@latest
with:
name: ${{ env.PROJECT_NAME }}
- uses: actions-rs/toolchain@v1
Expand Down
20 changes: 8 additions & 12 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,18 +8,14 @@ inputs:
description: '--path argument'
required: false
default: '.'
branch:
description: '--branch argument'
required: false
subfolder:
description: '--subfolder argument'
required: false
template_values_file:
description: '--template-values-file argument'
required: false
other:
description: 'expanded to additional parameters! e.g. "-d foo=\"value1\" -d bar=\"value2\""'
arguments:
description: "The arguments to pass to cargo-generate"
required: false

runs:
using: 'docker'
image: 'docker://cargogenerate/cargo-generate-action:0.18.3'
image: 'Dockerfile'
args:
- --path ${{ inputs.template }}
- ${{ inputs.arguments }}
- ${{ inputs.name }}
35 changes: 5 additions & 30 deletions entrypoint.sh
Original file line number Diff line number Diff line change
@@ -1,35 +1,10 @@
#!/bin/bash
set -e

if [ -z "${INPUT_TEMPLATE}" ]; then
TEMPLATE="."
else
TEMPLATE=${INPUT_TEMPLATE}
fi

if [ -z "${INPUT_BRANCH}" ]; then
BRANCH=""
else
BRANCH="--branch ${INPUT_BRANCH}"
fi

if [ -z "${INPUT_TEMPLATE_VALUES_FILE}" ]; then
TEMPLATE_VALUES_FILE=""
else
TEMPLATE_VALUES_FILE="--template-values-file ${INPUT_TEMPLATE_VALUES_FILE}"
fi
export PATH=$PATH:/usr/local/cargo/bin

# USER variable MUST be set
if [ -z "${USER}" ]; then
export USER="root"
if [ -z "$USER" ]; then
export USER="runner"
fi

set -o xtrace
cargo-generate generate\
--silent\
${BRANCH}\
${SUBFOLDER}\
${TEMPLATE_VALUES_FILE}\
${INPUT_OTHER[@]}\
--path ${TEMPLATE}\
--name ${INPUT_NAME}\
${INPUT_SUBFOLDER}
/usr/bin/cargo-generate generate --silent $*
6 changes: 6 additions & 0 deletions test/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
/target
**/*.rs.bk
Cargo.lock
bin/
pkg/
wasm-pack.log
34 changes: 34 additions & 0 deletions test/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "{{project-name}}"
version = "0.1.0"
authors = ["{{authors}}"]
edition = "2018"

[lib]
crate-type = ["cdylib", "rlib"]

[features]
default = ["console_error_panic_hook"]

[dependencies]
wasm-bindgen = "0.2.63"

# The `console_error_panic_hook` crate provides better debugging of panics by
# logging them with `console.error`. This is great for development, but requires
# all the `std::fmt` and `std::panicking` infrastructure, so isn't great for
# code size when deploying.
console_error_panic_hook = { version = "0.1.6", optional = true }

# `wee_alloc` is a tiny allocator for wasm that is only ~1K in code size
# compared to the default allocator's ~10K. It is slower than the default
# allocator, however.
#
# Unfortunately, `wee_alloc` requires nightly Rust when targeting wasm for now.
wee_alloc = { version = "0.4.5", optional = true }

[dev-dependencies]
wasm-bindgen-test = "0.3.13"

[profile.release]
# Tell `rustc` to optimize for small code size.
opt-level = "s"
Loading
Loading