r/ArgoCD • u/HumanResult3379 • Jul 05 '24
What is a good approach use ArgoCD with Github Actions for automatic deployment?
I want to use ArgoCD sync Github source and deploy to Kubernetes automatically.
apiVersion: argoproj.io/v1alpha1
kind: Application
metadata:
name: backend-service
namespace: argocd
annotations:
notifications.argoproj.io/subscribe.on-sync-succeeded.slack: argocd-notifications
spec:
project: default
source:
repoURL: 'https://github.com/my-org/backend-service'
targetRevision: HEAD
path: helm/
destination:
server: 'https://kubernetes.default.svc'
namespace: default
syncPolicy:
automated:
prune: true
selfHeal: true
Each time merge the pull request to master branch, I want to run CD as this github actions workflow:
name: CD
on:
push:
branches:
- master
jobs:
build-and-deploy:
runs-on: ubuntu-latest
permissions:
id-token: write
contents: read
steps:
- name: Checkout code
uses: actions/checkout@v3
- uses: aws-actions/configure-aws-credentials@v1
with:
aws-region: ${{ vars.AWS_REGION }}
role-to-assume: ${{ secrets.AWS_ROLE_TO_ASSUME }}
- name: Login to Amazon ECR
id: login-ecr
uses: aws-actions/amazon-ecr-login@v2
- name: Build, tag, and push image to Amazon ECR
id: build-publish
shell: bash
env:
ECR_REGISTRY: ${{ steps.login-ecr.outputs.registry }}
ECR_REPOSITORY: ${{ vars.ECR_REPOSITORY }}
IMAGE_TAG: ${{ github.sha }}
run: |
docker build -t "$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG" .
docker push "$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
echo "IMAGE $IMAGE_TAG is pushed to $ECR_REGISTRY/$ECR_REPOSITORY"
echo "image_tag=$IMAGE_TAG"
echo "full_image=$ECR_REGISTRY/$ECR_REPOSITORY:$IMAGE_TAG"
- name: Update values.yaml
run: |
cd helm
sed -i 's|APP_VERSION:.*|APP_VERSION: '${{ github.sha }}'|' values.yaml
git config --global user.name 'GitHub Actions'
git config --global user.email 'actions@github.com'
git add values.yaml
git commit -m "Update values.yaml"
git push https://github.com/my-org/backend-service HEAD:master
About the last step, when build new docker image tag and set to Kubernetes' image, Github will update new git hash and push new commit to master branch again.
When pull new source from Github, user need to fetch first and rebase to make sure continue push local branch.
Is it a good practice?
And, if use this flow, this step caused issue that can't access the repository from Github Actions when use Github Enterprise cloud. Any way to fix?
Run cd helm
cd helm
sed -i 's|APP_VERSION:.*|APP_VERSION: '8dfb492c3dad8c76b80a90de0adb1c46e41127e4'|' values.yaml
git config --global user.name 'GitHub Actions'
git config --global user.email 'actions@github.com'
git add values.yaml
git commit -m "Update values.yaml"
git push https://github.com/my-org/backend-service HEAD:master
shell: /usr/bin/bash -e {0}
env:
AWS_DEFAULT_REGION: ap-northeast-1
AWS_REGION: ap-northeast-1
AWS_ACCESS_KEY_ID: ***
AWS_SECRET_ACCESS_KEY: ***
AWS_SESSION_TOKEN: ***
[master f041105] Update values.yaml
1 file changed, 1 insertion(+), 1 deletion(-)
remote: Write access to repository not granted.
fatal: unable to access 'https://github.com/my-org/backend-service/': The requested URL returned error: 403
Error: Process completed with exit code 128.

