Skip to content

Commit 041ad14

Browse files
authored
Merge pull request #1 from PacktPublishing/init-gh-action
add gh-action code
2 parents d2a37d4 + d07d333 commit 041ad14

20 files changed

+692
-1
lines changed

.github/release.yml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
changelog:
2+
exclude:
3+
labels:
4+
- ignore-for-release
5+
categories:
6+
- title: Breaking Changes 🛠
7+
labels:
8+
- breaking-change
9+
- title: New Features 🎉
10+
labels:
11+
- enhancement
12+
- title: Bug Fixes 🐛
13+
labels:
14+
- bug-fix
15+
- title: Other Changes
16+
labels:
17+
- "*"
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
name: Release new tweeter version
2+
on:
3+
release:
4+
types: [released]
5+
workflow_dispatch:
6+
inputs:
7+
TAG_NAME:
8+
description: 'Tag name that the major tag will point to'
9+
required: true
10+
11+
permissions:
12+
contents: write
13+
14+
env:
15+
TAG_NAME: ${{ github.event.inputs.TAG_NAME || github.event.release.tag_name }}
16+
17+
jobs:
18+
update_tag:
19+
name: Update the major tag to include the ${{ github.event.inputs.TAG_NAME || github.event.release.tag_name }} changes
20+
runs-on: ubuntu-latest
21+
steps:
22+
- name: Update the ${{ env.TAG_NAME }} tag
23+
uses: actions/[email protected]
24+
with:
25+
source-tag: ${{ env.TAG_NAME }}

.github/workflows/first.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
name: first-workflow
2+
on: workflow_dispatch
3+
jobs:
4+
echo:
5+
runs-on: ubuntu-latest
6+
steps:
7+
- name: echo step
8+
run: echo 'Hello World!'
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: release image
2+
on:
3+
push:
4+
tags:
5+
- 'image-v*' # push events for tags matching image-v for version (image-v1.0, etc)
6+
permissions:
7+
contents: read
8+
packages: write
9+
jobs:
10+
image:
11+
runs-on: ubuntu-latest
12+
steps:
13+
- uses: actions/checkout@v2
14+
- name: set env
15+
run: echo "RELEASE_VERSION=${GITHUB_REF:17}" >> $GITHUB_ENV # refs/tags/image-v1.0.0 substring starting at 1.0.0
16+
- name: setup buildx
17+
uses: docker/setup-buildx-action@v1
18+
- name: login to GitHub container registry
19+
uses: docker/login-action@v1
20+
with:
21+
registry: ghcr.io
22+
username: ${{ github.repository_owner }}
23+
password: ${{ secrets.GITHUB_TOKEN }}
24+
- name: build and push
25+
uses: docker/build-push-action@v2
26+
with:
27+
push: true
28+
tags: |
29+
ghcr.io/packtpublishing/tweeter:${{ env.RELEASE_VERSION }}
30+
ghcr.io/packtpublishing/tweeter:latest
Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
1+
name: tweeter-automation
2+
on:
3+
push:
4+
tags:
5+
- 'v[0-9]+.[0-9]+.*'
6+
branches:
7+
- main
8+
pull_request:
9+
branches:
10+
- main
11+
jobs:
12+
test:
13+
permissions:
14+
contents: read
15+
strategy:
16+
matrix:
17+
go-version: [ 1.16.x, 1.17.x ]
18+
os: [ ubuntu-latest, macos-latest, windows-latest ]
19+
runs-on: ${{ matrix.os }}
20+
steps:
21+
- name: install go
22+
uses: actions/setup-go@v2
23+
with:
24+
go-version: ${{ matrix.go-version }}
25+
- uses: actions/checkout@v2
26+
- name: lint with golangci-lint
27+
uses: golangci/golangci-lint-action@v2
28+
- name: run go test
29+
run: go test ./...
30+
test-action:
31+
permissions:
32+
contents: read
33+
packages: read
34+
runs-on: ubuntu-latest
35+
steps:
36+
- uses: actions/checkout@v2
37+
- name: test the tweeter action in DRY_RUN
38+
id: tweeterAction
39+
env:
40+
DRY_RUN: true
41+
uses: ./
42+
with:
43+
message: hello world!
44+
accessToken: fake
45+
accessTokenSecret: fake
46+
apiKey: fake
47+
apiKeySecret: fake
48+
- run: echo ${{ steps.tweeterAction.outputs.sentMessage }} from dry run test
49+
release:
50+
permissions:
51+
contents: write
52+
needs: test
53+
if: startsWith(github.ref, 'refs/tags/v')
54+
runs-on: ubuntu-latest
55+
steps:
56+
- uses: actions/checkout@v2
57+
- name: Set RELEASE_VERSION ENV var
58+
run: echo "RELEASE_VERSION=${GITHUB_REF:10}" >> $GITHUB_ENV
59+
- uses: actions/setup-go@v2
60+
with:
61+
go-version: 1.17.x
62+
- name: install gox
63+
run: go install github.com/mitchellh/[email protected]
64+
- name: build cross-platform binaries
65+
env:
66+
PLATFORMS: darwin/amd64 darwin/arm64 windows/amd64 linux/amd64 linux/arm64
67+
VERSION_INJECT: github.com/PacktPublishing/Go-for-DevOps/chapter/9/pkg/tweeter.Version
68+
OUTPUT_PATH_FORMAT: ./bin/${{ env.RELEASE_VERSION }}/{{.OS}}/{{.Arch}}/tweeter
69+
run: |
70+
gox -osarch="${PLATFORMS}" -ldflags "-X ${VERSION_INJECT}=${RELEASE_VERSION}" -output "${OUTPUT_PATH_FORMAT}"
71+
- name: generate release notes
72+
env:
73+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
74+
run: |
75+
gh api -X POST 'repos/{owner}/{repo}/releases/generate-notes' \
76+
-F commitish=${{ env.RELEASE_VERSION }} \
77+
-F tag_name=${{ env.RELEASE_VERSION }} \
78+
> tmp-release-notes.json
79+
- name: gzip the bins
80+
env:
81+
OUT_BASE: ./bin/${{ env.RELEASE_VERSION }}
82+
run: |
83+
tar -czvf "${OUT_BASE}/darwin/amd64/tweeter_darwin_amd64.tar.gz" -C "${OUT_BASE}/darwin/amd64" tweeter
84+
tar -czvf "${OUT_BASE}/darwin/arm64/tweeter_darwin_arm64.tar.gz" -C "${OUT_BASE}/darwin/arm64" tweeter
85+
tar -czvf "${OUT_BASE}/windows/amd64/tweeter_windows_amd64.tar.gz" -C "${OUT_BASE}/windows/amd64" tweeter.exe
86+
tar -czvf "${OUT_BASE}/linux/amd64/tweeter_linux_amd64.tar.gz" -C "${OUT_BASE}/linux/amd64" tweeter
87+
tar -czvf "${OUT_BASE}/linux/arm64/tweeter_linux_arm64.tar.gz" -C "${OUT_BASE}/linux/arm64" tweeter
88+
- name: create release
89+
env:
90+
OUT_BASE: ./bin/${{ env.RELEASE_VERSION }}
91+
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
92+
run: |
93+
jq -r .body tmp-release-notes.json > tmp-release-notes.md
94+
gh release create ${{ env.RELEASE_VERSION }} \
95+
-t "$(jq -r .name tmp-release-notes.json)" \
96+
-F tmp-release-notes.md \
97+
"${OUT_BASE}/darwin/amd64/tweeter_darwin_amd64.tar.gz#tweeter_osx_amd64" \
98+
"${OUT_BASE}/darwin/arm64/tweeter_darwin_arm64.tar.gz#tweeter_osx_arm64" \
99+
"${OUT_BASE}/windows/amd64/tweeter_windows_amd64.tar.gz#tweeter_windows_amd64" \
100+
"${OUT_BASE}/linux/amd64/tweeter_linux_amd64.tar.gz#tweeter_linux_amd64" \
101+
"${OUT_BASE}/linux/arm64/tweeter_linux_arm64.tar.gz#tweeter_linux_arm64"

.gitignore

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
# Binaries for programs and plugins
2+
*.exe
3+
*.exe~
4+
*.dll
5+
*.so
6+
*.dylib
7+
8+
# Test binary, built with `go test -c`
9+
*.test
10+
11+
# Output of the go coverage tool, specifically when used with LiteIDE
12+
*.out
13+
14+
# Dependency directories (remove the comment below to include it)
15+
# vendor/
16+
17+
.idea

Dockerfile

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
FROM golang:1.17 as builder
2+
WORKDIR /workspace
3+
4+
# Run this with docker build --build_arg $(go env GOPROXY) to override the goproxy
5+
ARG goproxy=https://proxy.golang.org
6+
ENV GOPROXY=$goproxy
7+
8+
# Copy the Go Modules manifests
9+
COPY go.mod go.mod
10+
COPY go.sum go.sum
11+
# Cache deps before building and copying source so that we don't need to re-download as much
12+
# and so that source changes don't invalidate our downloaded layer
13+
RUN go mod download
14+
15+
# Copy the sources
16+
COPY ./ ./
17+
18+
RUN CGO_ENABLED=0 GOOS=linux GOARCH=amd64 \
19+
go build -a -ldflags '-extldflags "-static"' \
20+
-o tweeter .
21+
22+
# Copy the action into a thin image
23+
FROM gcr.io/distroless/static:latest
24+
WORKDIR /
25+
COPY --from=builder /workspace/tweeter .
26+
ENTRYPOINT ["/tweeter"]

README.md

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,58 @@
11
# B18275-08-Automating-Workflows-with-GitHub-Actions-Code-Files
2-
The code files for chapter 8 of B18275 have been added on a separate repository of it's own.
2+
The code files for chapter 8 of B18275 have been added on a separate repository of its own.
3+
4+
## DevOps for Go Tweeter
5+
The tweeter command line tool will send a tweet via Twitter.
6+
7+
## Setup
8+
You can use tweeter to send a tweet or to output the message to STDOUT. If you want to send a tweet, you will need to set up a Twitter application.
9+
10+
### Setup With a Twitter Application
11+
To send a tweet, you will need to create or use an existing Twitter account, create a Twitter application, and generate API credentials. All of this can be done through the
12+
[Twitter Developer Portal](https://developer.twitter.com/en/portal/projects-and-apps).
13+
14+
### Setup Without a Twitter Application
15+
Some people may not want to set up a Twitter account. If you would like to use tweeter without sending tweets, use the `--dry-run` argument. This will cause the tool to write the message to STDOUT rather than sending the message to Twitter.
16+
17+
## Inputs
18+
19+
- `--message` **Required** the tweet message you would like to send
20+
- `--apiKey` the API key under Consumer Keys in the [Twitter developer portal](https://developer.twitter.com/en/portal/projects-and-apps)
21+
- `--apiKeySecret` the API key secret under Consumer Keys in the [Twitter developer portal](https://developer.twitter.com/en/portal/projects-and-apps)
22+
- `--accessToken` the access token under Authentication Tokens in the [Twitter developer portal](https://developer.twitter.com/en/portal/projects-and-apps)
23+
- `--accessTokenSecret` the access token secret under Authentications Tokens in the [Twitter developer portal](https://developer.twitter.com/en/portal/projects-and-apps)
24+
- `--dryRun` will skip authentication validation and sending the message to Twitter
25+
26+
## Test
27+
```
28+
$ go test ./...
29+
? github.com/devopsforgo/github-actions [no test files]
30+
ok github.com/devopsforgo/github-actions/pkg/tweeter 0.002s
31+
```
32+
33+
## Run Help
34+
To see the command line arguments and descriptions, display the help.
35+
```
36+
$ go run . -h
37+
Usage of /tmp/go-build3731631588/b001/exe/github-actions:
38+
--accessToken string twitter access token
39+
--accessTokenSecret string twitter access token secret
40+
--apiKey string twitter api key
41+
--apiKeySecret string twitter api key secret
42+
--dryRun if true, then a tweet will not be sent
43+
--message string message you'd like to send to twitter
44+
pflag: help requested
45+
exit status 2
46+
```
47+
48+
## Run Without Sending a Tweet
49+
The `--dryRun` argument will skip validation of the authentication arguments and output the message to STDOUT.
50+
```
51+
$ go run . --dryRun --message foo
52+
```
53+
54+
## Run Sending a Tweet
55+
Without `--dryRun` specified, tweeter will send the `--messsage` argument as a Tweet.
56+
```
57+
$ go run . --message foo --apiKey 123 --apiKeySecret secret --accessToken token --accessTokenSecret secret
58+
```

action.yaml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
name: Tweeter Action
2+
author: DevOps for Go
3+
description: Simple action to send a tweet via an GitHub Action.
4+
inputs:
5+
message:
6+
description: 'message you want to tweet'
7+
required: true
8+
apiKey:
9+
description: 'api key for Twitter api'
10+
required: true
11+
apiKeySecret:
12+
description: 'api key secret for Twitter api'
13+
required: true
14+
accessToken:
15+
description: 'access token for Twitter api'
16+
required: true
17+
accessTokenSecret:
18+
description: 'access token secret for Twitter api'
19+
required: true
20+
outputs:
21+
errorMessage:
22+
description: 'if something went wrong, the error message'
23+
sentMessage:
24+
description: 'message sent to Twitter'
25+
runs:
26+
using: docker
27+
image: Dockerfile
28+
args:
29+
- --message
30+
- "${{ inputs.message }}"
31+
- --apiKey
32+
- ${{ inputs.apiKey }}
33+
- --apiKeySecret
34+
- ${{ inputs.apiKeySecret }}
35+
- --accessToken
36+
- ${{ inputs.accessToken }}
37+
- --accessTokenSecret
38+
- ${{ inputs.accessTokenSecret }}

go.mod

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
module github.com/PacktPublishing/Go-for-DevOps/chapter/9
2+
3+
go 1.17
4+
5+
require (
6+
github.com/dghubble/go-twitter v0.0.0-20210609183100-2fdbf421508e
7+
github.com/dghubble/oauth1 v0.7.0
8+
github.com/hashicorp/go-multierror v1.1.1
9+
github.com/pkg/errors v0.9.1
10+
github.com/spf13/pflag v1.0.5
11+
)
12+
13+
require (
14+
github.com/cenkalti/backoff v2.1.1+incompatible // indirect
15+
github.com/dghubble/sling v1.3.0 // indirect
16+
github.com/google/go-querystring v1.0.0 // indirect
17+
github.com/hashicorp/errwrap v1.0.0 // indirect
18+
)

0 commit comments

Comments
 (0)