To create a workflow that creates a git tag using TypeScript, we need to set up two GitHub Actions: one that defines the reusable action for creating a git tag and another that calls this action from a different repository. Let's break down the necessary steps and provide a clear, structured workflow.
This action will be defined in a separate repository.
- Create the repository (e.g.,
git-tag-action
). - Define the action in a file named
action.yml
.
name: 'Create Git Tag'
description: 'Action to create a git tag'
inputs:
tag_name:
description: 'Name of the tag to create'
required: true
override:
description: 'Whether to override the tag if it exists'
required: false
default: 'false'
outputs:
tag:
description: 'The created tag'
runs:
using: 'node16'
main: 'dist/index.js'
import * as core from '@actions/core';
import * as github from '@actions/github';
async function run() {
try {
const token = core.getInput('token');
const tagName = core.getInput('tag_name');
const override = core.getInput('override') === 'true';
const octokit = github.getOctokit(token);
const { owner, repo } = github.context.repo;
const ref = `tags/${tagName}`;
try {
await octokit.rest.git.getRef({
owner,
repo,
ref,
});
if (override) {
core.info(`Tag '${tagName}' exists. Deleting...`);
await octokit.rest.git.deleteRef({
owner,
repo,
ref,
});
} else {
core.setFailed(`Tag '${tagName}' already exists.`);
return;
}
} catch (error) {
core.info(`Tag '${tagName}' does not exist. Creating...`);
}
const sha = github.context.sha;
await octokit.rest.git.createRef({
owner,
repo,
ref: `refs/${ref}`,
sha,
});
core.info(`Tag '${tagName}' created from '${sha}'.`);
core.setOutput('tag', tagName);
} catch (error) {
core.setFailed(error.message);
}
}
run();
{
"name": "create-git-tag",
"version": "1.0.0",
"main": "dist/index.js",
"scripts": {
"build": "tsc"
},
"dependencies": {
"@actions/core": "^1.6.0",
"@actions/github": "^5.0.0"
},
"devDependencies": {
"typescript": "^4.5.2"
}
}
This will be your main repository where you want to call the reusable action to create a git tag.
- Define the workflow file in
.github/workflows/create-tag.yml
.
name: Create Tag
on:
push:
branches:
- main
workflow_dispatch:
jobs:
create_tag:
runs-on: ubuntu-latest
steps:
- name: Checkout repository
uses: actions/checkout@v2
- name: Create git tag
uses: <owner>/<git-tag-action-repo>@main
with:
token: ${{ secrets.GITHUB_TOKEN }}
tag_name: 'v1.0.0'
override: 'true'
-
Reusable Action (
git-tag-action
repository):action.yml
: Defines the action's metadata.src/index.ts
: Contains the logic to create or override a git tag using the GitHub API.package.json
: Manages dependencies and scripts.
-
Calling Repository:
create-tag.yml
: The workflow triggers on push to the main branch or via manual dispatch. It uses the reusable action to create a git tag.
Ensure you replace <owner>/<git-tag-action-repo>
with your actual GitHub username and repository name for the reusable action.
- Commit and push the code to both repositories.
- Test the setup by pushing changes to the main branch of the calling repository or triggering the workflow manually.
This setup should help you create a git tag using a TypeScript GitHub Action and call this action from another repository.