Subsections of Action
Customize A Gitea Action
Introduction
In this guide, you’ll learn about the basic components needed to create and use a packaged composite action. To focus this guide on the components needed to package the action, the functionality of the action’s code is minimal. The action prints “Hello World” and then “Goodbye”, or if you provide a custom name, it prints “Hello [who-to-greet]” and then “Goodbye”. The action also maps a random number to the random-number output variable, and runs a script named goodbye.sh.
Once you complete this project, you should understand how to build your own composite action and test it in a workflow.
Warning
When creating workflows and actions, you should always consider whether your code might execute untrusted input from possible attackers. Certain contexts should be treated as untrusted input, as an attacker could insert their own malicious content. For more information, see Secure use reference.
Composite actions and reusable workflows
Composite actions allow you to collect a series of workflow job steps into a single action which you can then run as a single job step in multiple workflows. Reusable workflows provide another way of avoiding duplication, by allowing you to run a complete workflow from within other workflows. For more information, see Reusing workflow configurations.
Prerequisites
Note
This example explains how to create a composite action within a separate repository. However, it is possible to create a composite action within the same repository. For more information, see Creating a composite action.
Before you begin, you’ll create a repository on GitHub.
Create a new public repository on GitHub. You can choose any repository name, or use the following
hello-world-composite-actionexample. You can add these files after your project has been pushed to GitHub.Clone your repository to your computer.
From your terminal, change directories into your new repository.
cd hello-world-composite-action- In the hello-world-composite-action repository, create a new file called goodbye.sh with example code:
echo "echo Goodbye" > goodbye.sh- From your terminal, make goodbye.sh executable.
chmod +x goodbye.sh- From your terminal, check in your goodbye.sh file.
git add goodbye.sh
git commit -m "Add goodbye script"
git pushCreating an action metadata file
- In the hello-world-composite-action repository, create a new file called action.yml and add the following example code. For more information about this syntax, see Metadata syntax reference.
name: 'Hello World'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
using: "composite"
steps:
- name: Set Greeting
run: echo "Hello $INPUT_WHO_TO_GREET."
shell: bash
env:
INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}
- name: Random Number Generator
id: random-number-generator
run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
shell: bash
- name: Set GitHub Path
run: echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH
shell: bash
env:
GITHUB_ACTION_PATH: ${{ github.action_path }}
- name: Run goodbye.sh
run: goodbye.sh
shell: bashThis file defines the who-to-greet input, maps the random generated number to the random-number output variable, adds the action’s path to the runner system path (to locate the goodbye.sh script during execution), and runs the goodbye.sh script.
For more information about managing outputs, see Metadata syntax reference.
For more information about how to use github.action_path, see Contexts reference.
- From your terminal, check in your action.yml file.
git add action.yml
git commit -m "Add action"
git push- From your terminal, add a tag. This example uses a tag called v1. For more information, see About custom actions.
git tag -a -m "Description of this release" v1
git push --follow-tagsTesting out your action in a workflow
The following workflow code uses the completed hello world action that you made in Creating a composite action.
Copy the workflow code into a .github/workflows/main.yml file in another repository, replacing OWNER and SHA with the repository owner and the SHA of the commit you want to use, respectively. You can also replace the who-to-greet input with your name.
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v5
- id: foo
uses: OWNER/hello-world-composite-action@SHA
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}From your repository, click the Actions tab, and select the latest workflow run. The output should include: “Hello Mona the Octocat”, the result of the “Goodbye” script, and a random number.
Creating a composite action within the same repository
Create a new subfolder called
hello-world-composite-action, this can be placed in any subfolder within the repository. However, it is recommended that this be placed in the.github/actionssubfolder to make organization easier.In the hello-world-composite-action folder, do the same steps to create the goodbye.sh script
echo "echo Goodbye" > goodbye.shchmod +x goodbye.shgit add goodbye.sh
git commit -m "Add goodbye script"
git pushIn the hello-world-composite-action folder, create the action.yml file based on the steps in Creating a composite action.
When using the action, use the relative path to the folder where the composite action’s
action.ymlfile is located in the uses key. The below example assumes it is in the.github/actions/hello-world-composite-actionfolder.
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v5
- id: foo
uses: ./.github/actions/hello-world-composite-action
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}Customize A Github Action
Introduction
In this guide, you’ll learn about the basic components needed to create and use a packaged composite action. To focus this guide on the components needed to package the action, the functionality of the action’s code is minimal. The action prints “Hello World” and then “Goodbye”, or if you provide a custom name, it prints “Hello [who-to-greet]” and then “Goodbye”. The action also maps a random number to the random-number output variable, and runs a script named goodbye.sh.
Once you complete this project, you should understand how to build your own composite action and test it in a workflow.
Warning
When creating workflows and actions, you should always consider whether your code might execute untrusted input from possible attackers. Certain contexts should be treated as untrusted input, as an attacker could insert their own malicious content. For more information, see Secure use reference.
Composite actions and reusable workflows
Composite actions allow you to collect a series of workflow job steps into a single action which you can then run as a single job step in multiple workflows. Reusable workflows provide another way of avoiding duplication, by allowing you to run a complete workflow from within other workflows. For more information, see Reusing workflow configurations.
Prerequisites
Note
This example explains how to create a composite action within a separate repository. However, it is possible to create a composite action within the same repository. For more information, see Creating a composite action.
Before you begin, you’ll create a repository on GitHub.
Create a new public repository on GitHub. You can choose any repository name, or use the following
hello-world-composite-actionexample. You can add these files after your project has been pushed to GitHub.Clone your repository to your computer.
From your terminal, change directories into your new repository.
cd hello-world-composite-action- In the hello-world-composite-action repository, create a new file called goodbye.sh with example code:
echo "echo Goodbye" > goodbye.sh- From your terminal, make goodbye.sh executable.
chmod +x goodbye.sh- From your terminal, check in your goodbye.sh file.
git add goodbye.sh
git commit -m "Add goodbye script"
git pushCreating an action metadata file
- In the hello-world-composite-action repository, create a new file called action.yml and add the following example code. For more information about this syntax, see Metadata syntax reference.
name: 'Hello World'
description: 'Greet someone'
inputs:
who-to-greet: # id of input
description: 'Who to greet'
required: true
default: 'World'
outputs:
random-number:
description: "Random number"
value: ${{ steps.random-number-generator.outputs.random-number }}
runs:
using: "composite"
steps:
- name: Set Greeting
run: echo "Hello $INPUT_WHO_TO_GREET."
shell: bash
env:
INPUT_WHO_TO_GREET: ${{ inputs.who-to-greet }}
- name: Random Number Generator
id: random-number-generator
run: echo "random-number=$(echo $RANDOM)" >> $GITHUB_OUTPUT
shell: bash
- name: Set GitHub Path
run: echo "$GITHUB_ACTION_PATH" >> $GITHUB_PATH
shell: bash
env:
GITHUB_ACTION_PATH: ${{ github.action_path }}
- name: Run goodbye.sh
run: goodbye.sh
shell: bashThis file defines the who-to-greet input, maps the random generated number to the random-number output variable, adds the action’s path to the runner system path (to locate the goodbye.sh script during execution), and runs the goodbye.sh script.
For more information about managing outputs, see Metadata syntax reference.
For more information about how to use github.action_path, see Contexts reference.
- From your terminal, check in your action.yml file.
git add action.yml
git commit -m "Add action"
git push- From your terminal, add a tag. This example uses a tag called v1. For more information, see About custom actions.
git tag -a -m "Description of this release" v1
git push --follow-tagsTesting out your action in a workflow
The following workflow code uses the completed hello world action that you made in Creating a composite action.
Copy the workflow code into a .github/workflows/main.yml file in another repository, replacing OWNER and SHA with the repository owner and the SHA of the commit you want to use, respectively. You can also replace the who-to-greet input with your name.
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v5
- id: foo
uses: OWNER/hello-world-composite-action@SHA
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}From your repository, click the Actions tab, and select the latest workflow run. The output should include: “Hello Mona the Octocat”, the result of the “Goodbye” script, and a random number.
Creating a composite action within the same repository
Create a new subfolder called
hello-world-composite-action, this can be placed in any subfolder within the repository. However, it is recommended that this be placed in the.github/actionssubfolder to make organization easier.In the hello-world-composite-action folder, do the same steps to create the goodbye.sh script
echo "echo Goodbye" > goodbye.shchmod +x goodbye.shgit add goodbye.sh
git commit -m "Add goodbye script"
git pushIn the hello-world-composite-action folder, create the action.yml file based on the steps in Creating a composite action.
When using the action, use the relative path to the folder where the composite action’s
action.ymlfile is located in the uses key. The below example assumes it is in the.github/actions/hello-world-composite-actionfolder.
on: [push]
jobs:
hello_world_job:
runs-on: ubuntu-latest
name: A job to say hello
steps:
- uses: actions/checkout@v5
- id: foo
uses: ./.github/actions/hello-world-composite-action
with:
who-to-greet: 'Mona the Octocat'
- run: echo random-number "$RANDOM_NUMBER"
shell: bash
env:
RANDOM_NUMBER: ${{ steps.foo.outputs.random-number }}Gitea Variables
Preset Variables
| 变量名称示例 | 说明 / 用途 |
|---|---|
gitea.actor | 触发 workflow 的用户的用户名。(docs.gitea.com) |
gitea.event_name | 事件名称,比如 push、pull_request 等。(docs.gitea.com) |
gitea.ref | 被触发的 Git 引用(branch/tag/ref)名称。(docs.gitea.com) |
gitea.repository | 仓库标识,一般是 owner/name。(docs.gitea.com) |
gitea.workspace | 仓库被 checkout 到 runner 上的工作目录路径。(docs.gitea.com) |
Common Variables
| 变量名称示例 | 说明 / 用途 |
|---|---|
runner.os | Runner 所在的操作系统环境,比如 ubuntu-latest。(docs.gitea.com) |
job.status | 当前 job 的状态(例如 success 或 failure)。(docs.gitea.com) |
env.xxxx | 自定义配置变量,在用户/组织/仓库层定义,统一以大写形式引用。(docs.gitea.com) |
secrets.XXXX | 存放敏感信息的密钥,同样可以在用户/组织/仓库层定义。(docs.gitea.com) |
Sample
name: Gitea Actions Demo
run-name: ${{ gitea.actor }} is testing out Gitea Actions 🚀
on: [push]
env:
author: gitea_admin
jobs:
Explore-Gitea-Actions:
runs-on: ubuntu-latest
steps:
- run: echo "🎉 The job was automatically triggered by a ${{ gitea.event_name }} event."
- run: echo "🐧 This job is now running on a ${{ runner.os }} server hosted by Gitea!"
- run: echo "🔎 The name of your branch is ${{ gitea.ref }} and your repository is ${{ gitea.repository }}."
- name: Check out repository code
uses: actions/checkout@v4
- run: echo "💡 The ${{ gitea.repository }} repository has been cloned to the runner."
- run: echo "🖥️ The workflow is now ready to test your code on the runner."
- name: List files in the repository
run: |
ls ${{ gitea.workspace }}
- run: echo "🍏 This job's status is ${{ job.status }}."Result
🎉 The job was automatically triggered by a `push` event.
🐧 This job is now running on a `Linux` server hosted by Gitea!
🔎 The name of your branch is `refs/heads/main` and your repository is `gitea_admin/data-warehouse`.
💡 The `gitea_admin/data-warehouse` repository has been cloned to the runner.
🖥️ The workflow is now ready to test your code on the runner.
Dockerfile README.md environments pom.xml src templates
🍏 This job's status is `success`.Github Variables
Context Variables
| 变量名称示例 | 说明 / 用途 |
|---|---|
github.actor | 触发 workflow 的用户的用户名。([docs.gitea.com][1]) |
github.event_name | 事件名称,比如 push、pull_request 等。([docs.gitea.com][1]) |
github.ref | 被触发的 Git 引用(branch/tag/ref)名称。([docs.gitea.com][1]) |
github.repository | 仓库标识,一般是 owner/name。([docs.gitea.com][1]) |
github.workspace | 仓库被 checkout 到 runner 上的工作目录路径。([docs.gitea.com][1]) |
env.xxxx | 在workflow中定义的变量,比如 ${{ env.xxxx }} |
secrets.XXXX | 通过 Settings -> Actions -> Secrets and variables 创建的密钥。 |
Template
Subsections of Template
Apply And Sync Argocd APP
name: apply-and-sync-app
run-name: ${{ gitea.actor }} is going to sync an sample argocd app 🚀
on: [push]
jobs:
sync-argocd-app:
runs-on: ubuntu-latest
steps:
- name: Sync App
uses: AaronYang0628/apply-and-sync-argocd@v1.0.6
with:
argocd-server: '192.168.100.125:30443'
argocd-token: ${{ secrets.ARGOCD_TOKEN }}
application-yaml-path: "environments/ops/argocd/operator.app.yaml"Publish Chart 2 Harbor
name: publish-chart-to-harbor-registry
run-name: ${{ gitea.actor }} is testing out Gitea Push Chart 🚀
on: [push]
env:
REGISTRY: harbor.zhejianglab.com
USER: byang628@zhejianglab.com
REPOSITORY_NAMESPACE: ay-dev
CHART_NAME: data-warehouse
jobs:
build-and-push-charts:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
strategy:
matrix:
include:
- chart_path: "environments/helm/metadata-environment"
steps:
- name: Checkout Repository
uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Log in to Harbor
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
with:
registry: "${{ env.REGISTRY }}"
username: "${{ env.USER }}"
password: "${{ secrets.ZJ_HARBOR_TOKEN }}"
- name: Helm Publish Action
uses: AaronYang0628/push-helm-chart-to-oci@v0.0.3
with:
working-dir: ${{ matrix.chart_path }}
oci-repository: oci://${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}
username: ${{ env.USER }}
password: ${{ secrets.ZJ_HARBOR_TOKEN }}Publish Image 2 Dockerhub
name: publish-image-to-ghcr
run-name: ${{ gitea.actor }} is testing out Gitea Push Image 🚀
on: [push]
env:
REGISTRY: ghcr.io
USER: aaronyang0628
REPOSITORY_NAMESPACE: aaronyang0628
jobs:
build-and-push-images:
strategy:
matrix:
include:
- name_suffix: "aria-ng"
container_path: "application/aria2/container/aria-ng"
dockerfile_path: "application/aria2/container/aria-ng/Dockerfile"
- name_suffix: "aria2"
container_path: "application/aria2/container/aria2"
dockerfile_path: "application/aria2/container/aria2/Dockerfile"
runs-on: ubuntu-latest
steps:
- name: checkout-repository
uses: actions/checkout@v4
- name: log in to the container registry
uses: docker/login-action@v3
with:
registry: "${{ env.REGISTRY }}"
username: "${{ env.USER }}"
password: "${{ secrets.GIT_REGISTRY_PWD }}"
- name: build and push container image
uses: docker/build-push-action@v6
with:
context: "${{ matrix.container_path }}"
file: "${{ matrix.dockerfile_path }}"
push: true
tags: |
${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ github.repository }}-${{ matrix.name_suffix }}:${{ inputs.tag || 'latest' }}
${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ github.repository }}-${{ matrix.name_suffix }}:${{ github.ref_name }}
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}Publish Image 2 Ghcr
name: publish-image-to-ghcr
run-name: ${{ gitea.actor }} is testing out Gitea Push Image 🚀
on: [push]
env:
REGISTRY: ghcr.io
USER: aaronyang0628
REPOSITORY_NAMESPACE: aaronyang0628
jobs:
build-and-push-images:
strategy:
matrix:
include:
- name_suffix: "aria-ng"
container_path: "application/aria2/container/aria-ng"
dockerfile_path: "application/aria2/container/aria-ng/Dockerfile"
- name_suffix: "aria2"
container_path: "application/aria2/container/aria2"
dockerfile_path: "application/aria2/container/aria2/Dockerfile"
runs-on: ubuntu-latest
steps:
- name: checkout-repository
uses: actions/checkout@v4
- name: log in to the container registry
uses: docker/login-action@v3
with:
registry: "${{ env.REGISTRY }}"
username: "${{ env.USER }}"
password: "${{ secrets.GIT_REGISTRY_PWD }}"
- name: build and push container image
uses: docker/build-push-action@v6
with:
context: "${{ matrix.container_path }}"
file: "${{ matrix.dockerfile_path }}"
push: true
tags: |
${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ github.repository }}-${{ matrix.name_suffix }}:${{ inputs.tag || 'latest' }}
${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ github.repository }}-${{ matrix.name_suffix }}:${{ github.ref_name }}
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}Publish Image 2 Harbor
name: publish-image-to-harbor-registry
run-name: ${{ gitea.actor }} is testing out Gitea Push Image 🚀
on: [push]
env:
REGISTRY: harbor.zhejianglab.com
USER: byang628@zhejianglab.com
REPOSITORY_NAMESPACE: ay-dev
IMAGE_NAME: metadata-crd-operator
jobs:
build-and-push-images:
runs-on: ubuntu-latest
permissions:
packages: write
contents: read
strategy:
matrix:
include:
- name_suffix: "dev"
container_path: "."
dockerfile_path: "./Dockerfile"
steps:
- name: Checkout Repository
uses: actions/checkout@v4
- name: Log in to Harbor
uses: docker/login-action@f4ef78c080cd8ba55a85445d5b36e214a81df20a
with:
registry: "${{ env.REGISTRY }}"
username: "${{ env.USER }}"
password: "${{ secrets.ZJ_HARBOR_TOKEN }}"
- name: Extract Current Date
id: extract-date
run: |
echo "current-date=$(date +'%Y%m%d')" >> $GITHUB_OUTPUT
echo will push image: ${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ env.IMAGE_NAME }}-${{ matrix.name_suffix }}:v${{ steps.extract-date.outputs.current-date }}
- name: Build And Push Container Image
uses: docker/build-push-action@v6
with:
context: "${{ matrix.container_path }}"
file: "${{ matrix.dockerfile_path }}"
push: true
tags: |
${{ env.REGISTRY }}/${{ env.REPOSITORY_NAMESPACE }}/${{ env.IMAGE_NAME }}-${{ matrix.name_suffix }}:v${{ steps.extract-date.outputs.current-date }}
labels: |
org.opencontainers.image.source=${{ github.server_url }}/${{ github.repository }}