Skip to content

Commit

Permalink
Modify CI to allow for main default branch
Browse files Browse the repository at this point in the history
Goal is to make CI scripts work with a default branch of main or
master.

notes:

`git symbolic-ref refs/remotes/origin/HEAD` worked locally but not
on GitHub actions.

references:

- actions/checkout#283 (comment)
- https://github.com/thoughtbot/dotfiles/blob/2a59c1890f81bffc1db79cae4482ce2b706b0f79/bin/git-up
- https://github.com/actions/checkout/pull/284/files
- jhauberg/gitdoctor#3 (comment)
  • Loading branch information
dhimmel committed Dec 10, 2020
1 parent 97b2948 commit 667d170
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 9 deletions.
3 changes: 2 additions & 1 deletion .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@ skip_branch_with_pr: true
# to only build commits from pull requests
branches:
only:
- main
- master

# Only run AppVeyor on commits that modify at least one of the following files
# Delete these lines to run AppVeyor on all master branch commits
# Delete these lines to run AppVeyor on all main/master branch commits
only_commits:
files:
- .appveyor.yml
Expand Down
20 changes: 13 additions & 7 deletions .github/workflows/manubot.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ name: Manubot
on:
push:
branches:
- main
- master
pull_request:
branches:
- main
- master
jobs:
manubot:
Expand All @@ -14,23 +16,27 @@ jobs:
GITHUB_PULL_REQUEST_SHA: ${{ github.event.pull_request.head.sha }}
SPELLCHECK: true
steps:
- name: Set Environment Variables
run: |
TRIGGERING_SHA=${GITHUB_PULL_REQUEST_SHA:-$GITHUB_SHA}
echo "TRIGGERING_SHA_7=${TRIGGERING_SHA::7}" >> $GITHUB_ENV
echo "TRIGGERING_SHA: $TRIGGERING_SHA"
- name: Checkout Repository
uses: actions/checkout@v2
with:
# fetch entire commit history to support get_rootstock_commit
fetch-depth: 0
- name: Set Environment Variables
run: |
TRIGGERING_SHA=${GITHUB_PULL_REQUEST_SHA:-$GITHUB_SHA}
echo "TRIGGERING_SHA_7=${TRIGGERING_SHA::7}" >> $GITHUB_ENV
echo "TRIGGERING_SHA: $TRIGGERING_SHA"
DEFAULT_BRANCH=$(git remote show origin | grep --perl-regexp --only-matching "(?<=HEAD branch: ).+")
echo "DEFAULT_BRANCH=${DEFAULT_BRANCH}" >> $GITHUB_ENV
echo "DEFAULT_BRANCH_REF=refs/heads/$DEFAULT_BRANCH" >> $GITHUB_ENV
echo "DEFAULT_BRANCH=$DEFAULT_BRANCH"
- name: Cache
uses: actions/cache@v1
with:
path: ci/cache
key: ci-cache-${{ github.ref }}
restore-keys: |
ci-cache-refs/heads/master
ci-cache-${{ env.DEFAULT_BRANCH_REF }}
- name: Install Environment
uses: conda-incubator/setup-miniconda@v2
with:
Expand All @@ -53,7 +59,7 @@ jobs:
name: manuscript-${{ github.run_id }}-${{ env.TRIGGERING_SHA_7 }}
path: output
- name: Deploy Manuscript
if: github.ref == 'refs/heads/master' && github.event_name == 'push' && !github.event.repository.fork
if: github.ref == env.DEFAULT_BRANCH_REF && github.event_name == 'push' && !github.event.repository.fork
env:
MANUBOT_ACCESS_TOKEN: ${{ secrets.GITHUB_TOKEN }}
MANUBOT_SSH_PRIVATE_KEY: ${{ secrets.MANUBOT_SSH_PRIVATE_KEY }}
Expand Down
2 changes: 2 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ services:
- docker
branches:
only:
- main
- master
env:
- SPELLCHECK=true
Expand All @@ -20,5 +21,6 @@ deploy:
script: bash ci/deploy.sh
skip_cleanup: true
on:
# branch kept as master since travis deployment is legacy.
branch: master
condition: $TRAVIS_EVENT_TYPE = "push" && (-v MANUBOT_SSH_PRIVATE_KEY || "${!encrypted_*}" != "")
3 changes: 2 additions & 1 deletion ci/deploy.sh
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ REPO_SLUG=${TRAVIS_REPO_SLUG:-$GITHUB_REPOSITORY}
COMMIT=${TRAVIS_COMMIT:-$GITHUB_SHA}
CI_BUILD_WEB_URL=${CI_BUILD_WEB_URL:-$TRAVIS_BUILD_WEB_URL}
CI_JOB_WEB_URL=${CI_JOB_WEB_URL:-$TRAVIS_JOB_WEB_URL}
BRANCH=${TRAVIS_BRANCH:-master}
BRANCH=${TRAVIS_BRANCH:-$DEFAULT_BRANCH}
BRANCH=${BRANCH:-$main}

# Add commit hash to the README
OWNER_NAME="$(dirname "$REPO_SLUG")"
Expand Down

3 comments on commit 667d170

@rainabba
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I left this comment, did another Google search and found your solution using grep --perl-regexp.

Thank you for open-sourcing this project!

@rainabba
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A more readable approach I found on that same page was: git remote show origin | awk '/HEAD branch/ {print $NF}' and no color info comes over (which might be preferable I think.)

@dhimmel
Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rainabba nice! For our use, we could probably update to using the github.event.repository.default_branch actions context:

Please sign in to comment.