Skip to content

Commit

Permalink
Merge pull request #2 from IndraGunawan/update-dependencies
Browse files Browse the repository at this point in the history
Update action
  • Loading branch information
abronin authored May 26, 2022
2 parents 0c2444e + eb0f824 commit e40f580
Show file tree
Hide file tree
Showing 6 changed files with 23,347 additions and 34,587 deletions.
44 changes: 28 additions & 16 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,42 @@ GitHub Action to add new tags to existing Docker Image. No pull/push here becaus

## Usage

```
name: ci
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
on:
push:
branches: master
### Provide AWS credential directly.

```sh
- name: Retag test/image:dev as test/image:staging and test/image:production
uses: abronin/ecr-retag-action@v1
with:
aws-account-id: "001234567899" # optional, specify if you need to push to not main account
aws-region: us-west-2 # optional, specify if you don't provide AWS_REGION env variable
repository: test/image
tag: dev
new-tags: staging, production
env:
AWS_ACCESS_KEY_ID: ${{ secrets.AWS_ACCESS_KEY_ID }}
AWS_SECRET_ACCESS_KEY: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
AWS_REGION: us-west-2

```

### Using [aws-actions/configure-aws-credentials](https://github.com/aws-actions/configure-aws-credentials) action.

```sh
- name: Configure AWS credentials
uses: aws-actions/configure-aws-credentials@v1
with:
aws-access-key-id: ${{ secrets.AWS_ACCESS_KEY_ID }}
aws-secret-access-key: ${{ secrets.AWS_SECRET_ACCESS_KEY }}
aws-region: us-east-2

jobs:
release-stage-and-prod:
runs-on: ubuntu-latest
steps:
- name: Retag test/image:dev as test/image:staging and test/image:production
uses: abronin/ecr-retag-action@v1
with:
aws-account-id: "001234567899" # optional, specify if you need to push to not main account
aws-region: us-west-2
repository: test/image
tag: dev
new-tags: staging, production
```

## License Summary

This code is made available under the MIT license.
Expand Down
4 changes: 2 additions & 2 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ inputs:
required: false
aws-region:
description: 'AWS Region (e.g. us-east-2; not required if you provide AWS_REGION env variable)'
required: true
required: false
repository:
description: 'ECR repository name (e.g. namespace/image)'
required: true
Expand All @@ -24,5 +24,5 @@ inputs:
required: true

runs:
using: 'node12'
using: 'node16'
main: 'dist/index.js'
55,295 changes: 21,782 additions & 33,513 deletions dist/index.js

Large diffs are not rendered by default.

97 changes: 54 additions & 43 deletions index.js
Original file line number Diff line number Diff line change
@@ -1,53 +1,64 @@
const core = require('@actions/core');
const aws = require('aws-sdk')
const aws = require("@aws-sdk/client-ecr");

async function run() {
const region = core.getInput('aws-region', { required: true })
const ecr = new aws.ECR({ region })

const registryId = core.getInput('aws-account-id', { required: false })
const repositoryName = core.getInput('repository', { required: true })
const imageTag = core.getInput('tag', { required: true })
const newTags = core.getInput('new-tags', { required: true }).replace(/\s+/g, '').split(',')

const getImageParams = { registryId, repositoryName, imageIds: [{ imageTag }]}

let putImageCallback = function(err, result) {
if (err) {
if (err.code == 'ImageAlreadyExistsException') {
console.log('Image already exists, no action')
return
try {
const region = core.getInput('aws-region', { required: false })
const ecrArgs = {}
if (region) {
ecrArgs['region'] = region
}
const ecr = new aws.ECR(ecrArgs)

const registryId = core.getInput('aws-account-id', { required: false })
const repositoryName = core.getInput('repository', { required: true })
const imageTag = core.getInput('tag', { required: true })
const newTags = core.getInput('new-tags', { required: true }).replace(/\s+/g, '').split(',')

const getImageParams = { repositoryName, imageIds: [{ imageTag }] }
if (registryId) {
getImageParams['registryId'] = registryId
}

let putImageCallback = function (err, result) {
if (err) {
if (err instanceof aws.ImageAlreadyExistsException) {
core.info(`${err.message}, no action`)
return
}
core.setFailed(err.message)
} else {
let image = result.image
core.info(`Image tagged: ${image.repositoryName}:${image.imageId.imageTag}`)
core.debug(result)
}
core.setFailed(err.message)
} else {
let image = result.image
core.info(`Image tagged: ${image.repositoryName}:${image.imageId.imageTag}`)
core.debug(result)
}
}

let getImageCallback = function(err, result) {
if (err) {
core.setFailed(err)
} else {
let image = result.images[0]
core.info(`Image found: ${image.repositoryName}:${image.imageId.imageTag}`)
core.debug(image)
newTags.forEach(function(tag) {
ecr.putImage(
{
registryId: image.registryId,
repositoryName: image.repositoryName, /* required */
imageManifest: image.imageManifest, /* required */
imageTag: tag,
},
putImageCallback
)
})

let getImageCallback = function (err, result) {
if (err) {
core.setFailed(err)
} else {
let image = result.images[0]
core.info(`Image found: ${image.repositoryName}:${image.imageId.imageTag}`)
core.debug(image)
newTags.forEach(function (tag) {
ecr.putImage(
{
registryId: image.registryId,
repositoryName: image.repositoryName, /* required */
imageManifest: image.imageManifest, /* required */
imageTag: tag,
},
putImageCallback
)
})
}
}

ecr.batchGetImage(getImageParams, getImageCallback);
} catch (e) {
core.setFailed(e instanceof Error ? e.message : JSON.stringify(e))
}

ecr.batchGetImage(getImageParams, getImageCallback);
}

module.exports = run;
Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@
"homepage": "https://github.com/abronin/ecr-retag-action#readme",
"dependencies": {
"@actions/core": "^1.2.6",
"aws-sdk": "^2.788.0"
"@aws-sdk/client-ecr": "^3.99.0"
},
"devDependencies": {
"@zeit/ncc": "^0.22.3",
"@vercel/ncc": "^0.34.0",
"eslint": "^7.13.0",
"jest": "^26.6.3"
}
}
}
Loading

0 comments on commit e40f580

Please sign in to comment.