mirror of
https://github.com/ButlerLogic/action-autotag.git
synced 2024-11-10 02:11:08 +07:00
Merge pull request #13 from ButlerLogic/master
Refactored workspace directory
This commit is contained in:
commit
935fa6dc20
23
README.md
23
README.md
@ -2,23 +2,30 @@
|
||||
|
||||
This action will read a `package.json` file and compare the `version` attribute to the project's known tags. If a corresponding tag does not exist, it will be created.
|
||||
|
||||
This tag works well in combination with:
|
||||
|
||||
- [actions/create-release](https://github.com/actions/create-release) (Auto-release)
|
||||
- [author/action-publish](https://github.com/author/action-publish) (Auto-publish JavaScript/Node modules)
|
||||
- [author/action-rollback](https://github.com/author/action-rollback) (Auto-rollback releases on failures)
|
||||
- [author/template-cross-runtime](https://github.com/author/template-cross-runtime) (a cross-runtime JavaScript repo template)
|
||||
|
||||
## Usage
|
||||
|
||||
The following is an example `.github/main.workflow` that will execute when a `push` to the `master` branch occurs.
|
||||
The following is an example `.github/workflows/main.yml` that will execute when a `push` to the `master` branch occurs.
|
||||
|
||||
```yaml
|
||||
name: My Workflow
|
||||
name: Create Tag
|
||||
|
||||
on:
|
||||
push:
|
||||
branches:
|
||||
- master
|
||||
- master
|
||||
|
||||
jobs:
|
||||
build:
|
||||
runs-on: ubuntu-latest
|
||||
steps:
|
||||
- uses: actions/checkout@master
|
||||
- uses: actions/checkout@v2
|
||||
- uses: butlerlogic/action-autotag@stable
|
||||
with:
|
||||
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
|
||||
@ -29,8 +36,8 @@ To make this work, the workflow must have the checkout action _before_ the autot
|
||||
This **order** is important!
|
||||
|
||||
```yaml
|
||||
- uses: actions/checkout@master
|
||||
- uses: butlerlogic/action-autotag@1.0.0
|
||||
- uses: actions/checkout@v2
|
||||
- uses: butlerlogic/action-autotag@stable
|
||||
```
|
||||
|
||||
> If the repository is not checked out first, the autotagger cannot find the package.json file.
|
||||
@ -40,7 +47,7 @@ This **order** is important!
|
||||
The `GITHUB_TOKEN` must be passed in. Without this, it is not possible to create a new tag. Make sure the autotag action looks like the following example:
|
||||
|
||||
```yaml
|
||||
- uses: butlerlogic/action-autotag@1.0.0
|
||||
- uses: butlerlogic/action-autotag@stable
|
||||
with:
|
||||
GITHUB_TOKEN: "${{ secrets.GITHUB_TOKEN }}"
|
||||
```
|
||||
@ -64,7 +71,7 @@ There are several options to customize how the tag is created.
|
||||
|
||||
1. `tag_prefix`
|
||||
|
||||
By default, `package.json` uses [semantic versioning](https://semver.org/), such as `1.0.0`. A prefix can be used to add text before the tag name. For example, if `tag_prefx` is set to `v`, then the tag would be labeled as `v1.0.0`.
|
||||
By default, `package.json` uses [semantic versioning](https://semver.org/), such as `1.0.0`. A prefix can be used to add text before the tag name. For example, if `tag_prefix` is set to `v`, then the tag would be labeled as `v1.0.0`.
|
||||
|
||||
```yaml
|
||||
- uses: butlerlogic/action-autotag@1.0.0
|
||||
|
93
lib/main.js
93
lib/main.js
@ -1,13 +1,11 @@
|
||||
const github = require('@actions/github')
|
||||
const core = require('@actions/core')
|
||||
const { GitHub, context } = require('@actions/github')
|
||||
const fs = require('fs')
|
||||
const path = require('path')
|
||||
const os = require('os')
|
||||
|
||||
async function run() {
|
||||
try {
|
||||
const { repo } = github.context
|
||||
|
||||
core.debug(
|
||||
` Available environment variables:\n -> ${Object.keys(process.env)
|
||||
.map(i => i + ' :: ' + process.env[i])
|
||||
@ -23,6 +21,13 @@ async function run() {
|
||||
|
||||
core.debug(` Working Directory: ${process.env.GITHUB_WORKSPACE}:\n${dir}`)
|
||||
|
||||
if (!process.env.hasOwnProperty('GITHUB_TOKEN')) {
|
||||
if (!process.env.hasOwnProperty('INPUT_GITHUB_TOKEN')) {
|
||||
core.setFailed('Invalid or missing GITHUB_TOKEN.')
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
const pkg_root = core.getInput('package_root', { required: false })
|
||||
const pkgfile = path.join(process.env.GITHUB_WORKSPACE, pkg_root, 'package.json')
|
||||
if (!fs.existsSync(pkgfile)) {
|
||||
@ -34,47 +39,22 @@ async function run() {
|
||||
core.setOutput('version', pkg.version)
|
||||
core.debug(` Detected version ${pkg.version}`)
|
||||
|
||||
// core.debug(` Available environment variables:\n -> ${Object.keys(process.env).map(i => i + ' :: ' + process.env[i]).join('\n -> ')}`)
|
||||
// Get authenticated GitHub client (Ocktokit): https://github.com/actions/toolkit/tree/master/packages/github#usage
|
||||
const github = new GitHub(process.env.GITHUB_TOKEN || process.env.INPUT_GITHUB_TOKEN)
|
||||
|
||||
// let version = ""
|
||||
// Get owner and repo from context of payload that triggered the action
|
||||
const { owner, repo } = context.repo
|
||||
|
||||
// if (!process.env.hasOwnProperty('INPUT_VERSION') || process.env.INPUT_VERSION.trim().length === 0) {
|
||||
// let dir = fs.readdirSync(path.resolve(process.env.GITHUB_WORKSPACE), { withFileTypes: true }).map(entry => {
|
||||
// return `${entry.isDirectory() ? '> ' : ' - '}${entry.name}`
|
||||
// }).join('\n')
|
||||
|
||||
// core.debug(` Working Directory: ${process.env.GITHUB_WORKSPACE}:\n${dir}`)
|
||||
|
||||
// const pkg_root = core.getInput('package_root', { required: false })
|
||||
// let pkgfile = path.join(process.env.GITHUB_WORKSPACE, pkg_root, 'package.json')
|
||||
|
||||
// if (!fs.existsSync(pkgfile)) {
|
||||
// core.setFailed('package.json does not exist.')
|
||||
// return
|
||||
// }
|
||||
|
||||
// let pkg = require(pkgfile)
|
||||
// version = pkg.version
|
||||
// } else {
|
||||
// version = process.env.INPUT_VERSION.trim()
|
||||
// }
|
||||
|
||||
// core.setOutput('version', version)
|
||||
// core.debug(` Detected version ${version}`)
|
||||
|
||||
|
||||
if (!process.env.hasOwnProperty('INPUT_GITHUB_TOKEN') || process.env.INPUT_GITHUB_TOKEN.trim().length === 0) {
|
||||
core.setFailed('Invalid or missing GITHUB_TOKEN.')
|
||||
return
|
||||
}
|
||||
|
||||
// Check for existing tag
|
||||
const git = new github.GitHub(process.env.INPUT_GITHUB_TOKEN)
|
||||
// // Check for existing tag
|
||||
// const git = new github.GitHub(process.env.INPUT_GITHUB_TOKEN || process.env.GITHUB_TOKEN)
|
||||
// const owner = process.env.GITHUB_REPOSITORY.split('/').shift()
|
||||
// const repo = process.env.GITHUB_REPOSITORY.split('/').pop()
|
||||
|
||||
let tags
|
||||
try {
|
||||
tags = await git.repos.listTags({
|
||||
...repo,
|
||||
tags = await github.repos.listTags({
|
||||
owner,
|
||||
repo,
|
||||
per_page: 100,
|
||||
})
|
||||
} catch (e) {
|
||||
@ -83,16 +63,17 @@ async function run() {
|
||||
}
|
||||
}
|
||||
|
||||
const tagPrefix = core.getInput('tag_prefix', { required: false })
|
||||
const tagSuffix = core.getInput('tag_suffix', { required: false })
|
||||
|
||||
const getTagName = version => {
|
||||
const tagPrefix = core.getInput('tag_prefix', { required: false })
|
||||
const tagSuffix = core.getInput('tag_suffix', { required: false })
|
||||
return `${tagPrefix}${version}${tagSuffix}`
|
||||
}
|
||||
|
||||
// Check for existance of tag and abort (short circuit) if it already exists.
|
||||
for (let tag of tags.data) {
|
||||
if (tag.name === getTagName(pkg.version)) {
|
||||
console.log(`"${tag.name.trim()}" tag already exists.` + os.EOL)
|
||||
core.warning(`"${tag.name.trim()}" tag already exists.` + os.EOL)
|
||||
core.setOutput('tagname', '')
|
||||
return
|
||||
}
|
||||
@ -106,8 +87,9 @@ async function run() {
|
||||
try {
|
||||
latestTag = tags.data.shift()
|
||||
|
||||
let changelog = await git.repos.compareCommits({
|
||||
...repo,
|
||||
let changelog = await github.repos.compareCommits({
|
||||
owner,
|
||||
repo,
|
||||
base: latestTag.name,
|
||||
head: 'master',
|
||||
})
|
||||
@ -125,7 +107,7 @@ async function run() {
|
||||
)
|
||||
.join('\n')
|
||||
} catch (e) {
|
||||
console.log('Failed to generate changelog from commits: ' + e.message + os.EOL)
|
||||
core.warning('Failed to generate changelog from commits: ' + e.message + os.EOL)
|
||||
tagMsg = tagName
|
||||
}
|
||||
}
|
||||
@ -134,15 +116,16 @@ async function run() {
|
||||
try {
|
||||
tagMsg = tagMsg.trim().length > 0 ? tagMsg : `Version ${pkg.version}`
|
||||
|
||||
newTag = await git.git.createTag({
|
||||
...repo,
|
||||
newTag = await github.git.createTag({
|
||||
owner,
|
||||
repo,
|
||||
tag: tagName,
|
||||
message: tagMsg,
|
||||
object: process.env.GITHUB_SHA,
|
||||
type: 'commit',
|
||||
type: 'commit'
|
||||
})
|
||||
|
||||
console.log(`Created new tag: ${newTag.data.tag}` + os.EOL)
|
||||
core.warning(`Created new tag: ${newTag.data.tag}`)
|
||||
} catch (e) {
|
||||
core.setFailed(e.message)
|
||||
return
|
||||
@ -150,16 +133,18 @@ async function run() {
|
||||
|
||||
let newReference
|
||||
try {
|
||||
newReference = await git.git.createRef({
|
||||
...repo,
|
||||
newReference = await github.git.createRef({
|
||||
owner,
|
||||
repo,
|
||||
ref: `refs/tags/${newTag.data.tag}`,
|
||||
sha: newTag.data.sha,
|
||||
})
|
||||
|
||||
console.log(`Reference ${newReference.data.ref} available at ${newReference.data.url}` + os.EOL)
|
||||
core.warning(`Reference ${newReference.data.ref} available at ${newReference.data.url}` + os.EOL)
|
||||
} catch (e) {
|
||||
core.warning({
|
||||
...repo,
|
||||
owner,
|
||||
repo,
|
||||
ref: `refs/tags/${newTag.data.tag}`,
|
||||
sha: newTag.data.sha,
|
||||
})
|
||||
@ -174,6 +159,7 @@ async function run() {
|
||||
core.setOutput('tagsha', newTag.data.sha)
|
||||
core.setOutput('taguri', newReference.data.url)
|
||||
core.setOutput('tagmessage', tagMsg.trim())
|
||||
core.setOutput('tagref', newReference.data.ref)
|
||||
}
|
||||
} catch (error) {
|
||||
core.warning(error.message)
|
||||
@ -181,6 +167,7 @@ async function run() {
|
||||
core.setOutput('tagsha', '')
|
||||
core.setOutput('taguri', '')
|
||||
core.setOutput('tagmessage', '')
|
||||
core.setOutput('tagref', '')
|
||||
}
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user