preface
With the popularity of Agile and DevOps, CI/CD has become an essential best practice for all developers in their development process, with the primary goal of delivering proven software to users faster and in shorter cycles.
It can bring us the following benefits:
- Shorten the release cycle
- To reduce risk
- Improve code quality
- More efficient feedback loops
- Visualization process
Therefore, as Serverless becomes more and more popular today, how to make Serverless projects also quickly build CI/CD is the focus of this article.
Users who are used to CI/CD are probably looking for a quick tutorial on how to set up automated deployments, so this article will look at the following popular platforms to show you how to set up automated deployments that you can push code to automate.
- Github
- Jenkins
- Coding
GitHub based automated deployment
GitHub Actions is GitHub’s automated software development workflow. You can perform any task through Actions, including CI/CD.
The premise condition
- Have hosted your Serverless project code to Github.
- The project must contain what is needed for Serverless Framework deployment
serverless.yml
.serverless.yml
Please refer to the usage mode ofwebsite. - If it is a Web function, ensure that it exists in the root directory
scf_bootstrap
For details, please refer towebsite.
steps
In order to make the deployment process easier, I published an Action for Tencent Cloud Serverless deployment in GitHub market to help you quickly complete the automated deployment.
Search For Tencent Serverless in GitHub’s Marketplace. The diagram below. It contains the detailed Action code.
To start, select Set Up a Workflow Yourself from Actions, as shown below.
If you know how to use Action, you can simply use the following sentence, which encapsulates the steps to install Serverless Framework and execute the deployment command.
- name: serverless scf deploy
uses: woodyyan/tencent-serverless-action@main
Copy the code
If you don’t know how to use Action, you can choose from the following different YML scripts based on your language. Here are Python, Java, and NodeJS scripts.
Apply toPython
project
# when pushed to the main branch code, perform the current working process # more configuration information: < https://docs.github.com/cn/actions/getting-started-with-github-actions > name: Deploy Serverless SCF on: # Monitor events and branches configuration Push: branches: -main jobs: deploy: name: deploy Serverless SCF runs-on: ubuntu-latest steps: - name: clone local repository uses: actions/checkout@v2 - name: deploy serverless uses: Woodyyan /tencent-serverless-action@main env: # Environment variables STAGE: dev # Your deployment environment SERVERLESS_PLATFORM_VENDOR: Tencent #serverless default is AWS, set to Tencent TENCENT_SECRET_ID: ${{secrecent_secret_id}} # Your Tencent cloud account sercret ID, please configure TENCENT_SECRET_KEY in settings-secrets: ${{secrets.TENCENT_SECRET_KEY}} # Your Tencent cloud account sercret key, please configure in settings-secretsCopy the code
Apply toJava
Project, please look carefully at the remarks in the code
Name: deploy serverless SCF on: # Listen on events and branches push: branches: -main jobs: build-and-deploy: runs-on: ubuntu-latest permissions: contents: read packages: write steps: - uses: actions/checkout@v2 - name: Set up JDK 11 uses: actions/setup-java@v2 with: java-version: '11' distribution: 'temurin' server-id: github # Value of the distributionManagement/repository/id field of the pom.xml settings-path: ${{github. Workspace}} # location for the settings. XML file name: Build with Gradle gradle/gradle-build-action@937999e9cc2425eddc7fd62d1053baf041147db7 with: arguments: build - name: Run: MVN -b package --file pom.xml - name: Create Zip Folder # This step is only used for Java Web functions to store JAR and SCF_bootstrap files. The Java event function only needs to specify the Jar directory in serverless.yml. Run: mkdir zip-name: Move JAR and scf_bootstrap to zip Folder # This step is only used for Java Web functions to move JAR and SCf_bootstrap files. The Java event function only needs to specify the Jar directory in serverless.yml. Note change the jar path below to /target for Maven compilation. run: cp ./build/libs/XXX.jar ./scf_bootstrap ./zip - name: deploy serverless uses: Woodyyan /tencent-serverless-action@main env: # Environment variables STAGE: dev # Your deployment environment SERVERLESS_PLATFORM_VENDOR: Tencent #serverless default is AWS, set to Tencent TENCENT_SECRET_ID: ${{secrecent_secret_id}} # Your Tencent cloud account sercret ID TENCENT_SECRET_KEY: ${{secrets.tencent_secret_key}} # Your Tencent cloud account sercret keyCopy the code
Apply toNodeJS
project
# when pushed to the main branch code, perform the current working process # more configuration information: < https://docs.github.com/cn/actions/getting-started-with-github-actions > name: Deploy Serverless SCF on: # Monitor events and branches configuration Push: branches: -main jobs: deploy: name: deploy Serverless SCF runs-on: ubuntu-latest steps: - name: clone local repository uses: actions/checkout@v2 - name: install dependency run: npm install - name: build run: npm build - name: deploy serverless uses: woodyyan/tencent-serverless-action@main env: SERVERLESS_PLATFORM_VENDOR: Tencent #serverless External default is AWS, configured as TENCENT_SECRET_ID: ${{secrecent_secret_id}} # Your Tencent cloud account sercret ID, please configure TENCENT_SECRET_KEY in settings-secrets: ${{secrets.TENCENT_SECRET_KEY}} # Your Tencent cloud account sercret key, please configure in settings-secretsCopy the code
Finally, as TENCENT_SECRET_ID and TENCENT_SECRET_KEY of Tencent Cloud are needed during deployment, these two variables need to be configured in Secrets of Github code repository Settings. As shown in the figure below. ID and KEY can be obtained from Tencent cloud access control.
After the configuration is complete, the deployment process will be triggered automatically every time the code is pushed, and the execution results and error logs can be displayed in Actions in real time. The diagram below.
You can also add testing, security checks, and release steps to the process based on your project needs.
Automated deployment based on Jenkinsfile
Jenkinsfile is applicable to Jenkins, Coding and other platforms. Therefore, automatic deployment can be completed on these platforms only after Jenkinsfile is configured.
The premise condition
- Have hosted your Serverless projects to Coding/Github/Gitlab/ code cloud platforms.
- The project must contain what is needed for Serverless Framework deployment
serverless.yml
.serverless.yml
Please refer to the usage mode ofwebsite. - If it is a Web function, ensure that it exists in the root directory
scf_bootstrap
For details, please refer towebsite.
steps
In Jenkinsfile below, I’ve included syntax for all languages, for Python, Java, and NodeJS. Please read the comments carefully.
Pipeline {agent any stages {stage(' checkout ') {steps {checkout([$class: 'GitSCM', Branches: [[name: env.GIT_BUILD_REF]], userRemoteConfigs: [[url: env.GIT_REPO_URL, credentialsId: Env.CREDENTIALS_ID]]])}} stage('Package'){Java project steps{container("maven") {echo 'Package start' sh "MVN Package "// This line is used for Java Maven project sh "./gradlew build" // this line is used for Java Gradle project sh "mkdir zip" // This line is only used for Java Web functions, Used to store JAR and SCF_bootstrap files. The Java event function only needs to specify the Jar directory in serverless.yml. Sh "cp./build/libs/ xxx.jar./scf_bootstrap./zip" // This line is only used by Java Web functions to move jar and SCf_bootstrap files. The Java event function only needs to specify the Jar directory in serverless.yml. Note change the jar path below to /target for Maven compilation. }} stage(' install dependencies ') {steps {echo 'install dependencies... 'sh' NPM I -g serverless' sh 'NPM install' // This line is used for NodeJS project echo' Install dependencies complete.'}} stage(' deploy ') {steps {echo 'Deploy... ' withCredentials([ cloudApi( credentialsId: "${env.TENCENT_CLOUD_API_CRED}", secretIdVariable: 'TENCENT_SECRET_ID', secretKeyVariable: 'TENCENT_SECRET_KEY' ), ]) {// Generate credential file sh 'echo "TENCENT_SECRET_ID=${TENCENT_SECRET_ID}\nTENCENT_SECRET_KEY=${TENCENT_SECRET_KEY}" >. Env '// Deploy Sh 'SLS deploy --debug' // Remove credentials sh' rm. env'} echo 'Deployment completed'}}}}Copy the code
Using Jenkinsfile above, you can complete CI/CD configuration on Jenkins, Coding and other platforms in one click.
Note that TENCENT_SECRET_ID and TENCENT_SECRET_KEY variables needed by Tencent cloud need to be configured on the platform.
conclusion
As a developer, you want all your code to be automated and efficient. Therefore, mastering how to quickly configure automated CI/CD processes is one of the skills that every developer must master.
Sharing these out of the box configurations here is also a hope to greatly reduce the cost of learning, quickly start the core business development.
I’ll continue to explore more DevOps practices for Serverless in the future.
If you have any questions or encounter any difficulties in the operation, please leave a message at the bottom of the article and I will reply to you.