Skip to content

Instantly share code, notes, and snippets.

@DominicSherman
Last active May 20, 2021
Embed
What would you like to do?
Blog Post Files
const createTag = async () => {
const response = await fetch(
// these environment variables are set for us in the github action
`${process.env.GITHUB_API_URL}/repos/${process.env.GITHUB_REPOSITORY}/tags`,
{
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${process.env.GITHUB_AUTH_TOKEN}`,
},
}
);
const data = await response.json();
const mostRecentTag = data[0].name;
// split tag on '-' to separate version from specifying number
const [tagVersion, tagVersionNumber] = mostRecentTag.split('-');
// remove v from our version so we have 1.1.0 instead of v1.1.0
const cleanTag = tagVersion.replace('v', '');
// get the new tag with the version change type
const newTag = getNewTag(cleanTag);
// if the tag didn't change, increment our specifying number
// otherwise restart our specifying numbers
const updatedTagVersionNumber =
cleanTag === newTag ? Number(tagVersionNumber) + 1 : 1;
// put it all back together into our tag name
const tagName = `v${newTag}-${updatedTagVersionNumber}`;
// create tag off of the branch that we created with our version change
await fetch(
`${process.env.GITHUB_API_URL}/repos/${process.env.GITHUB_REPOSITORY}/releases`,
{
body: JSON.stringify({
owner: 'YOUR_REPO_OWNER',
repo: 'YOUR_REPO',
tag_name: tagName,
target_commitish: process.env.BRANCH_TO_TAG,
name: `${tagName}`,
body: `Released at ${new Date(Date.now()).toISOString()}`,
}),
method: 'POST',
headers: {
Accept: 'application/vnd.github.v3+json',
Authorization: `Bearer ${process.env.GITHUB_AUTH_TOKEN}`,
},
}
);
};
const getNewTag = tag => {
// 1.1.0 becomes major=1, minor=1, patch=0
const [major, minor, patch] = tag.split('.');
if (process.env.VERSION_CHANGE_TYPE === 'major') {
return `${Number(major) + 1}.0.0`;
} else if (process.env.VERSION_CHANGE_TYPE === 'minor') {
return `${major}.${Number(minor) + 1}.0`;
} else if (process.env.VERSION_CHANGE_TYPE === 'patch') {
return `${major}.${minor}.${Number(patch) + 1}`;
}
// if we didn't specify a version change type, don't change it
return tag;
};
name: Tag
on:
workflow_dispatch:
inputs:
versionChangeType:
description: 'Type of version bump (major, minor, patch, none)'
required: true
default: 'none'
schedule:
# Scheduled for 12PM UTC every Tuesday and Friday (8AM EST)
- cron: 0 12 * * 2,5
jobs:
create-new-tag:
name: Create new tag with version bump
runs-on: macos-latest
env:
GITHUB_AUTH_TOKEN: ${{ secrets.CI_GITHUB_PERSONAL_ACCESS_TOKEN }}
BASE_BRANCH: 'main'
steps:
- name: Set environment variables
env:
DEFAULT_VERSION_CHANGE_TYPE: none
VERSION_CHANGE_BRANCH_TO_TAG: 'release/${{ github.event.inputs.versionChangeType }}-version-${{ github.run_id }}'
run: |
echo "VERSION_CHANGE_TYPE=${{ github.event.inputs.versionChangeType || env.DEFAULT_VERSION_CHANGE_TYPE}}" >> $GITHUB_ENV
if [$VERSION_CHANGE_TYPE != 'none']
then
echo "VERSION_BUMP=true"
echo "BRANCH_TO_TAG=${{ env.VERSION_CHANGE_BRANCH_TO_TAG }}" >> $GITHUB_ENV
else
echo "VERSION_BUMP=false"
echo "BRANCH_TO_TAG=develop" >> $GITHUB_ENV
fi
- uses: actions/checkout@v2
with:
ref: ${{ env.BASE_BRANCH }}
- name: Bump iOS version
if: ${{ env.VERSION_BUMP == 'true'}}
run: cd ios && bundle install && bundle exec fastlane version_bump
- name: Create Pull Request
if: ${{ env.VERSION_BUMP == 'true'}}
uses: peter-evans/create-pull-request@v3
with:
token: ${{ secrets.CI_GITHUB_PERSONAL_ACCESS_TOKEN }}
branch: '${{ env.BRANCH_TO_TAG }}'
title: 'release: ${{ github.event.inputs.versionChangeType }} version release'
commit-message: 'release: ${{ github.event.inputs.versionChangeType }} version release'
author: ${{ github.actor }} <${{ github.actor }}@users.noreply.github.com>
labels: |
release
automated pr
delete-branch: true
- name: Create tag
run: cd ci-scripts && yarn && yarn createTag
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment