Skip to content

Commit

Permalink
feat: failure trap
Browse files Browse the repository at this point in the history
  • Loading branch information
anton-yurchenko committed Feb 14, 2021
1 parent 9d1b336 commit 149ec5f
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
## [1.1.0](https://github.com/ReasonSoftware/action-github-deployment/releases/tag/v1.1.0) - 2021-02-14
### Added
- Failure trap which enable to use only one action for either success/failure status instead of two actions

## [1.0.0](https://github.com/ReasonSoftware/action-github-deployment/releases/tag/v1.0.0) - 2021-02-14
- First release
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ RUN CGO_ENABLED=0 go build -ldflags="-w -s" -o /opt/action-github-deployment

FROM scratch
LABEL "repository"="https://github.com/ReasonSoftware/action-github-deployment"
LABEL "version"="1.0.0"
LABEL "version"="1.1.0"
COPY --from=build /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/
COPY --from=build /etc/passwd /etc/passwd
COPY LICENSE.md /LICENSE.md
Expand Down
15 changes: 15 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,20 @@ Just to add an action to your CD flow
STATUS: success
```
You may also update the deployment to either `success` or `failure` in one step by providing an overall status as `FAIL` environmental variable:

```yaml
[...]
- name: Update Status
uses: docker://reasonsoftware/action-github-deployment:v1
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
DEPLOYMENT: ${{steps.deployment.outputs.id}}
STATUS: success
FAIL: "${{ failure() }}"
```

</details>

<details><summary>:information_source: Create a deployment with non-default status</summary>
Expand All @@ -65,6 +79,7 @@ Just to add an action to your CD flow
- `DEPLOYMENT` - existing deployment ID, required when updating status
- `STATUS` - status of a deployment, must be one of the following: `[ error, failure, inactive, in_progress, queued, pending, success ]`
- `ENVIRONMENT` - GitHub Environment, default `production`
- `FAIL`: failure trap which will tweak the status to be **failure** on value `"true"`. Used to reduce overal workflow code by configuring one action for both successful or failed deployments with `FAIL: "${{ failure() }}"`

## Notes

Expand Down
23 changes: 20 additions & 3 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// Version of an application
const Version string = "1.0.0"
const Version string = "1.1.0"

func init() {
log.SetReportCaller(false)
Expand Down Expand Up @@ -88,8 +88,25 @@ func main() {
if os.Getenv("STATUS") != "" {
log.Infof("updating status of a deployment %v in %v environment", id, env)

if err := app.UpdateStatus(cli.Repositories, id, os.Getenv("STATUS")); err != nil {
log.Fatal(errors.Wrap(err, "error updating deployment status"))
failed := false
if os.Getenv("FAIL") != "" {
var err error
failed, err = strconv.ParseBool(os.Getenv("FAIL"))
if err != nil {
log.Fatal(errors.Wrap(err, "error parsing env.var 'FAIL'"))
}
}

if failed {
log.Warnf("FAIL=%v, updating deployment status to 'failure'", failed)

if err := app.UpdateStatus(cli.Repositories, id, "failure"); err != nil {
log.Fatal(errors.Wrap(err, "error updating deployment status"))
}
} else {
if err := app.UpdateStatus(cli.Repositories, id, os.Getenv("STATUS")); err != nil {
log.Fatal(errors.Wrap(err, "error updating deployment status"))
}
}

log.Info("deployment status set to ", os.Getenv("STATUS"))
Expand Down

0 comments on commit 149ec5f

Please sign in to comment.