pre and post

This commit is contained in:
Zettat123 2023-04-10 11:59:19 +08:00
parent 94b7ef5e4a
commit 341b719495
3 changed files with 96 additions and 1 deletions

View file

@ -142,7 +142,7 @@ name: 'Test Go Action'
on: [push]
jobs:
use-go-action:
runs-on: host-docker-ubuntu20.04
runs-on: ubuntu-latest
steps:
- name: Setup Go
uses: actions/setup-go@v3
@ -162,3 +162,98 @@ jobs:
After pushing the updated workflow file, you will see the result:
![use-go-action-input-and-output](/demos/creating-go-actions/use-go-action-input-and-output.png)
## Pre and Post
In the `action.yml` file, we set the `runs.main` to `main.go` to specify the code that the action needs to run.
Except the `main`, You can also specify the `pre` and the `post` for an action.
The `pre` allows you to run some code before running the `main` and the `post` allows you to run some code at the end of the job.
If you need more information, please read GitHub's documentation about [`pre`](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runspre) and [`post`](https://docs.github.com/en/actions/creating-actions/metadata-syntax-for-github-actions#runspost).
Let's add the `pre` and the `post` to the action.
Firstly, you need to create a `pre` directory and add a `pre.go` file in it:
```go
package main
import "fmt"
func main() {
fmt.Println("Pre of Simple Go Action")
}
```
Then, create the `post` directory and `post.go` file in the similar way:
```go
package main
import "fmt"
func main() {
fmt.Println("Post of Simple Go Action")
}
```
And you'll also need to update the `action.yml` file:
```yml
name: 'Simple Go Action'
description: 'A simple Gitea action written in go'
inputs:
username:
description: 'The username to print'
required: true
outputs:
time:
description: 'The time when the action was called'
runs:
using: 'go'
main: 'main.go'
pre: "pre/pre.go"
post: "post/post.go"
```
Now the structure of the directory should be like:
```
├── action.yml
├── go.mod
├── main.go
├── post
│   └── post.go
└── pre
└── pre.go
```
Everything looks ok! But when you re-run the workflow, you may see an error: `"go": executable file not found in $PATH`.
The reason is that the code in `pre` will be executed before calling the `setup-go` action, and the Go runtime has not yet been set up.
In this case, you need to use an image with Go installed to run this workflow and you won't need to call the `setup-go` action since the image already has the Go runtime.
You can use `container.image` to specify the image (replace `<image-with-go>` with an image with go installed):
```yml
name: 'Test Go Action'
on: [push]
jobs:
use-go-action:
runs-on: host-docker-ubuntu20.04
container:
image: <image-with-go>
steps:
- name: Use Go Action
id: use-go-action
uses: <action-url>
with:
username: foo
- name: Print Output
run: echo 'output time is ${{ steps.use-go-action.outputs.time }}'
```
Then the workflow will work. You'll see the output of the `pre` in `Set up job`:
![go-action-pre](/demos/creating-go-actions/go-action-pre.png)
and the output of the `post` in `Complete job`:
![go-action-post](/demos/creating-go-actions/go-action-post.png)

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 1.4 KiB