-
Notifications
You must be signed in to change notification settings - Fork 63
142 lines (119 loc) · 4.36 KB
/
java-build-push-git-template.yaml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# This workflow will:
# - build a maven Java application
# - create a docker container
# - publish it to Quay
# - commit updates to gitops repo
#
### Before you begin:
# - Have write access to a container image registry such as quay.io or Dockerhub.
# - Have access to your gitops repo.
# Name of the workflow
name: Build, Push, Gitops
on:
push:
branches:
- master
paths-ignore:
- '.github/**'
# Environment variables available to all jobs and steps in this workflow
env:
# EDIT secrets with with your registry, registry path, and apikey
REGISTRY: quay.io
REGISTRY_NAMESPACE: ${{ secrets.REGISTRY_NAMESPACE }}
# EDIT with your registry username.
REGISTRY_USER: ${{ secrets.REGISTRY_USER }}
REGISTRY_PASSWORD: ${{ secrets.REGISTRY_PASSWORD }}
IMAGE_NAME: trader
GITHUB_SHA: ${{ github.sha }}
GITOPS_REPO: stocktrader-ops/stocktrader-gitops
GITOPS_DIR: application
GITOPS_USERNAME: ${{ secrets.GITOPS_USERNAME }}
GITOPS_TOKEN: ${{ secrets.GITOPS_TOKEN }}
jobs:
setup-build-publish-deploy:
name: Setup, Build, Publish
runs-on: ubuntu-latest
outputs:
image-registry-path: ${{ steps.push-to-registry.outputs.image-registry-path }}
unit-test-result: ${{ steps.unit-test.outputs.unit-test-result }}
environment: production
steps:
# Checkout app repo
- name: Checkout
uses: actions/checkout@v2
# Setup java
- name: Setup Java
uses: actions/setup-java@v1
with:
java-version: 17
# Build and package app
- name: Build and package app
id: unit-test
run: |
mvn clean verify
cat target/failsafe-reports/failsafe-summary.xml
grep -q "<failures>0</failures>" target/failsafe-reports/failsafe-summary.xml
code=$?
echo "ret: $code"
if [[ $code -eq 0 ]]; then
echo "success"
echo '::set-output name=unit-test-result::success'
else
echo "failed"
echo '::set-output name=unit-test-result::failed'
fi
# Build the Docker image
- name: Build with Docker
run: |
docker build -t "$REGISTRY"/"$REGISTRY_NAMESPACE"/"$IMAGE_NAME":"$GITHUB_SHA" \
--build-arg GITHUB_SHA="$GITHUB_SHA" \
--build-arg GITHUB_REF="$GITHUB_REF" .
# Push the image to Image Registry
- name: Push the image to Registry
id: push-to-registry
run: |
docker login -u="$REGISTRY_USER" -p="$REGISTRY_PASSWORD" "$REGISTRY"
docker push $REGISTRY/$REGISTRY_NAMESPACE/$IMAGE_NAME:$GITHUB_SHA
echo '::set-output name=image-registry-path::$REGISTRY_HOSTNAME/$REGISTRY_NAMESPACE/$IMAGE_NAME:$GITHUB_SHA'
update-gitops-repo:
name: Publish image updates to gitops repo
runs-on: ubuntu-latest
needs: [setup-build-publish-deploy]
steps:
# Checkout gitops repo
- name: Checkout gitops repo
uses: actions/checkout@v2
with:
repository: ${{env.GITOPS_REPO}}
path: gitops
token: ${{secrets.GITOPS_TOKEN}}
# Update application
- name: Upate application
run: |
set -x
set +e
ls -la
ls -la gitops
cd gitops
## update manifests to new image and tag
APP_IMAGE="$REGISTRY/$REGISTRY_NAMESPACE/$IMAGE_NAME"
VERSION="$GITHUB_SHA"
echo "image-registry-path: ${{needs.setup-build-publish-deploy.image-registry-path}}"
echo "${APP_IMAGE}"
echo "${VERSION}"
echo "print yq version"
yq --version
# yq w -i "${GITOPS_DIR}/stocktrader-cr.yaml" spec.trader.image.repository "${APP_IMAGE}"
yq e ".spec.trader.image.repository = \"$APP_IMAGE\"" -i "${GITOPS_DIR}/stocktrader-cr.yaml"
# yq w -i "${GITOPS_DIR}/stocktrader-cr.yaml" spec.trader.image.tag "${VERSION}"
yq e ".spec.trader.image.tag = \"$VERSION\"" -i "${GITOPS_DIR}/stocktrader-cr.yaml"
cat "${GITOPS_DIR}/stocktrader-cr.yaml"
if [[ $(git status -s | wc -l) -eq 0 ]]; then
echo "No changes"
exit 0
fi
git add "${GITOPS_DIR}/"
git config --global user.name 'GH Actions'
git config --global user.email '[email protected]'
git commit -am "Updates ${APP_NAME} to ${VERSION}"
git push https://$GITOPS_USERNAME:[email protected]/$GITOPS_REPO