diff --git a/Dockerfile b/Dockerfile index 9d86ea3..4794a33 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,10 +1,15 @@ ####################################### # 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 @@ -12,27 +17,12 @@ 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 diff --git a/README.md b/README.md index 5286d20..23a36f2 100644 --- a/README.md +++ b/README.md @@ -37,10 +37,11 @@ jobs: - name: Checkout uses: actions/checkout@v2 - name: actions-template-sync - uses: AndreasAugustin/actions-template-sync@v0.1.1-draft + uses: AndreasAugustin/actions-template-sync@v0.1.2-draft with: github_token: ${{ secrets.GITHUB_TOKEN }} source_repo_path: + upstream_branch: # defaults to main ``` You will receive a pull request within your repository if there are some changes available. diff --git a/action.yml b/action.yml index 31bf842..0c66973 100644 --- a/action.yml +++ b/action.yml @@ -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 }} diff --git a/docker-compose.yml b/docker-compose.yml index 88a3b7b..ce5b08e 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: dev: build: context: . - target: DEV + target: dev volumes: - .:/app/ - ~/.gitconfig:/root/.gitconfig:ro @@ -13,6 +13,6 @@ services: docs: build: context: . - target: DOCS + target: docs volumes: - .:/app/ diff --git a/src/Dockerfile b/src/Dockerfile index b24c8db..4701600 100644 --- a/src/Dockerfile +++ b/src/Dockerfile @@ -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" \ @@ -7,22 +10,10 @@ LABEL \ "maintainer"="Andreas Augustin " # 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 \ diff --git a/src/sync_template.sh b/src/sync_template.sh index 34cb434..eb9ca60 100755 --- a/src/sync_template.sh +++ b/src/sync_template.sh @@ -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}"