CI/CD #16
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
name: CI/CD Workflow | |
on: | |
push: | |
branches: | |
- main | |
- qa | |
- prod | |
jobs: | |
build: | |
runs-on: ubuntu-latest | |
steps: | |
- name: Checkout code | |
uses: actions/checkout@v2 | |
with: | |
fetch-depth: 0 # this is required to fetch all tags | |
- name: Set up Node.js | |
uses: actions/setup-node@v2 | |
with: | |
node-version: '14' | |
- name: Install dependencies | |
run: npm install | |
- name: Build Docker image | |
run: docker build -t nodejs . | |
- 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: eu-west-1 | |
- name: Login to Amazon ECR | |
id: login-ecr | |
uses: aws-actions/amazon-ecr-login@v1 | |
- name: Generate tag based on branch | |
id: generate-tag | |
run: | | |
BRANCH_NAME=$(echo ${GITHUB_REF##*/} | tr / -) | |
TAG_NAME="latest" | |
if [ "$BRANCH_NAME" == "qa" ]; then | |
LAST_TAG=$(git tag -l "0.*" | sort -V | tail -n 1) | |
if [ -z "$LAST_TAG" ]; then | |
TAG_NAME="0.1.0" | |
else | |
PATCH_VERSION=$(echo $LAST_TAG | cut -d '.' -f 3) | |
NEW_PATCH_VERSION=$((PATCH_VERSION + 1)) | |
TAG_NAME="0.1.$NEW_PATCH_VERSION" | |
fi | |
elif [ "$BRANCH_NAME" == "prod" ]; then | |
LAST_TAG=$(git tag -l "1.*" | sort -V | tail -n 1) | |
if [ -z "$LAST_TAG" ]; then | |
TAG_NAME="1.0.0" | |
else | |
PATCH_VERSION=$(echo $LAST_TAG | cut -d '.' -f 3) | |
NEW_PATCH_VERSION=$((PATCH_VERSION + 1)) | |
TAG_NAME="1.0.$NEW_PATCH_VERSION" | |
fi | |
fi | |
echo "New tag: $TAG_NAME" | |
echo "TAG_NAME=$TAG_NAME" >> $GITHUB_ENV | |
- name: Tag and push Docker image to Amazon ECR | |
run: | | |
docker tag nodejs:latest ${{ secrets.ECR_REGISTRY }}:${{ env.TAG_NAME }} | |
docker push ${{ secrets.ECR_REGISTRY }}:${{ env.TAG_NAME }} | |
- name: Create Git Tag | |
run: | | |
git config user.name "github-actions" | |
git config user.email "github-actions@github.com" | |
git tag ${{ env.TAG_NAME }} | |
git push origin ${{ env.TAG_NAME }} | |
- name: Install Helm | |
run: | | |
curl -fsSL -o get_helm.sh https://raw.githubusercontent.com/helm/helm/main/scripts/get-helm-3 | |
chmod 700 get_helm.sh | |
./get_helm.sh | |
- name: Install Amazon ECR Public Gallery Helm Chart CLI | |
run: | | |
wget https://github.com/aws/amazon-ecr-public-gallery-helm-chart-cli/releases/download/v0.1.0/amazon-ecr-public-gallery-helm-chart-cli-linux-amd64.tar.gz | |
tar -xzvf amazon-ecr-public-gallery-helm-chart-cli-linux-amd64.tar.gz | |
sudo mv amazon-ecr-public-gallery-helm-chart-cli-linux-amd64/helm-ecr /usr/local/bin | |
# - name: Package Helm chart | |
# run: | | |
# # Change directories to where your chart is located | |
# cd helm/poc | |
# helm package . | |
- name: Push Helm chart to ECR | |
run: | | |
export CHART=nodejs | |
export VERSION=${{ env.TAG_NAME }} | |
helm ecr login ${{ secrets.ECR_REGISTRY }} --username AWS --password-stdin "$(aws ecr get-login-password --region eu-west-1)" | |
helm package $CHART | |
helm ecr push ${{ secrets.ECR_REGISTRY }}/$CHART:$VERSION $CHART-$VERSION.tgz | |
# | |
# - name: Login to ECR (for Helm) | |
# run: | | |
# aws ecr get-login-password --region eu-west-1 | helm registry login --username AWS --password-stdin ${{ secrets.ECR_REGISTRY }} | |
- name: Push Helm chart to ECR | |
run: | | |
# Adjust the chart and version accordingly | |
CHART_NAME=nodejs | |
CHART_VERSION=${{ env.TAG_NAME }} | |
helm chart save $CHART_NAME-$CHART_VERSION.tgz ${{ secrets.ECR_REGISTRY }}/$CHART_NAME:$CHART_VERSION | |
helm chart push ${{ secrets.ECR_REGISTRY }}/$CHART_NAME:$CHART_VERSION | |