Skip to content

Commit

Permalink
Fix/error when no change (#69)
Browse files Browse the repository at this point in the history
* fix(): only do PR if some changes detected (#16)

* fix(): add hash to branch name

* fix(): add target branch

* chore(): switch from hub to github-cli

* fix(): fix gh command

* fix(): change variable in PR body
  • Loading branch information
AndreasAugustin authored May 4, 2021
1 parent 0cf7095 commit a1a027d
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 52 deletions.
26 changes: 8 additions & 18 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,38 +1,28 @@
#######################################
# image for dev build environment
######################################
FROM golang:1.16.3-alpine as DEV
FROM alpine:3.13 as dev

ARG GH_CLI_VER=1.9.2

# install packages
RUN apk add --update --no-cache bash make git zsh curl tmux musl build-base openssh
RUN apk add --update --no-cache bash make git zsh curl tmux musl

RUN wget https://github.com/cli/cli/releases/download/v${GH_CLI_VER}/gh_${GH_CLI_VER}_linux_386.tar.gz -O ghcli.tar.gz
RUN tar --strip-components=1 -xf ghcli.tar.gz

# Make zsh your default shell for tmux
RUN echo "set-option -g default-shell /bin/zsh" >> /root/.tmux.conf

# install oh-my-zsh
RUN sh -c "$(curl -fsSL https://raw.githubusercontent.com/robbyrussell/oh-my-zsh/master/tools/install.sh)"

# github-cli not stable yet
#RUN git clone https://github.com/cli/cli.git gh-cli \
# && cd gh-cli \
# && make \
# && mv ./bin/gh /usr/local/bin/

RUN apk add --update --no-cache groff util-linux
RUN git clone \
--config transfer.fsckobjects=false \
--config receive.fsckobjects=false \
--config fetch.fsckobjects=false \
https://github.com/github/hub.git \
&& cd hub \
&& make install prefix=/usr/local

WORKDIR /app

#######################################
# image for creating the documentation
######################################
FROM node:16.0.0-alpine as DOCS
FROM node:16.0.0-alpine as docs

# install packages
RUN apk add --update --no-cache bash make git zsh curl tmux
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,11 @@ jobs:
- name: Checkout
uses: actions/checkout@v2
- name: actions-template-sync
uses: AndreasAugustin/[email protected].1-draft
uses: AndreasAugustin/[email protected].2-draft
with:
github_token: ${{ secrets.GITHUB_TOKEN }}
source_repo_path: <owner/repo>
upstream_branch: <target_branch> # defaults to main
```
You will receive a pull request within your repository if there are some changes available.
Expand Down
5 changes: 5 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,14 @@ inputs:
source_repo_path:
description: 'Repository path of the template'
required: true
upstream_branch:
description: 'The target branch'
required: true
default: 'main'
runs:
using: 'docker'
image: 'src/Dockerfile'
env:
GITHUB_TOKEN: ${{ inputs.github_token }}
SOURCE_REPO_PATH: ${{ inputs.source_repo_path }}
UPSTREAM_BRANCH: ${{ inputs.upstream_branch }}
4 changes: 2 additions & 2 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ services:
dev:
build:
context: .
target: DEV
target: dev
volumes:
- .:/app/
- ~/.gitconfig:/root/.gitconfig:ro
Expand All @@ -13,6 +13,6 @@ services:
docs:
build:
context: .
target: DOCS
target: docs
volumes:
- .:/app/
23 changes: 7 additions & 16 deletions src/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
FROM golang:1.14-alpine
FROM alpine:3.13

ARG GH_CLI_VER=1.9.2

# labels
LABEL \
"name"="GitHub template sync" \
Expand All @@ -7,22 +10,10 @@ LABEL \
"maintainer"="Andreas Augustin <[email protected]>"

# install packages
RUN apk add --update --no-cache bash make git zsh curl tmux musl build-base

# github-cli not stable yet
#RUN git clone https://github.com/cli/cli.git gh-cli \
# && cd gh-cli \
# && make \
# && mv ./bin/gh /usr/local/bin/
RUN apk add --update --no-cache bash git curl musl

RUN apk add --update --no-cache groff util-linux
RUN git clone \
--config transfer.fsckobjects=false \
--config receive.fsckobjects=false \
--config fetch.fsckobjects=false \
https://github.com/github/hub.git \
&& cd hub \
&& make install prefix=/usr/local
RUN wget https://github.com/cli/cli/releases/download/v${GH_CLI_VER}/gh_${GH_CLI_VER}_linux_386.tar.gz -O ghcli.tar.gz
RUN tar --strip-components=1 -xf ghcli.tar.gz

ADD *.sh /bin/
RUN chmod +x /bin/entrypoint.sh \
Expand Down
50 changes: 35 additions & 15 deletions src/sync_template.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,32 +6,52 @@ if [[ -z "${SOURCE_REPO}" ]]; then
exit 1;
fi

# if ! [ -x "$(command -v gh)" ]; then
# echo "Error: github-cli gh is not installed. 'https://github.com/cli/cli'" >&2;
# exit 1;
# fi
if [[ -z "${UPSTREAM_BRANCH}" ]]; then
echo "Error: Missing env variable 'UPSTREAM_BRANCH'" >&2;
exit 1;
fi

if ! [ -x "$(command -v hub)" ]; then
echo "Error: hub is not installed. 'https://github.com/github/hub'" >&2;
if ! [ -x "$(command -v gh)" ]; then
echo "Error: github-cli gh is not installed. 'https://github.com/cli/cli'" >&2;
exit 1;
fi

NEW_BRANCH="chore/template_sync"
TEMPLATE_VERSION_FILE_NAME=".templateversionrc"
TEMPLATE_REMOTE_GIT_HASH=$(git ls-remote "${SOURCE_REPO}" HEAD | awk {'print $1}')
NEW_TEMPLATE_GIT_HASH=$(git rev-parse --short "${TEMPLATE_REMOTE_GIT_HASH}")
NEW_BRANCH="chore/template_sync_${NEW_TEMPLATE_GIT_HASH}"

echo "start sync"
echo "create new branch from default branch with name ${NEW_BRANCH}"
git checkout -b ${NEW_BRANCH}
git checkout -b "${NEW_BRANCH}"
echo "pull changes from template"
git pull "${SOURCE_REPO}" --allow-unrelated-histories --squash --strategy=recursive -X theirs

echo "new Git HASH ${NEW_TEMPLATE_GIT_HASH}"
if [ -r ${TEMPLATE_VERSION_FILE_NAME} ]
then
CURRENT_TEMPLATE_GIT_HASH=$(cat ${TEMPLATE_VERSION_FILE_NAME})
echo "Current git hash ${CURRENT_TEMPLATE_GIT_HASH}"
fi

if [ "${NEW_TEMPLATE_GIT_HASH}" == "${CURRENT_TEMPLATE_GIT_HASH}" ]
then
echo "repository is up to date"
exit 0
fi

echo "write new template version file"
echo "${NEW_TEMPLATE_GIT_HASH}" > ${TEMPLATE_VERSION_FILE_NAME}
echo "wrote new template version file with content $(cat ${TEMPLATE_VERSION_FILE_NAME})"

git add .
git commit -m "chore(template): merge template changes :up:"

echo "push changes"
git push --set-upstream origin "${NEW_BRANCH}"
echo "create pull request"
# Workaround for `hub` auth error https://github.com/github/hub/issues/2149#issuecomment-513214342
export GITHUB_USER="$GITHUB_ACTOR"
hub pull-request \
-b master \
-h $NEW_BRANCH \
--no-edit
# gh pr create -B master -f -l chore

gh pr create \
--title "upstream merge template repository" \
--body "Merge ${SOURCE_REPO_PATH} ${NEW_TEMPLATE_GIT_HASH}" \
-B "${UPSTREAM_BRANCH}"

0 comments on commit a1a027d

Please sign in to comment.