-
Notifications
You must be signed in to change notification settings - Fork 8
155 lines (143 loc) · 6.63 KB
/
release-to-github-packages.yml
File metadata and controls
155 lines (143 loc) · 6.63 KB
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
143
144
145
146
147
148
149
150
151
152
153
154
155
name: Catalog - Branch-Based Release and Deploy
on:
push:
branches: [ 'develop', 'staging', 'main' ]
tags: [ 'v*' ]
workflow_dispatch:
jobs:
determine-config:
runs-on: self-hosted
outputs:
environment: ${{ steps.config.outputs.ENVIRONMENT }}
should_release: ${{ steps.config.outputs.SHOULD_RELEASE }}
version: ${{ steps.config.outputs.VERSION }}
cluster: ${{ steps.config.outputs.CLUSTER }}
service: ${{ steps.config.outputs.SERVICE }}
task_def: ${{ steps.config.outputs.TASK_DEF }}
container: ${{ steps.config.outputs.CONTAINER }}
profile: ${{ steps.config.outputs.PROFILE }}
steps:
- uses: actions/checkout@v4
with:
fetch-depth: 0
- name: Determine environment and config
id: config
run: |
# Determine from branch/tag
if [[ "${{ github.ref }}" == refs/tags/v* ]]; then
ENV="prod"
SHOULD_RELEASE="true"
VERSION=${GITHUB_REF#refs/tags/v}
CLUSTER="prod-cloudfeeds-ecs-cluster"
SERVICE="prod-prod-catalog"
TASK_DEF="prod-prod-catalog"
CONTAINER="prod-catalog"
PROFILE="feeds"
elif [[ "${{ github.ref }}" == "refs/heads/main" ]]; then
ENV="prod"
SHOULD_RELEASE="false"
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
CLUSTER="prod-cloudfeeds-ecs-cluster"
SERVICE="prod-prod-catalog"
TASK_DEF="prod-prod-catalog"
CONTAINER="prod-catalog"
PROFILE="feeds"
elif [[ "${{ github.ref }}" == "refs/heads/staging" ]]; then
ENV="staging"
SHOULD_RELEASE="false"
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
CLUSTER="staging-staging-cloudfeeds-ecs-cluster"
SERVICE="staging-staging-catalog"
TASK_DEF="staging-staging-catalog"
CONTAINER="staging-catalog"
PROFILE="staging"
elif [[ "${{ github.ref }}" == "refs/heads/develop" ]]; then
ENV="test"
SHOULD_RELEASE="true"
VERSION=$(mvn help:evaluate -Dexpression=project.version -q -DforceStdout)
CLUSTER="abdu7511-test-cloudfeeds-ecs-cluster"
SERVICE="test-abdu7511-catalog"
TASK_DEF="test-abdu7511-catalog"
CONTAINER="abdu7511-catalog"
PROFILE="test"
fi
echo "ENVIRONMENT=${ENV}" >> $GITHUB_OUTPUT
echo "SHOULD_RELEASE=${SHOULD_RELEASE}" >> $GITHUB_OUTPUT
echo "VERSION=${VERSION}" >> $GITHUB_OUTPUT
echo "CLUSTER=${CLUSTER}" >> $GITHUB_OUTPUT
echo "SERVICE=${SERVICE}" >> $GITHUB_OUTPUT
echo "TASK_DEF=${TASK_DEF}" >> $GITHUB_OUTPUT
echo "CONTAINER=${CONTAINER}" >> $GITHUB_OUTPUT
echo "PROFILE=${PROFILE}" >> $GITHUB_OUTPUT
echo "Deploying to: ${ENV}"
echo "Should release JAR: ${SHOULD_RELEASE}"
echo "Version: ${VERSION}"
release-jar:
name: Release JAR to GitHub Packages
runs-on: self-hosted
needs: determine-config
if: needs.determine-config.outputs.should_release == 'true'
steps:
- uses: actions/checkout@v4
- name: Set version
run: mvn versions:set -DnewVersion=${{ needs.determine-config.outputs.version }} -DgenerateBackupPoms=false
- name: Build and test
run: mvn clean test -B
- name: Deploy to GitHub Packages
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
GITHUB_ACTOR: ${{ github.actor }}
run: |
mvn -up -P build-app-rpm deploy -DskipTests -B
echo " Published Catalog ${{ needs.determine-config.outputs.version }}"
get-schema:
runs-on: self-hosted
needs: [determine-config, release-jar]
if: always() && (needs.release-jar.result == 'success' || needs.release-jar.result == 'skipped')
outputs:
schema_version: ${{ steps.get.outputs.SCHEMA_VERSION }}
steps:
- name: Get latest schema
id: get
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
run: |
SCHEMA=$(curl -s -H "Authorization: token ${GITHUB_TOKEN}" \
"https://api.github.com/orgs/rackerlabs/packages/maven/com.rackspace.usage.usage-schema/versions" | \
jq -r 'sort_by(.created_at) | reverse | .[0].name' 2>/dev/null || echo "1.138.6")
echo "SCHEMA_VERSION=${SCHEMA}" >> $GITHUB_OUTPUT
build-and-push:
runs-on: self-hosted
needs: [determine-config, get-schema]
outputs:
image_uri: ${{ steps.build.outputs.IMAGE_URI }}
steps:
- uses: actions/checkout@v4
- name: ECR Login
run: aws ecr get-login-password --region us-east-2 --profile tf_user | docker login --username AWS --password-stdin 583275065488.dkr.ecr.us-east-2.amazonaws.com
- name: Build
id: build
run: |
IMAGE_TAG=${{ needs.determine-config.outputs.environment }}-$(date +%Y%m%d%H%M%S)
IMAGE_URI="583275065488.dkr.ecr.us-east-2.amazonaws.com/catalog:$IMAGE_TAG"
echo "IMAGE_URI=$IMAGE_URI" >> $GITHUB_OUTPUT
docker build -t catalog:$IMAGE_TAG -f docker/Dockerfile \
--build-arg SCHEMA_VERSION=${{ needs.get-schema.outputs.schema_version }} \
.
docker tag catalog:$IMAGE_TAG $IMAGE_URI
- name: Push
run: docker push ${{ steps.build.outputs.IMAGE_URI }}
deploy:
runs-on: self-hosted
needs: [determine-config, build-and-push]
steps:
- name: Deploy to ECS
run: |
aws ecs describe-task-definition --task-definition ${{ needs.determine-config.outputs.task_def }} --profile ${{ needs.determine-config.outputs.profile }} --region us-east-2 \
--query 'taskDefinition | {containerDefinitions: containerDefinitions, family: family, taskRoleArn: taskRoleArn, executionRoleArn: executionRoleArn, networkMode: networkMode, volumes: volumes, placementConstraints: placementConstraints, requiresCompatibilities: requiresCompatibilities, cpu: cpu, memory: memory}' \
--output json > task-def.json
sed -i "/"name": "${{ needs.determine-config.outputs.container }}"/,/}/s|\"image\": \".*\"|\"image\": \"${{ needs.build-and-push.outputs.image_uri }}\"|" task-def.json
TASK_ARN=$(aws ecs register-task-definition --cli-input-json file://task-def.json --profile ${{ needs.determine-config.outputs.profile }} --region us-east-2 --query 'taskDefinition.taskDefinitionArn' --output text)
rm task-def.json
aws ecs update-service --cluster ${{ needs.determine-config.outputs.cluster }} --service ${{ needs.determine-config.outputs.service }} --task-definition "$TASK_ARN" --profile ${{ needs.determine-config.outputs.profile }} --region us-east-2
echo " Deployed to ${{ needs.determine-config.outputs.environment }}"