|
3 | 3 | CWA_GITHUB_TEST_REPO_NAME: "aws/amazon-cloudwatch-agent-test" |
4 | 4 | CWA_GITHUB_TEST_REPO_BRANCH: "main" |
5 | 5 | CHECKOUT_ROOT_DIR: "test" |
| 6 | + SSM_DISTRIBUTOR_VERSION_LIMIT: 25 |
6 | 7 | on: |
7 | 8 | workflow_dispatch: |
8 | 9 | inputs: |
@@ -74,22 +75,39 @@ jobs: |
74 | 75 | contents: read |
75 | 76 | outputs: |
76 | 77 | agent-version: ${{ steps.version.outputs.version }} |
| 78 | + should-build: ${{ steps.check.outputs.should-build }} |
77 | 79 | steps: |
78 | 80 | - name: Configure AWS Credentials |
79 | 81 | uses: aws-actions/configure-aws-credentials@v4 |
80 | 82 | with: |
81 | 83 | role-to-assume: ${{ inputs.TerraformAWSAssumeRole }} |
82 | 84 | aws-region: ${{ inputs.Region }} |
| 85 | + |
83 | 86 | - name: Agent Version |
84 | 87 | id: version |
85 | 88 | run: | |
86 | 89 | aws s3 cp s3://${{ inputs.Bucket }}/${{ inputs.BucketKey }}/CWAGENT_VERSION . |
87 | 90 | echo "version=$(cat CWAGENT_VERSION)" >> "$GITHUB_OUTPUT" |
88 | 91 |
|
| 92 | + - name: Check if SSM distributor version already exists |
| 93 | + id: check |
| 94 | + run: | |
| 95 | + if aws ssm describe-document \ |
| 96 | + --name "${{ inputs.DistributorName }}" \ |
| 97 | + --region ${{ inputs.Region }} \ |
| 98 | + --query 'Document.VersionName' \ |
| 99 | + --output text 2>/dev/null | grep -q "^${{ steps.version.outputs.version }}$"; then |
| 100 | + echo "should-build=false" >> "$GITHUB_OUTPUT" |
| 101 | + echo "Version ${{ steps.version.outputs.version }} already exists, skipping build" |
| 102 | + else |
| 103 | + echo "should-build=true" >> "$GITHUB_OUTPUT" |
| 104 | + fi |
| 105 | +
|
89 | 106 | PackageLinux: |
90 | 107 | name: 'PackageLinux' |
91 | 108 | runs-on: ubuntu-latest |
92 | 109 | needs: [AgentVersion] |
| 110 | + if: needs.AgentVersion.outputs.should-build == 'true' |
93 | 111 | permissions: |
94 | 112 | id-token: write |
95 | 113 | contents: read |
@@ -149,6 +167,7 @@ jobs: |
149 | 167 | name: 'PackageDarwin' |
150 | 168 | runs-on: ubuntu-latest |
151 | 169 | needs: [AgentVersion] |
| 170 | + if: needs.AgentVersion.outputs.should-build == 'true' |
152 | 171 | permissions: |
153 | 172 | id-token: write |
154 | 173 | contents: read |
@@ -206,6 +225,7 @@ jobs: |
206 | 225 | name: 'PackageWindows' |
207 | 226 | runs-on: ubuntu-latest |
208 | 227 | needs: [AgentVersion] |
| 228 | + if: needs.AgentVersion.outputs.should-build == 'true' |
209 | 229 | permissions: |
210 | 230 | id-token: write |
211 | 231 | contents: read |
@@ -260,6 +280,7 @@ jobs: |
260 | 280 | name: 'UploadDistributor' |
261 | 281 | runs-on: ubuntu-latest |
262 | 282 | needs: [AgentVersion, PackageLinux, PackageDarwin, PackageWindows] |
| 283 | + if: needs.AgentVersion.outputs.should-build == 'true' |
263 | 284 | permissions: |
264 | 285 | id-token: write |
265 | 286 | contents: read |
@@ -321,6 +342,16 @@ jobs: |
321 | 342 | --document-type Package \ |
322 | 343 | --region ${{ inputs.Region }} |
323 | 344 |
|
| 345 | + - name: Remove oldest if limit hit |
| 346 | + if: steps.check-distributor.outputs.exists == 'true' |
| 347 | + run: | |
| 348 | + VERSION_COUNT=$(aws ssm list-document-versions --name "${{ inputs.DistributorName }}" --region ${{ inputs.Region }} --no-paginate --query 'length(DocumentVersions)' --output text) |
| 349 | + if [ "$VERSION_COUNT" -ge ${{ env.SSM_DISTRIBUTOR_VERSION_LIMIT }} ]; then |
| 350 | + OLDEST_VERSION=$(aws ssm list-document-versions --name "${{ inputs.DistributorName }}" --region ${{ inputs.Region }} --no-paginate --query 'DocumentVersions[-1].DocumentVersion' --output text) |
| 351 | + echo "Deleting oldest version: $OLDEST_VERSION" |
| 352 | + aws ssm delete-document --name "${{ inputs.DistributorName }}" --document-version "$OLDEST_VERSION" --region ${{ inputs.Region }} |
| 353 | + fi |
| 354 | +
|
324 | 355 | - name: Update distributor |
325 | 356 | if: steps.check-distributor.outputs.exists == 'true' |
326 | 357 | run: | |
|
0 commit comments