Skip to content

Commit

Permalink
Merge pull request #4 from jprosenbaum/dockerize-for-github-execution
Browse files Browse the repository at this point in the history
Dockerize for GitHub execution
  • Loading branch information
Emm authored Oct 29, 2023
2 parents 374876d + 0b7fc33 commit 05e9ddb
Show file tree
Hide file tree
Showing 10 changed files with 215 additions and 4 deletions.
9 changes: 9 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
template.md.jinja
config.toml
Dockerfile
README.md

/examples
/.github
/repos

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
/config.toml
/shortcut_client
/target
/repos
36 changes: 36 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
FROM rust:1.70-buster as builder

RUN apt-get update
RUN apt-get install -y jq curl maven

ENV OPENAPI_GENERATOR_VERSION=6.6.0
RUN curl "https://repo1.maven.org/maven2/org/openapitools/openapi-generator-cli/$OPENAPI_GENERATOR_VERSION/openapi-generator-cli-$OPENAPI_GENERATOR_VERSION.jar" -o /usr/local/lib/openapi-generator.jar

WORKDIR /usr/src/shortcut_release_helper
COPY --chmod=700 bin/generate_openapi_client.sh bin/cleanup.sh bin/
COPY --chmod=700 docker/openapi-generator-cli /usr/local/bin/
COPY Cargo.toml Cargo.lock ./
COPY shortcut_release_helper ./shortcut_release_helper/
RUN ./bin/generate_openapi_client.sh
RUN cargo build --release --bin shortcut_release_helper


FROM debian:bullseye-slim
RUN apt update \
&& apt install -y curl jq git \
&& rm -rf /var/lib/apt/lists/*

## GITHUB Actions execution group is 123 while testing, locally it is 1000
## This is to resolve file ownership issues will running inside a container
RUN groupadd -g 123 schelper \
&& useradd -m -u 1001 -g schelper schelper
USER schelper

WORKDIR /src
RUN git config --global --add safe.directory "*"

CMD ["/script/execute.sh"]

COPY --chmod=700 docker/execute.sh /script/execute.sh
COPY --from=builder /usr/src/shortcut_release_helper/target/release /usr/local/bin/shortcut_release_helper

42 changes: 40 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,24 +74,61 @@ Ensure that the dependencies below (both utilities and libararies) are present i

- [OpenSSL](https://www.openssl.org/)

### Building
### Building Locally

Clone the repository.

Generate the OpenAPI client via `./bin/generate_openapi_client.sh`.

Build the application via `cargo build`

### Building with Docker

Clone the repository.

If desired Regenerate the OpenAPI client via `./bin/generate_openapi_client.sh`.

Build the application via `docker build . -t shortcut_release_helper:local`

# Usage

Build a Configuration and jinja Template file

## Locally

```bash
$ shortcut_release_helper --version 3.22.1 --name "My new release" notes.md
```

## Docker

Mount `/src` to a local folder to capture generated report
Mount all repos to be scanned under the `/src` folder.
Mount the `config.toml` and `template.md.jinja` under the `/src` folder.

Output Report will be written to the `/src` folder

```bash
docker run --rm -v $(pwd)/repos:/src \
-v $(pwd)/config.toml:/src/config.toml \
-v $(pwd)/examples/template.md.jinja:/src/template.md.jinja \
shortcut_release_helper:local
```

## Github Action

GitHub Workflow example provided under `./examples`

## Configuration

The software expects a `config.toml` configuration file in the current folder.

* The `path_to_the_repo` may be absolute or relative to the current directory.
* The `branch_name_or_commit` must be a branch name or full SHA. The short SHA will not work.
* To pull latest commit SHA from a branch using the `production` tag

`git log --oneline --tags="*production*" --reverse -n 1 --format=%H`

It has the following format:

```toml
Expand All @@ -108,12 +145,13 @@ repo2_name = { location = "<path_to_the_repo>", release_branch = "<branch_name_o
repo3_name = { location = "<path_to_the_repo>", release_branch = "<branch_name_or_commit>", next_branch = "<branch_name_or_commit>" }
```

The `path_to_the_repo` may be absolute or relative to the current directory.

## Building the template

The template file is a Jinja Markdown template, which lays out the data retrieved from Shortcut.

Example provided: `./examples/template.md.jinja`

### Input data

The template receives the following data:
Expand Down
1 change: 1 addition & 0 deletions bin/cleanup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

set -eu
set -o pipefail
set -x

# Cleanup the generated code to ensure it compiles

Expand Down
3 changes: 3 additions & 0 deletions docker/execute.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/bash
git config --global --add safe.directory "*"
/usr/local/bin/shortcut_release_helper/shortcut_release_helper RELEASE_NOTES.md
96 changes: 96 additions & 0 deletions examples/github_action.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
name: 'release-notes-notification'

on:
workflow_dispatch:
schedule:
# * is a special character in YAML so you have to quote this string
- cron: '0 9 * * MON,WED,FRI'

permissions: read-all

concurrency:
group: '${{ github.workflow }} @ ${{ github.event.pull_request.head.label || github.head_ref || github.ref }}'
cancel-in-progress: true

env:
RELEASE_NOTES_EMAIL_FROM: ${{ vars.RELEASE_NOTES_EMAIL_FROM }}
RELEASE_NOTES_EMAIL_TO: ${{ vars.RELEASE_NOTES_EMAIL_TO }}
RELEASE_NOTES_EMAIL_SUBJECT: "Release Notes"

## Using GMAIL smtp relay
MAIL_URL: smtp+starttls://${{env.MAIL_USERNAME}}:${{env.MAIL_PASSWORD}}@${{env.MAIL_SERVER}}:${{env.MAIL_SERVER_PORT}}
MAIL_USERNAME: ${{ secrets.MAIL_USERNAME }}
MAIL_PASSWORD: ${{ secrets.MAIL_PASSWORD }}
MAIL_SERVER: "smtp.gmail.com"
MAIL_SERVER_PORT: 465

SHORTCUT_TOKEN: ${{ secrets.SHORTCUT_TOKEN }}
SHORTCUT_RELEASE_HELPER_IMAGE: ghcr.io/<username>/<repo>/<image>:<tag>

jobs:
release-notes:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3

- uses: actions/checkout@v3
with:
path: src/repo_one
repository: <REPOSITORY ONE>
token: ${{ secrets.ACTION_PAT_TOKEN }}
fetch-depth: 0 # Needed to pull TAG history

- uses: actions/checkout@v3
with:
path: src/repo_two
repository: <REPOSITORY TWO>
token: ${{ secrets.ACTION_PAT_TOKEN }}
fetch-depth: 0 # Needed to pull TAG history

- name: Configure
run: |
echo "Coping Jinja template"
cp $GITHUB_WORKSPACE/notifications/template.md.jinja $GITHUB_WORKSPACE/src
echo "Generating config.toml"
echo "api_key = '$SHORTCUT_TOKEN'" > $GITHUB_WORKSPACE/src/config.toml
echo "template_file = 'template.md.jinja'" >> $GITHUB_WORKSPACE/src/config.toml
echo "[repositories]" >> $GITHUB_WORKSPACE/src/config.toml
echo "Fetching Last Release Branch SHAs"
for dir in $GITHUB_WORKSPACE/src/*/ # list directories in the form "/src/dirname/"
do
GIT_RELEASE_SHA=""
dir=${dir%*/} # remove the trailing "/"
dir=$(echo "${dir##*/}") # print everything after the final "/"
## Looks for the last PRODUCTION tag added as the starting point for the release notes
GIT_RELEASE_SHA=$(cd $GITHUB_WORKSPACE/src/$dir && git log --oneline --tags="*production*" --reverse -n 1 --format=%H)
echo "Last Release Branch SHA - $dir - $GIT_RELEASE_SHA"
echo "$dir = { location = '/src/$dir', release_branch = '$GIT_RELEASE_SHA', next_branch = 'main' }" >> $GITHUB_WORKSPACE/src/config.toml
done
- name: Compile
run: |
cd ./src
docker login ghcr.io -u ${{ secrets.ACTION_PAT_USERNAME }} -p ${{ secrets.ACTION_PAT_TOKEN }}
docker run --rm -v $(pwd):/src ${{ env.SHORTCUT_RELEASE_HELPER_IMAGE }}
- name: Send
uses: dawidd6/action-send-mail@v3
# https://github.com/marketplace/actions/send-email
with:
connection_url: ${{env_MAIL_URL}}
server_address: ${{env.MAIL_SERVER}}
server_port: ${{env.MAIL_SERVER_PORT}}
secure: true

username: ${{env.MAIL_USERNAME}}
password: ${{env.MAIL_PASSWORD}}

subject: ${{ env.RELEASE_NOTES_EMAIL_SUBJECT }}
to: ${{ env.RELEASE_NOTES_EMAIL_TO }}
from: ${{ env.RELEASE_NOTES_EMAIL_FROM }}
html_body: file://src/RELEASE_NOTES.md
convert_markdown: true
priority: low
27 changes: 27 additions & 0 deletions examples/template.md.jinja
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Stage Release Notes

compiled on {{ today() }}


{%- for story in (stories | split_by_label("Release Notes") | first) %}
* [sc-{{ story.id }}]({{ story.app_url }}) {{ story.name }}
{%- endfor %}

---

## All Stories

{%- for story in stories %}
* [sc-{{ story.id }}]({{ story.app_url }}) {{ story.name }}
{%- endfor %}

---

## Changes missing story associations

{%- for commitgroup in unparsed_commits %}
### {{ commitgroup }}
{%- for commit in unparsed_commits[commitgroup] %}
* {{ commit.message | indent(2) }}
{%- endfor %}
{%- endfor %}
2 changes: 1 addition & 1 deletion shortcut_release_helper/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ mod types;

/// A command-line tool to generate release notes.
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
#[clap(author, about, long_about = None, disable_version_flag = true)]
struct Args {
/// Output file for the release notes
output_file: PathBuf,
Expand Down
2 changes: 1 addition & 1 deletion shortcut_release_helper/src/shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ pub fn parse_commits(
) -> Result<Commits> {
lazy_static! {
static ref SHORTCUT_RE: Regex =
Regex::new(r"^\[(?:sc-|ch)(\d+)\]").expect("Could not compile SHORTCUT_RE");
Regex::new(r"(?:\[sc-|\[ch|story/)(\d+)").expect("Could not compile SHORTCUT_RE");
};
let mut story_commits: HashMap<StoryId, RepoToCommits> = HashMap::new();
let mut unparsed_commits: RepoToCommits = HashMap::new();
Expand Down

0 comments on commit 05e9ddb

Please sign in to comment.