Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add CICD-Pipeline to build and push Docker Image #4

Open
wants to merge 106 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
106 commits
Select commit Hold shift + click to select a range
22d104c
Set start/end time KPI widget chart, add attribute filter to picker d…
DonWillems Aug 28, 2023
ea80bbe
Consoles: Updated Android to use SDK 33 due Play Store policies (#1106)
Miggets7 Aug 30, 2023
e10e5c1
Bugfix for appearance logo upload (#1108)
MartinaeyNL Aug 31, 2023
a30a538
Test change to try GitHub Actions
emilmerle-work Sep 14, 2023
f33cc86
Added test action after aws test config
emilmerle-work Sep 21, 2023
be6a325
fix
emilmerle-work Sep 21, 2023
c89db5b
Fix 2
emilmerle-work Sep 21, 2023
c08f098
Fix 3
emilmerle-work Sep 21, 2023
c0f290b
Fix 4
emilmerle-work Sep 21, 2023
7dea80b
ECR Login Action
emilmerle-work Sep 21, 2023
ec0e6c0
Test Docker build
emilmerle-work Sep 21, 2023
8415867
Added id-token permission to workflow
emilmerle-work Sep 28, 2023
e504c88
Try to tag and push image to aws
emilmerle-work Sep 28, 2023
94cdcbd
Added debugging
emilmerle-work Sep 28, 2023
9852cb7
Added quiet option to docker build command
emilmerle-work Sep 28, 2023
316f356
Try default deploy script
emilmerle-work Sep 28, 2023
b9435d3
Try without push
emilmerle-work Sep 28, 2023
315c51a
Try without push
emilmerle-work Sep 28, 2023
1af6486
Try without tag Umweg
emilmerle-work Sep 28, 2023
6a2494d
Fix
emilmerle-work Sep 28, 2023
71310b4
Only build
emilmerle-work Sep 28, 2023
865d34a
New AWS Policies
emilmerle-work Sep 28, 2023
4bbd745
Different build command
emilmerle-work Sep 28, 2023
d10455c
Dont install buildx
emilmerle-work Sep 28, 2023
1dce0f1
Removed unused docker commands
emilmerle-work Sep 28, 2023
d543b85
Test with buildx docker setup (roll back if it doesnt work)
emilmerle-work Sep 28, 2023
1e739d7
added debugging commands
emilmerle-work Sep 28, 2023
33b7c35
Try push with tag: latest
emilmerle-work Sep 28, 2023
94b6ece
Try without buildx again, but try to keep other steps as they are
emilmerle-work Sep 28, 2023
7e647ea
Remove tag latest
emilmerle-work Sep 28, 2023
dfa1de4
Added action to delete old docker images
emilmerle-work Sep 28, 2023
9de9fa4
Fix
emilmerle-work Sep 28, 2023
e1089c6
Try without admin privileges
emilmerle-work Sep 28, 2023
8f36d28
Try to delete old images
emilmerle-work Sep 28, 2023
31f83ee
Delete old images
emilmerle-work Sep 28, 2023
bc493e0
Added action to automatically delete old images from AWS ECR
emilmerle-work Sep 28, 2023
31cbd8d
action deleted
emilmerle-work Sep 28, 2023
8d255b4
Test change in backend? (manager)
emilmerle-work Sep 28, 2023
eed008c
Test backend tests in GH actions
emilmerle-work Sep 28, 2023
05ca797
Try build and push with one buildx command
emilmerle-work Oct 5, 2023
0fd25cd
Try with other command order
emilmerle-work Oct 5, 2023
a3b22e4
Try without docker rmi command
emilmerle-work Oct 5, 2023
56d745f
Try with multi platform build
emilmerle-work Oct 5, 2023
46456a7
Install buildx and try new command
emilmerle-work Oct 5, 2023
69baa62
Fixed build command
emilmerle-work Oct 5, 2023
3c0a719
Try with provenance=false to avoid images with 0mb in AWS ECR
emilmerle-work Oct 5, 2023
ee272e9
Fix
emilmerle-work Oct 5, 2023
2a973b4
Only build for one platform
emilmerle-work Oct 9, 2023
36f0e98
Try with two tags (latest)
emilmerle-work Oct 9, 2023
90125cc
Mini commit to generate new image
emilmerle-work Oct 9, 2023
c7f2e83
Mini Commit for Docker Test
emilmerle-work Oct 9, 2023
b8ec24d
Mini Commit for new image
emilmerle-work Oct 12, 2023
62d4f89
Mini Commit for new Image
emilmerle-work Oct 12, 2023
1df9ab0
Added test folder
emilmerle-work Oct 12, 2023
db39cac
Better scripts
emilmerle-work Oct 16, 2023
9bef852
Recover Script added
emilmerle-work Oct 16, 2023
c8cbd15
Removed one script
emilmerle-work Oct 16, 2023
c237cbc
Change of ECR Repo
emilmerle-work Oct 19, 2023
7e0b37b
Pushing failed build
emilmerle-work Oct 19, 2023
25c3a45
Try to run tests in CI
emilmerle-work Oct 19, 2023
7e6d065
Test fail changes
emilmerle-work Oct 19, 2023
e6e18a4
Corrected build
emilmerle-work Oct 19, 2023
0aebcd3
Added pipeline
emilmerle-work Oct 23, 2023
653bad7
Removed public ip step from pipeline
emilmerle-work Oct 23, 2023
65ba26d
Removed public ip step
emilmerle-work Oct 23, 2023
9736387
Added public ip step again
emilmerle-work Oct 23, 2023
4b3e915
Test public ip step again
emilmerle-work Oct 23, 2023
7d4d07c
Fixed config file for cicd
emilmerle-work Oct 23, 2023
c035337
Mini commit to test tests in pipeline
emilmerle-work Oct 23, 2023
b4dea8a
Smallest ci_cd.json file test
emilmerle-work Oct 26, 2023
4693709
Try with deploy in ci_cd.json
emilmerle-work Oct 26, 2023
da0b9ec
Smallest ci_cd.json file
emilmerle-work Oct 26, 2023
709ab45
Comment added
emilmerle-work Oct 26, 2023
85e6d9a
Docker build only if there is deploy for that branch in ci_cd.json
emilmerle-work Oct 26, 2023
160ce35
Test mit neuem Tag
emilmerle-work Oct 26, 2023
2c26eef
2 mini commits to try frontend and backend tests in pipeline
emilmerle-work Oct 26, 2023
a186fcc
Frontend changes to test tests
emilmerle-work Oct 26, 2023
986a3c0
New docker compose with watchtower
emilmerle-work Oct 30, 2023
d9e891c
Excluded dir from backend files
emilmerle-work Oct 30, 2023
71c0363
Added watchtower enable flag in docker-compose
emilmerle-work Nov 2, 2023
551f9dc
Added enable label to images
emilmerle-work Nov 2, 2023
3b3a8fd
Minimal ci_cd.json file possible?
emilmerle-work Nov 2, 2023
08ffed1
Minimal ci_cd.json file?
emilmerle-work Nov 2, 2023
f429de2
Tag Docker Image with date
emilmerle-work Nov 2, 2023
2e9648a
Corrected date format
emilmerle-work Nov 2, 2023
ce8de6a
Two branches in ci_cd.json but push on master
emilmerle-work Nov 2, 2023
b9915ed
Changed backend file
emilmerle-work Nov 2, 2023
fb987db
Minimal CICD changes
emilmerle-work Nov 6, 2023
368d1dc
Test minimal cicd
emilmerle-work Nov 6, 2023
f21bb50
Test minimal cicd changes
emilmerle-work Nov 6, 2023
12579c5
Minimal cicd
emilmerle-work Nov 6, 2023
f886a87
Test minimal cicd with backend changes
emilmerle-work Nov 6, 2023
8d9023d
Added documentation
emilmerle-work Nov 6, 2023
e78702a
Added cicd on development push too
emilmerle-work Nov 6, 2023
5dc87d3
ref_name change to function on merge too
emilmerle-work Nov 6, 2023
e81f2de
Try ref_name
emilmerle-work Nov 6, 2023
7a13d2b
Try without -PopenremoteVersion in installDist command
emilmerle-work Nov 6, 2023
d91eff0
Merge pull request #1 from emilmerle-work/development
emilmerle-work Nov 6, 2023
bd6624b
different structure
emilmerle-work Nov 6, 2023
d212096
Reversed unnecessary changes
emilmerle-work Nov 6, 2023
7f549c8
Added frontend test step (no test right now)
emilmerle-work Nov 6, 2023
385e6b9
Try to remove unnecesary changes
emilmerle-work Nov 6, 2023
abcbe67
Remove changes
emilmerle-work Nov 6, 2023
c951c6d
Remove changes
emilmerle-work Nov 6, 2023
5b67359
Added doku
emilmerle-work Nov 9, 2023
7c8f052
Merge branch 'feature/cicd_pipeline' into master
emilmerle-work Nov 9, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 2 additions & 16 deletions .ci_cd/ci_cd.json
Original file line number Diff line number Diff line change
@@ -1,23 +1,9 @@
{
"push": {
"master": {
"distribute": {
"docker": "develop,latest"
},
"developement": {
"deploy": {
"managerTag": "develop",
"environment": "staging"

}
}
},
"release": {
"distribute": {
"docker": "latest,$version",
"maven": "$version"
},
"deploy": {
"managerTag": "latest",
"environment": "production"
}
}
}
294 changes: 170 additions & 124 deletions .github/workflows/ci_cd.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ on:
# Push on master excluding tags
push:
branches:
- 'master'
- 'feature/cicd_pipeline'
tags-ignore:
- '*.*'

Expand Down Expand Up @@ -93,6 +93,12 @@ on:
secrets:
SECRETS:
required: false

# needed for aws auth
permissions:
id-token: write # This is required for requesting the JWT
contents: read # This is required for actions/checkout


jobs:

Expand Down Expand Up @@ -176,7 +182,7 @@ jobs:
# Check which files have changed to only run appropriate tests and checks
- name: Backend files changed
id: backend-files-changed
if: github.event_name == 'pull_request'
# if: github.event_name == 'pull_request'
uses: tj-actions/changed-files@v35
with:
files_ignore: |
Expand All @@ -192,7 +198,7 @@ jobs:
# Check which files have changed to only run appropriate tests and checks
- name: UI files changed
id: ui-files-changed
if: github.event_name == 'pull_request'
# if: github.event_name == 'pull_request'
uses: tj-actions/changed-files@v35
with:
files: |
Expand Down Expand Up @@ -379,9 +385,9 @@ jobs:
deploys = eventConfig['deploy'] if 'deploy' in eventConfig else {}
if 'distribute' in eventConfig:
if 'docker' in eventConfig['distribute']:
dockerPublishTags = eventConfig['distribute']['docker']
dockerPublishTags = eventConfig['distribute']['docker']
if 'maven' in eventConfig['distribute']:
mavenPublishTag = eventConfig['distribute']['maven']
mavenPublishTag = eventConfig['distribute']['maven']

if dockerPublishTags is not None and isMainRepo == 'true':
dockerPublishTags = dockerPublishTags.replace("$version", refName)
Expand Down Expand Up @@ -567,7 +573,7 @@ jobs:
uses: docker/setup-buildx-action@v2
with:
version: latest
install: true
install: true # set docker buildx build as synonym for docker build

- name: Set up JDK 17 and gradle cache
id: java
Expand Down Expand Up @@ -758,131 +764,171 @@ jobs:
run: |
${{ steps.deployment-docker-command.outputs.value }}

- name: Do deployments
if: steps.deployments.outputs.value != ''
shell: python
run: |
import json
import os
import sys
import subprocess

deployments = os.getenv("DEPLOYMENTS")
deployments = deployments.split(";")
managerRef = os.getenv("MANAGER_REF")
deploymentRef = os.getenv("DEPLOYMENT_REF")
isCustomProject = os.getenv("IS_CUSTOM_PROJECT")
inputsAndSecrets = json.loads(os.getenv("INPUTS_AND_SECRETS"))
ipv4 = os.getenv("IPV4")
ipv6 = os.getenv("IPV6")

failure = False

# Determine deploy script to use
deployScript = ".ci_cd/deploy.sh"

if not os.path.exists(deployScript) and isCustomProject == 'true':
deployScript = "openremote/.ci_cd/deploy.sh"

if not os.path.exists(deployScript):
os.system(f"Deploy script not found '{deployScript}'")
sys.exit(1)

for deployment in deployments:
dep = deployment.split(":")
env = dep[0]
managerTag = dep[1]
managerTagFound = True

os.putenv("MANAGER_TAG", managerTag)
os.putenv("ENVIRONMENT", env)

# Clean stale ssh credentials and temp files
os.system("rm temp.env 2>/dev/null")
os.system("rm ssh.key 2>/dev/null")
os.system("rm -r temp 2>/dev/null")
os.system("mkdir temp")

# ------------------------------------------------------
# Output env variables to temp env file for POSIX shell
# ------------------------------------------------------

# Output inputs and secrets (spacial handling for SSH_KEY and some other variables)
# _$ENV_ prefixed keys are output last (to override any non env specific keys)
environment = (env if env else "").upper()
prefix = "_" + environment + "_"

for key, value in inputsAndSecrets.items():
if "." in key:
continue
- name: Configure AWS Credentials
uses: aws-actions/configure-aws-credentials@v4
with:
role-to-assume: arn:aws:iam::205672091018:role/GitHubAction-AssumeRoleWithAction
role-session-name: GitHub_to_AWS_via_FederatedOIDC_meterverse
aws-region: ${{ env.AWS_REGION }}
env:
AWS_REGION: eu-central-1

- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v1

envFile = "temp/env"
- name: Set current date as env variable
run: echo "NOW=$(TZ='Europe/Berlin' date +'%Y-%m-%dT%H-%M-%S')" >> $GITHUB_ENV

# Look for temp and env prefixed keys
if key.startswith("_"):
if key.startswith("_TEMP_"):
key = key.replace("_TEMP_", "")
envFile = "temp.env"
elif key.startswith(prefix):
key = key.replace(prefix, "")
else:
continue
- name: Build, Tag, and Push image to Amazon ECR
id: build-image
if: steps.deployments.outputs.value != ''
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: meterverse_manager
ECR_IMAGE_TAG:
IMAGE_TAG: ${{ github.sha }}
run: |
docker build --provenance=false --platform linux/amd64 --build-arg GIT_COMMIT=$IMAGE_TAG \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG \
-t $ECR_REGISTRY/$ECR_REPOSITORY:$NOW \
-t $ECR_REGISTRY/$ECR_REPOSITORY:latest \
manager/build/install/manager -o type=registry
echo "image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" >> $GITHUB_OUTPUT

if key == "github_token":
continue
else:
os.system(f"echo 'Secret found {key}...'")
if key == "SSH_KEY":
os.system(f"echo \"{value}\" > ssh.key")
else:
lines = len(value.split("\n"))
if lines > 1:
os.system(f"echo '{key}='\"'\"'' >> {envFile}")
os.system(f"echo '{value}'\"'\"'' >> {envFile}")
else:
os.system(f"echo '{key}='\"'\"'{value}'\"'\"'' >> {envFile}")

# delete image from github actions after push:
# docker rmi $ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG
docker image prune -f

# Output env file if exists
if os.path.exists(".ci_cd/env/.env"):
os.system(f"echo 'Outputting .ci_cd/env/.env to temp/env'")
os.system("cat .ci_cd/env/.env >> temp/env")
# echo "Docker images:"
# docker images

# - name: Do deployments
# if: steps.deployments.outputs.value != ''
# shell: python
# run: |
# import json
# import os
# import sys
# import subprocess

# deployments = os.getenv("DEPLOYMENTS")
# deployments = deployments.split(";")
# managerRef = os.getenv("MANAGER_REF")
# deploymentRef = os.getenv("DEPLOYMENT_REF")
# isCustomProject = os.getenv("IS_CUSTOM_PROJECT")
# inputsAndSecrets = json.loads(os.getenv("INPUTS_AND_SECRETS"))
# ipv4 = os.getenv("IPV4")
# ipv6 = os.getenv("IPV6")

# failure = False

# # Determine deploy script to use
# deployScript = ".ci_cd/deploy.sh"

# if not os.path.exists(deployScript) and isCustomProject == 'true':
# deployScript = "openremote/.ci_cd/deploy.sh"

# if not os.path.exists(deployScript):
# os.system(f"Deploy script not found '{deployScript}'")
# sys.exit(1)

# for deployment in deployments:
# dep = deployment.split(":")
# env = dep[0]
# managerTag = dep[1]
# managerTagFound = True

# os.putenv("MANAGER_TAG", managerTag)
# os.putenv("ENVIRONMENT", env)

# # Clean stale ssh credentials and temp files
# os.system("rm temp.env 2>/dev/null")
# os.system("rm ssh.key 2>/dev/null")
# os.system("rm -r temp 2>/dev/null")
# os.system("mkdir temp")

# # ------------------------------------------------------
# # Output env variables to temp env file for POSIX shell
# # ------------------------------------------------------

# # Output inputs and secrets (spacial handling for SSH_KEY and some other variables)
# # _$ENV_ prefixed keys are output last (to override any non env specific keys)
# environment = (env if env else "").upper()
# prefix = "_" + environment + "_"

# for key, value in inputsAndSecrets.items():
# if "." in key:
# continue

# envFile = "temp/env"

# # Look for temp and env prefixed keys
# if key.startswith("_"):
# if key.startswith("_TEMP_"):
# key = key.replace("_TEMP_", "")
# envFile = "temp.env"
# elif key.startswith(prefix):
# key = key.replace(prefix, "")
# else:
# continue

# if key == "github_token":
# continue
# else:
# os.system(f"echo 'Secret found {key}...'")
# if key == "SSH_KEY":
# os.system(f"echo \"{value}\" > ssh.key")
# else:
# lines = len(value.split("\n"))
# if lines > 1:
# os.system(f"echo '{key}='\"'\"'' >> {envFile}")
# os.system(f"echo '{value}'\"'\"'' >> {envFile}")
# else:
# os.system(f"echo '{key}='\"'\"'{value}'\"'\"'' >> {envFile}")

# # Output env file if exists
# if os.path.exists(".ci_cd/env/.env"):
# os.system(f"echo 'Outputting .ci_cd/env/.env to temp/env'")
# os.system("cat .ci_cd/env/.env >> temp/env")

# Output environment specific env file if exists
if env is not None and env != '' and os.path.exists(f".ci_cd/env/{env}.env"):
os.system(f"echo 'Outputting .ci_cd/env/{env}.env to temp/env'")
os.system(f"cat .ci_cd/env/{env}.env >> temp/env")
# # Output environment specific env file if exists
# if env is not None and env != '' and os.path.exists(f".ci_cd/env/{env}.env"):
# os.system(f"echo 'Outputting .ci_cd/env/{env}.env to temp/env'")
# os.system(f"cat .ci_cd/env/{env}.env >> temp/env")

# Set CIDR environment variable
if ipv4 is not None and ipv4 != '':
os.putenv("CIDR", ipv4 + '/32')
elif ipv6 is not None and ipv6 != '':
os.putenv("CIDR", ipv6 + '/64')
# # Set CIDR environment variable
# if ipv4 is not None and ipv4 != '':
# os.putenv("CIDR", ipv4 + '/32')
# elif ipv6 is not None and ipv6 != '':
# os.putenv("CIDR", ipv6 + '/64')

# Execute deploy script
os.system(f"echo 'Executing deploy script for deployment: managerTag={managerTag} deploymentTag={deploymentRef} environment={env}'")
# Uncomment this in combination with the SSH debug step afterwards to debug deployment script
#sys.exit(0)
result = subprocess.run(f"bash {deployScript}", shell=True)

if result.returncode != 0:
os.system(f"echo 'Deployment failed: managerTag={managerTag} deploymentTag={deploymentRef} environment={env}'")
failure = True
continue

if failure == True:
os.system("echo 'One or more deployments failed'")
sys.exit(1)
env:
IS_CUSTOM_PROJECT: ${{ steps.check_custom_project.outputs.files_exists }}
REPO_NAME: ${{ github.repository }}
DEPLOYMENTS: ${{ steps.deployments.outputs.value }}
MANAGER_DOCKER_BUILD_PATH: ${{ steps.manager-docker-command.outputs.buildPath }}
DEPLOYMENT_DOCKER_BUILD_PATH: ${{ steps.deployment-docker-command.outputs.buildPath }}
MANAGER_REF: ${{ steps.manager-docker-command.outputs.refTag }}
DEPLOYMENT_REF: ${{ steps.deployment-docker-command.outputs.refTag }}
INPUTS_AND_SECRETS: ${{ toJSON(steps.inputs-and-secrets.outputs) }}
IPV4: ${{ steps.ip-address.outputs.ipv4 }}
IPV6: ${{ steps.ip-address.outputs.ipv6 }}
# # Execute deploy script
# os.system(f"echo 'Executing deploy script for deployment: managerTag={managerTag} deploymentTag={deploymentRef} environment={env}'")
# # Uncomment this in combination with the SSH debug step afterwards to debug deployment script
# #sys.exit(0)
# result = subprocess.run(f"bash {deployScript}", shell=True)

# if result.returncode != 0:
# os.system(f"echo 'Deployment failed: managerTag={managerTag} deploymentTag={deploymentRef} environment={env}'")
# failure = True
# continue

# if failure == True:
# os.system("echo 'One or more deployments failed'")
# sys.exit(1)
# env:
# IS_CUSTOM_PROJECT: ${{ steps.check_custom_project.outputs.files_exists }}
# REPO_NAME: ${{ github.repository }}
# DEPLOYMENTS: ${{ steps.deployments.outputs.value }}
# MANAGER_DOCKER_BUILD_PATH: ${{ steps.manager-docker-command.outputs.buildPath }}
# DEPLOYMENT_DOCKER_BUILD_PATH: ${{ steps.deployment-docker-command.outputs.buildPath }}
# MANAGER_REF: ${{ steps.manager-docker-command.outputs.refTag }}
# DEPLOYMENT_REF: ${{ steps.deployment-docker-command.outputs.refTag }}
# INPUTS_AND_SECRETS: ${{ toJSON(steps.inputs-and-secrets.outputs) }}
# IPV4: ${{ steps.ip-address.outputs.ipv4 }}
# IPV6: ${{ steps.ip-address.outputs.ipv6 }}

# - name: Setup upterm session
# uses: lhotari/action-upterm@v1
Expand Down
Loading
Loading